build_binary.sh (1685B)
1 #!/bin/bash 2 set -euo pipefail 3 4 # --------------------------------------------------------------------------- 5 # Build a static git-jci binary for the requested platform. 6 # 7 # Required environment variables (set by the calling docker run command): 8 # GOOS - Target OS (linux | windows | darwin) 9 # GOARCH - Target arch (amd64 | arm64 | arm) 10 # OUTPUT_BINARY_NAME - Final filename placed in ${OUTPUT_DIR} 11 # e.g. git-jci-linux-amd64 / git-jci-windows-amd64.exe 12 # 13 # Optional: 14 # GOARM - ARM version for GOARCH=arm (default: 7) 15 # --------------------------------------------------------------------------- 16 17 REPO_DIR="/tmp/repo" 18 OUTPUT_DIR="${REPO_DIR}/bin" 19 ENTRY_POINT="./cmd/git-jci" 20 21 # Ensure required vars are present 22 : "${GOOS:?GOOS must be set}" 23 : "${GOARCH:?GOARCH must be set}" 24 : "${OUTPUT_BINARY_NAME:?OUTPUT_BINARY_NAME must be set}" 25 26 GOARM="${GOARM:-7}" 27 28 cd "${REPO_DIR}" 29 30 # Read the version from the VERSION file at the repo root. 31 VERSION="$(cat "${REPO_DIR}/VERSION" | tr -d '[:space:]')" 32 echo "[${OUTPUT_BINARY_NAME}] Version: ${VERSION}" 33 34 echo "[${OUTPUT_BINARY_NAME}] Downloading dependencies..." 35 go mod download 36 37 echo "[${OUTPUT_BINARY_NAME}] Building for GOOS=${GOOS} GOARCH=${GOARCH} GOARM=${GOARM}..." 38 mkdir -p "${OUTPUT_DIR}" 39 40 CGO_ENABLED=0 \ 41 GOOS="${GOOS}" \ 42 GOARCH="${GOARCH}" \ 43 GOARM="${GOARM}" \ 44 go build \ 45 -buildvcs=false \ 46 -tags "netgo osusergo" \ 47 -ldflags "-s -w -X main.version=${VERSION} -extldflags '-static'" \ 48 -o "${OUTPUT_DIR}/${OUTPUT_BINARY_NAME}" \ 49 "${ENTRY_POINT}" 50 51 echo "[${OUTPUT_BINARY_NAME}] Binary built successfully: ${OUTPUT_DIR}/${OUTPUT_BINARY_NAME}"