- #!/usr/bin/env bash
- # Build, sign (Developer ID Application), notarise, and push the macOS arm64
- # binary of Roneo for Forge's `binaries-macos` protected landing job.
- #
- # Usage:
- # ci/binaries/macos/build.sh <commit-sha>
- #
- # Environment:
- # FORGE_SIGNING "keychain" (Developer ID + notarytool on forge-mac-1)
- # or "adhoc" (local dev test, default)
- # FORGE_SIGNING_TEAM Apple Team ID (default: 4SKW2Z97A2)
- # FORGE_SIGNING_KEYCHAIN keychain path (default: ~/Library/Keychains/forge-signing.keychain-db)
- # FORGE_SIGNING_UNLOCK_FILE 0400 file holding the keychain password (default: ~/forge/keychain-password)
- # FORGE_NOTARY_PROFILE notarytool keychain profile (default: forge-notary)
- # FORGE_SIGNING_FLAT 1 on a flat signing host (requires FORGE_SIGNING_RUN)
- # FORGE_SIGNING_RUN "landing" (default -> candidate tag) or "change"
- # FORGE_MACOS_POOL OCI pool repo, e.g. $CLEYROP_REGISTRY/forge-mac-binaries/roneo-binaries
- # (unset skips crane push for local dev builds)
- # FORGE_MACOS_REGISTRY_USER Harbor push robot (default: robot$forge-mac-1-push)
- # FORGE_MACOS_REGISTRY_PASSWORD robot secret (optional; else read from FORGE_MACOS_ROBOT_FILE)
- # FORGE_MACOS_ROBOT_FILE 0400 file holding the robot secret (default: ~/forge/harbor-robot)
- # FORGE_MACOS_RECORD output record JSON (default: roneo-macos.json)
- # FORGE_MACOS_NOTARISATIONS notarisations record JSON (default: notarisations.json)
- # FORGE_MACOS_MIN_FREE_GIB minimum free GiB required before building (default: 5)
- set -euo pipefail
- target_triple="aarch64-apple-darwin"
- platform="darwin/arm64"
- bundle_id="com.cleyrop.roneo"
- binary_name="roneo"
- check_free_space() {
- local dir=$1 floor_gib=$2 free_kib
- while [ ! -d "$dir" ] && [ "$dir" != / ]; do
- dir=$(dirname "$dir")
- done
- free_kib=$(df -Pk "$dir" | awk 'NR == 2 {print $4}')
- case $free_kib in
- '' | *[!0-9]*)
- echo "build.sh: cannot read free space of $dir" >&2
- return 1
- ;;
- esac
- if [ "$free_kib" -lt $((floor_gib * 1024 * 1024)) ]; then
- echo "build.sh: $dir has $((free_kib / 1024 / 1024)) GiB free, below ${floor_gib} GiB" >&2
- return 1
- fi
- }
- tag_kind() {
- if [ "${FORGE_SIGNING_FLAT:-}" = 1 ] && [ -z "${FORGE_SIGNING_RUN:-}" ]; then
- echo "build.sh: FORGE_SIGNING_FLAT=1 requires FORGE_SIGNING_RUN" >&2
- return 2
- fi
- case ${FORGE_SIGNING_RUN:-landing} in
- landing) echo "candidate" ;;
- change) echo "change" ;;
- *)
- echo "build.sh: FORGE_SIGNING_RUN=${FORGE_SIGNING_RUN}: expected landing or change" >&2
- return 2
- ;;
- esac
- }
- # Notarisation record in Forge server schema:
- # {"commit":"<40-hex>","notarisations":[{"id":"<uuid>","sha256":"<64-hex>","file":"<basename>","status":"<status>"}]}
- notarisations_file=""
- notarisations_commit=""
- notarisations_entries=""
- notarisations_count=0
- notarisations_most=16
- notarisations_write() {
- [ -n "$notarisations_file" ] || return 0
- if ! printf '{"commit":"%s","notarisations":[%s]}\n' "$notarisations_commit" "$notarisations_entries" \
- >"${notarisations_file}.part" ||
- ! mv -f "${notarisations_file}.part" "$notarisations_file"; then
- rm -f "${notarisations_file}.part" 2>/dev/null || true
- echo "build.sh: failed to write notarisations record $notarisations_file" >&2
- return 1
- fi
- }
- notarisations_begin() {
- notarisations_file=$1
- notarisations_commit=$2
- notarisations_entries=""
- notarisations_count=0
- notarisations_write
- }
- notarisations_entry() {
- local id=$1 sum=$2 name=$3 status=$4 bytes
- local id_form='^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
- local sum_form='^[0-9a-f]{64}$'
- local status_form='^[A-Za-z ]{1,32}$'
- [[ $id =~ $id_form ]] || return 1
- [[ $sum =~ $sum_form ]] || return 1
- [[ $status =~ $status_form ]] || return 1
- case $name in '' | . | .. | */*) return 1 ;; esac
- if [[ $name == *[[:cntrl:]]* ]]; then return 1; fi
- bytes=$(printf '%s' "$name" | wc -c) || return 1
- [ "$bytes" -le 255 ] || return 1
- name=${name//\\/\\\\}
- name=${name//\"/\\\"}
- printf '{"id":"%s","sha256":"%s","file":"%s","status":"%s"}' "$id" "$sum" "$name" "$status"
- }
- submit_notary() {
- local file=$1 sum name answer status="" entry
- local id_re='"id"[[:space:]]*:[[:space:]]*"([^"]*)"'
- local status_re='"status"[[:space:]]*:[[:space:]]*"([^"]*)"'
- local sum_form='^[0-9a-f]{64}$'
- shift
- submission=""
- name=${file##*/}
- if ! sum=$(shasum -a 256 <"$file"); then
- echo "build.sh: cannot hash $name; skipping notarisation" >&2
- return 1
- fi
- sum=${sum%%[[:space:]]*}
- if ! [[ $sum =~ $sum_form ]]; then
- echo "build.sh: unexpected sha256 ($sum) for $name" >&2
- return 1
- fi
- answer=$(xcrun notarytool submit "$file" "$@" --wait --timeout 60m --output-format json 2>&1) || true
- echo "$answer"
- if [[ $answer =~ $id_re ]]; then submission=${BASH_REMATCH[1]}; fi
- if [[ $answer =~ $status_re ]]; then status=${BASH_REMATCH[1]}; fi
- if [ -n "$submission" ]; then
- if ! entry=$(notarisations_entry "$submission" "$sum" "$name" "$status"); then
- echo "build.sh: notary answer out of record schema (id=$submission, status=$status)" >&2
- elif [ "$notarisations_count" -ge "$notarisations_most" ]; then
- echo "build.sh: notarisations record reached max entries ($notarisations_most)" >&2
- elif [[ $notarisations_entries != *"\"id\":\"$submission\""* ]]; then
- notarisations_entries+=${notarisations_entries:+,}$entry
- notarisations_count=$((notarisations_count + 1))
- notarisations_write || return 1
- fi
- fi
- if [ "$status" != "Accepted" ]; then
- echo "build.sh: Apple notarisation did not accept $name (status=${status:-unknown})" >&2
- return 1
- fi
- }
- push_image() {
- local binary=$1 tag=$2 commit=$3 layer digest manifest config
- layer="${work}/$(basename "$binary").tar"
- COPYFILE_DISABLE=1 tar -C "$(dirname "$binary")" \
- --uid 0 --gid 0 --uname root --gname wheel \
- -cf "$layer" "$(basename "$binary")"
- crane append --oci-empty-base -f "$layer" -t "$tag" >/dev/null
- crane mutate "$tag" -t "$tag" --set-platform "$platform" \
- --label org.opencontainers.image.revision="$commit" \
- --label org.opencontainers.image.description="Roneo static macOS arm64 binary" \
- --label org.opencontainers.image.vendor=Cleyrop >/dev/null
- digest=$(crane digest "$tag")
- manifest=$(crane manifest "${tag%:*}@${digest}")
- config=$(crane config "${tag%:*}@${digest}")
- if [ "$(jq '.layers | length' <<<"$manifest")" != "1" ]; then
- echo "build.sh: $tag does not have exactly one layer" >&2
- return 1
- fi
- if [ "$(jq -r '.os + "/" + .architecture' <<<"$config")" != "$platform" ]; then
- echo "build.sh: $tag platform config is not $platform" >&2
- return 1
- fi
- echo "$digest"
- }
- write_record() {
- local file=$1 commit=$2 name=$3 image=$4 digest=$5
- printf '{"commit":"%s","platform":"%s","binaries":[{"name":"%s","image":"%s","digest":"%s"}]}\n' \
- "$commit" "$platform" "$name" "$image" "$digest" >"$file"
- }
- main() {
- local commit=${1:-$(git rev-parse HEAD)}
- local signing_mode=${FORGE_SIGNING:-adhoc}
- local team=${FORGE_SIGNING_TEAM:-4SKW2Z97A2}
- local pool=${FORGE_MACOS_POOL:-}
- local record=${FORGE_MACOS_RECORD:-roneo-macos.json}
- local notarisations=${FORGE_MACOS_NOTARISATIONS:-notarisations.json}
- local floor=${FORGE_MACOS_MIN_FREE_GIB:-5}
- local reg_user=${FORGE_MACOS_REGISTRY_USER:-robot\$forge-mac-1-push}
- local robot_file=${FORGE_MACOS_ROBOT_FILE:-$HOME/forge/harbor-robot}
- local kind
- if ! [[ $commit =~ ^[0-9a-f]{40}$ ]]; then
- echo "build.sh: $commit is not a full 40-hex commit SHA" >&2
- return 2
- fi
- notarisations_begin "$notarisations" "$commit" || return 1
- kind=$(tag_kind) || return 2
- if [ "$(uname -s)/$(uname -m)" != "Darwin/arm64" ]; then
- echo "build.sh: Apple Silicon macOS is required, got $(uname -s)/$(uname -m)" >&2
- return 1
- fi
- if [ "$(git rev-parse HEAD)" != "$commit" ]; then
- echo "build.sh: checkout is at $(git rev-parse HEAD), expected $commit" >&2
- return 1
- fi
- local tools=(cargo codesign lipo shasum)
- if [ "$signing_mode" = "keychain" ]; then
- tools+=(security xcrun ditto)
- fi
- if [ -n "$pool" ]; then
- tools+=(crane jq tar)
- fi
- for tool in "${tools[@]}"; do
- command -v "$tool" >/dev/null || {
- echo "build.sh: missing required tool on runner: $tool" >&2
- return 1
- }
- done
- if [ -n "$pool" ]; then
- if [ -z "${FORGE_MACOS_REGISTRY_PASSWORD:-}" ] && ! { [ -r "$robot_file" ] && [ -s "$robot_file" ]; }; then
- echo "build.sh: FORGE_MACOS_REGISTRY_PASSWORD unset and $robot_file missing or empty" >&2
- return 1
- fi
- fi
- local base=${TMPDIR:-/tmp}
- check_free_space "$base" "$floor"
- check_free_space "${CARGO_HOME:-$HOME/.cargo}" "$floor"
- work=$(mktemp -d "${base%/}/roneo-macos.XXXXXX")
- keychain_to_restore=""
- original_keychains=""
- cleanup() {
- if [ -n "$keychain_to_restore" ]; then
- # shellcheck disable=SC2086
- security list-keychains -d user -s $original_keychains 2>/dev/null || true
- security lock-keychain "$keychain_to_restore" 2>/dev/null || true
- fi
- rm -rf "$work" || echo "build.sh: failed to remove $work" >&2
- }
- trap cleanup EXIT
- trap 'trap - EXIT; cleanup; exit 130' INT
- trap 'trap - EXIT; cleanup; exit 143' TERM
- mkdir -p "$work/out"
- echo "build.sh: building $binary_name (${commit:0:10}) for $target_triple"
- local cargo_home="${CARGO_HOME:-$HOME/.cargo}"
- if [ "$signing_mode" = "keychain" ]; then
- cargo_home="$work/cargo"
- fi
- CARGO_HOME="$cargo_home" \
- CARGO_TARGET_DIR="$work/target" \
- RUSTFLAGS="--remap-path-prefix=${HOME}=/build --remap-path-prefix=$(pwd)=/src -C link-arg=-Wl,-S,-x,-dead_strip" \
- cargo build --release --locked --target "$target_triple" -p roneo-cli --bin "$binary_name"
- cp "$work/target/$target_triple/release/$binary_name" "$work/out/$binary_name"
- rm -rf "$work/target"
- local bin="$work/out/$binary_name"
- if [ "$(lipo -archs "$bin")" != "arm64" ]; then
- echo "build.sh: $binary_name is not arm64 alone ($(lipo -archs "$bin"))" >&2
- return 1
- fi
- if [ "$signing_mode" = "keychain" ]; then
- local keychain=${FORGE_SIGNING_KEYCHAIN:-$HOME/Library/Keychains/forge-signing.keychain-db}
- local unlock=${FORGE_SIGNING_UNLOCK_FILE:-$HOME/forge/keychain-password}
- local profile=${FORGE_NOTARY_PROFILE:-forge-notary}
- [ -f "$keychain" ] || {
- echo "build.sh: signing keychain not found at $keychain" >&2
- return 1
- }
- { [ -r "$unlock" ] && [ -s "$unlock" ]; } || {
- echo "build.sh: keychain unlock file $unlock is missing or empty" >&2
- return 1
- }
- original_keychains=$(security list-keychains -d user | sed -e 's/"//g' | xargs)
- keychain_to_restore="$keychain"
- security unlock-keychain -p "$(cat "$unlock")" "$keychain"
- # shellcheck disable=SC2086
- security list-keychains -d user -s "$keychain" $original_keychains
- local identities identity
- identities=$(security find-identity -v -p codesigning "$keychain")
- identity=$(printf '%s\n' "$identities" |
- awk -v t="($team)\"" '/Developer ID Application/ && $NF == t {print $2; exit}')
- if [ -z "$identity" ]; then
- echo "build.sh: no valid Developer ID Application identity for team $team in $keychain" >&2
- security find-identity "$keychain" >&2 || true
- return 1
- fi
- echo "build.sh: signing $binary_name as $bundle_id (team $team)"
- codesign --force --options=runtime --timestamp --keychain "$keychain" \
- --sign "$identity" --identifier "$bundle_id" "$bin"
- codesign --verify --strict "$bin"
- mkdir -p "$work/notarise"
- cp -Rp "$bin" "$work/notarise/"
- ditto -c -k "$work/notarise" "$work/notarise.zip"
- echo "build.sh: submitting $binary_name to Apple notarisation service"
- local notary_args=(--keychain-profile "$profile" --keychain "$keychain")
- if ! submit_notary "$work/notarise.zip" "${notary_args[@]}"; then
- if [ -n "${submission:-}" ]; then
- xcrun notarytool log "$submission" "${notary_args[@]}" >&2 || true
- fi
- return 1
- fi
- local req="anchor apple generic and certificate 1[field.1.2.840.113635.100.6.2.6] exists"
- req+=" and certificate leaf[field.1.2.840.113635.100.6.1.13] exists"
- req+=" and certificate leaf[subject.OU] = \"$team\""
- codesign --verify --strict -R="$req" "$bin"
- echo "build.sh: $binary_name signed, notarised, and verified for team $team"
- elif [ "$signing_mode" = "adhoc" ]; then
- codesign --force --sign - --identifier "$bundle_id" "$bin"
- codesign --verify --strict "$bin"
- else
- echo "build.sh: unsupported FORGE_SIGNING=$signing_mode (expected keychain or adhoc)" >&2
- return 2
- fi
- local ver
- ver=$("$bin" --version)
- echo "build.sh: verified executable: $ver"
- if [ -n "$pool" ]; then
- export DOCKER_CONFIG="$work/docker"
- if [ -n "${FORGE_MACOS_REGISTRY_PASSWORD:-}" ]; then
- printf '%s' "$FORGE_MACOS_REGISTRY_PASSWORD" |
- crane auth login "${pool%%/*}" -u "$reg_user" --password-stdin >/dev/null
- else
- crane auth login "${pool%%/*}" -u "$reg_user" --password-stdin <"$robot_file" >/dev/null
- fi
- local digest
- digest=$(push_image "$bin" "${pool}:${binary_name}-darwin-arm64-${kind}-${commit}" "$commit")
- echo "build.sh: pushed $binary_name as ${pool}@${digest}"
- write_record "$record" "$commit" "$binary_name" "$pool" "$digest"
- else
- local sha size
- sha=$(shasum -a 256 <"$bin" | awk '{print $1}')
- size=$(stat -f%z "$bin")
- printf '{"commit":"%s","platform":"%s","signing":"%s","binaries":[{"name":"%s","sha256":"%s","size":%s}]}\n' \
- "$commit" "$platform" "$signing_mode" "$binary_name" "$sha" "$size" >"$record"
- fi
- echo "build.sh: wrote $record"
- }
- if [ "${BASH_SOURCE[0]}" = "$0" ]; then
- main "$@"
- fi