#!/usr/bin/env bash # # build-dist.sh — cross-compile the rickub CLI and package release archives. # # Usage: scripts/build-dist.sh # VERSION=1.2.3 scripts/build-dist.sh # # is the bare version WITHOUT a leading "v" (e.g. 1.2.3 or 0.1.0-rc.42). # It is stamped into the binary and used in the archive file names. # # Output (in ./dist): # rickub___.tar.gz for each target # SHA256SUMS checksums of the archives # # Everything is built with CGO_ENABLED=0 so a single Linux amd64 runner can # produce all four targets (the rickub CI fleet is Linux/amd64 only). set -euo pipefail MODULE="rickub.com/rickub/cli" BINARY="rickub" # Targets: rickub CI runners are linux/amd64 only, so every artifact is a # cross-compile. Pure Go + CGO_ENABLED=0 makes that safe. TARGETS=( "linux/amd64" "linux/arm64" "darwin/amd64" "darwin/arm64" ) VERSION="${1:-${VERSION:-}}" if [ -z "${VERSION}" ]; then echo "build-dist.sh: no version given (pass as \$1 or set \$VERSION)" >&2 exit 2 fi VERSION="${VERSION#v}" REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "${REPO_ROOT}" DIST="${REPO_ROOT}/dist" rm -rf "${DIST}" mkdir -p "${DIST}" # Stage outside the work tree so nothing untracked is left behind. STAGE="$(mktemp -d "${TMPDIR:-/tmp}/rickub-dist.XXXXXX")" trap 'rm -rf "${STAGE}"' EXIT # sha256sum (GNU/Linux) vs shasum (macOS dev boxes). sha256() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$@" else shasum -a 256 "$@" fi } LDFLAGS="-s -w -X ${MODULE}/cmd.Version=${VERSION}" echo "==> building ${BINARY} ${VERSION}" echo " ldflags: ${LDFLAGS}" go version for target in "${TARGETS[@]}"; do GOOS="${target%%/*}" GOARCH="${target##*/}" name="${BINARY}_${VERSION}_${GOOS}_${GOARCH}" out="${STAGE}/${name}" mkdir -p "${out}" echo "==> ${GOOS}/${GOARCH}" CGO_ENABLED=0 GOOS="${GOOS}" GOARCH="${GOARCH}" \ go build -trimpath -ldflags "${LDFLAGS}" -o "${out}/${BINARY}" . # Ship docs alongside the binary when they exist. LICENSE does not exist in # this repo yet; it is picked up automatically once someone adds one. contents=("${BINARY}") for extra in README.md LICENSE LICENSE.md LICENSE.txt; do if [ -f "${REPO_ROOT}/${extra}" ]; then cp "${REPO_ROOT}/${extra}" "${out}/${extra}" contents+=("${extra}") fi done tar -czf "${DIST}/${name}.tar.gz" -C "${out}" "${contents[@]}" echo " -> dist/${name}.tar.gz" done echo "==> SHA256SUMS" ( cd "${DIST}" # Deterministic ordering, names only (no ./ prefix) so `sha256sum -c` works # from inside an unpacked download. # shellcheck disable=SC2035 sha256 *.tar.gz > SHA256SUMS ) cat "${DIST}/SHA256SUMS" echo "==> done: $(ls -1 "${DIST}" | wc -l | tr -d ' ') files in dist/"