rickub/clipublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/rickub/cli.git
git clone ssh://git@rickub.com/rickub/cli.git
build-dist.sh · 100 lines · 2.8 KBBash Blame HistoryRaw
Initial import of the rickub CLI as a standalone public project 1a1d430 Olivier Girardot 9h ago1#!/usr/bin/env bash
2#
3# build-dist.sh — cross-compile the rickub CLI and package release archives.
4#
5# Usage: scripts/build-dist.sh <version>
6# VERSION=1.2.3 scripts/build-dist.sh
7#
8# <version> is the bare version WITHOUT a leading "v" (e.g. 1.2.3 or 0.1.0-rc.42).
9# It is stamped into the binary and used in the archive file names.
10#
11# Output (in ./dist):
12# rickub_<version>_<os>_<arch>.tar.gz for each target
13# SHA256SUMS checksums of the archives
14#
15# Everything is built with CGO_ENABLED=0 so a single Linux amd64 runner can
16# produce all four targets (the rickub CI fleet is Linux/amd64 only).
17
18set -euo pipefail
19
20MODULE="rickub.com/rickub/cli"
21BINARY="rickub"
22
23# Targets: rickub CI runners are linux/amd64 only, so every artifact is a
24# cross-compile. Pure Go + CGO_ENABLED=0 makes that safe.
25TARGETS=(
26 "linux/amd64"
27 "linux/arm64"
28 "darwin/amd64"
29 "darwin/arm64"
30)
31
32VERSION="${1:-${VERSION:-}}"
33if [ -z "${VERSION}" ]; then
34 echo "build-dist.sh: no version given (pass as \$1 or set \$VERSION)" >&2
35 exit 2
36fi
37VERSION="${VERSION#v}"
38
39REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
40cd "${REPO_ROOT}"
41
42DIST="${REPO_ROOT}/dist"
43rm -rf "${DIST}"
44mkdir -p "${DIST}"
45
46# Stage outside the work tree so nothing untracked is left behind.
47STAGE="$(mktemp -d "${TMPDIR:-/tmp}/rickub-dist.XXXXXX")"
48trap 'rm -rf "${STAGE}"' EXIT
49
50# sha256sum (GNU/Linux) vs shasum (macOS dev boxes).
51sha256() {
52 if command -v sha256sum >/dev/null 2>&1; then
53 sha256sum "$@"
54 else
55 shasum -a 256 "$@"
56 fi
57}
58
59LDFLAGS="-s -w -X ${MODULE}/cmd.Version=${VERSION}"
60
61echo "==> building ${BINARY} ${VERSION}"
62echo " ldflags: ${LDFLAGS}"
63go version
64
65for target in "${TARGETS[@]}"; do
66 GOOS="${target%%/*}"
67 GOARCH="${target##*/}"
68 name="${BINARY}_${VERSION}_${GOOS}_${GOARCH}"
69 out="${STAGE}/${name}"
70 mkdir -p "${out}"
71
72 echo "==> ${GOOS}/${GOARCH}"
73 CGO_ENABLED=0 GOOS="${GOOS}" GOARCH="${GOARCH}" \
74 go build -trimpath -ldflags "${LDFLAGS}" -o "${out}/${BINARY}" .
75
76 # Ship docs alongside the binary when they exist. LICENSE does not exist in
77 # this repo yet; it is picked up automatically once someone adds one.
78 contents=("${BINARY}")
79 for extra in README.md LICENSE LICENSE.md LICENSE.txt; do
80 if [ -f "${REPO_ROOT}/${extra}" ]; then
81 cp "${REPO_ROOT}/${extra}" "${out}/${extra}"
82 contents+=("${extra}")
83 fi
84 done
85
86 tar -czf "${DIST}/${name}.tar.gz" -C "${out}" "${contents[@]}"
87 echo " -> dist/${name}.tar.gz"
88done
89
90echo "==> SHA256SUMS"
91(
92 cd "${DIST}"
93 # Deterministic ordering, names only (no ./ prefix) so `sha256sum -c` works
94 # from inside an unpacked download.
95 # shellcheck disable=SC2035
96 sha256 *.tar.gz > SHA256SUMS
97)
98cat "${DIST}/SHA256SUMS"
99
100echo "==> done: $(ls -1 "${DIST}" | wc -l | tr -d ' ') files in dist/"