|
| 1 | +#!/usr/bin/env sh |
| 2 | +# Build the current branch into a Docker image and push it as a tag. |
| 3 | +# Refuses to run on develop/main/master or in a detached HEAD state. |
| 4 | +# |
| 5 | +# Builds for linux/amd64 (x86_64) by default so the image runs on the real |
| 6 | +# servers even when building from an arm64 machine (Apple Silicon). |
| 7 | +# |
| 8 | +# The Dockerfile expects a prebuilt jar in build/libs/, so we run the Gradle |
| 9 | +# bootJar task first (skip with SKIP_BUILD=1 if you already built it). |
| 10 | +# |
| 11 | +# Usage: ./push-branch-image.sh |
| 12 | +# Env: IMAGE override the destination repo (default: faforever/faf-java-api) |
| 13 | +# PLATFORM override the target platform (default: linux/amd64) |
| 14 | +# SKIP_BUILD set to 1 to reuse an existing build/libs jar |
| 15 | +set -eu |
| 16 | + |
| 17 | +IMAGE="${IMAGE:-faforever/faf-java-api}" |
| 18 | +PLATFORM="${PLATFORM:-linux/amd64}" |
| 19 | + |
| 20 | +branch=$(git rev-parse --abbrev-ref HEAD) |
| 21 | + |
| 22 | +case "$branch" in |
| 23 | + develop|main|master|HEAD) |
| 24 | + echo "Refusing to push '$branch' as a Docker tag." >&2 |
| 25 | + exit 1 |
| 26 | + ;; |
| 27 | +esac |
| 28 | + |
| 29 | +# Docker tags must match [A-Za-z0-9_][A-Za-z0-9_.-]{0,127}. |
| 30 | +# Replace anything else (most commonly '/' from branch prefixes) with '-'. |
| 31 | +tag=$(printf '%s' "$branch" | sed 's/[^A-Za-z0-9_.-]/-/g') |
| 32 | + |
| 33 | +if [ "${SKIP_BUILD:-0}" != "1" ]; then |
| 34 | + echo "Building jar with ./gradlew bootJar" |
| 35 | + ./gradlew bootJar -x test |
| 36 | +fi |
| 37 | + |
| 38 | +echo "Building $IMAGE:$tag from branch $branch for $PLATFORM" |
| 39 | +# buildx with --push builds and pushes in one step. --provenance=false keeps |
| 40 | +# the registry tag pointing at a plain image manifest rather than a manifest |
| 41 | +# list, which some deployment tooling expects. |
| 42 | +docker buildx build \ |
| 43 | + --platform "$PLATFORM" \ |
| 44 | + --provenance=false \ |
| 45 | + -t "$IMAGE:$tag" \ |
| 46 | + --push \ |
| 47 | + . |
| 48 | + |
| 49 | +echo "Done: $IMAGE:$tag" |
0 commit comments