rickub/clipublic Fork 0
1a1d4302402433417a40905bf2e2a7b85504872e
Commits
Clone
git clone https://git.rickub.com/rickub/cli.git
git clone ssh://git@rickub.com/rickub/cli.git
Initial import of the rickub CLI as a standalone public project 1a1d430Unverified · on 1a1d4302402433417a40905bf2e2a7b85504872e · Olivier Girardot · 10h ago
build-dist.sh · 100 lines · 2.8 KBBash Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
#!/usr/bin/env bash
#
# build-dist.sh — cross-compile the rickub CLI and package release archives.
#
# Usage:  scripts/build-dist.sh <version>
#         VERSION=1.2.3 scripts/build-dist.sh
#
# <version> 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_<version>_<os>_<arch>.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/"