roneocibinariesmacosbuild.sh

ee892817f152551895f8482d4db15cefa51ef865

At commit ee89281

367 lines · 13.6 KiBShellRaw
  1. #!/usr/bin/env bash
  2. # Build, sign (Developer ID Application), notarise, and push the macOS arm64
  3. # binary of Roneo for Forge's `binaries-macos` protected landing job.
  4. #
  5. # Usage:
  6. # ci/binaries/macos/build.sh <commit-sha>
  7. #
  8. # Environment:
  9. # FORGE_SIGNING "keychain" (Developer ID + notarytool on forge-mac-1)
  10. # or "adhoc" (local dev test, default)
  11. # FORGE_SIGNING_TEAM Apple Team ID (default: 4SKW2Z97A2)
  12. # FORGE_SIGNING_KEYCHAIN keychain path (default: ~/Library/Keychains/forge-signing.keychain-db)
  13. # FORGE_SIGNING_UNLOCK_FILE 0400 file holding the keychain password (default: ~/forge/keychain-password)
  14. # FORGE_NOTARY_PROFILE notarytool keychain profile (default: forge-notary)
  15. # FORGE_SIGNING_FLAT 1 on a flat signing host (requires FORGE_SIGNING_RUN)
  16. # FORGE_SIGNING_RUN "landing" (default -> candidate tag) or "change"
  17. # FORGE_MACOS_POOL OCI pool repo, e.g. $CLEYROP_REGISTRY/forge-mac-binaries/roneo-binaries
  18. # (unset skips crane push for local dev builds)
  19. # FORGE_MACOS_REGISTRY_USER Harbor push robot (default: robot$forge-mac-1-push)
  20. # FORGE_MACOS_REGISTRY_PASSWORD robot secret (optional; else read from FORGE_MACOS_ROBOT_FILE)
  21. # FORGE_MACOS_ROBOT_FILE 0400 file holding the robot secret (default: ~/forge/harbor-robot)
  22. # FORGE_MACOS_RECORD output record JSON (default: roneo-macos.json)
  23. # FORGE_MACOS_NOTARISATIONS notarisations record JSON (default: notarisations.json)
  24. # FORGE_MACOS_MIN_FREE_GIB minimum free GiB required before building (default: 5)
  25. set -euo pipefail
  26. target_triple="aarch64-apple-darwin"
  27. platform="darwin/arm64"
  28. bundle_id="com.cleyrop.roneo"
  29. binary_name="roneo"
  30. check_free_space() {
  31. local dir=$1 floor_gib=$2 free_kib
  32. while [ ! -d "$dir" ] && [ "$dir" != / ]; do
  33. dir=$(dirname "$dir")
  34. done
  35. free_kib=$(df -Pk "$dir" | awk 'NR == 2 {print $4}')
  36. case $free_kib in
  37. '' | *[!0-9]*)
  38. echo "build.sh: cannot read free space of $dir" >&2
  39. return 1
  40. ;;
  41. esac
  42. if [ "$free_kib" -lt $((floor_gib * 1024 * 1024)) ]; then
  43. echo "build.sh: $dir has $((free_kib / 1024 / 1024)) GiB free, below ${floor_gib} GiB" >&2
  44. return 1
  45. fi
  46. }
  47. tag_kind() {
  48. if [ "${FORGE_SIGNING_FLAT:-}" = 1 ] && [ -z "${FORGE_SIGNING_RUN:-}" ]; then
  49. echo "build.sh: FORGE_SIGNING_FLAT=1 requires FORGE_SIGNING_RUN" >&2
  50. return 2
  51. fi
  52. case ${FORGE_SIGNING_RUN:-landing} in
  53. landing) echo "candidate" ;;
  54. change) echo "change" ;;
  55. *)
  56. echo "build.sh: FORGE_SIGNING_RUN=${FORGE_SIGNING_RUN}: expected landing or change" >&2
  57. return 2
  58. ;;
  59. esac
  60. }
  61. # Notarisation record in Forge server schema:
  62. # {"commit":"<40-hex>","notarisations":[{"id":"<uuid>","sha256":"<64-hex>","file":"<basename>","status":"<status>"}]}
  63. notarisations_file=""
  64. notarisations_commit=""
  65. notarisations_entries=""
  66. notarisations_count=0
  67. notarisations_most=16
  68. notarisations_write() {
  69. [ -n "$notarisations_file" ] || return 0
  70. if ! printf '{"commit":"%s","notarisations":[%s]}\n' "$notarisations_commit" "$notarisations_entries" \
  71. >"${notarisations_file}.part" ||
  72. ! mv -f "${notarisations_file}.part" "$notarisations_file"; then
  73. rm -f "${notarisations_file}.part" 2>/dev/null || true
  74. echo "build.sh: failed to write notarisations record $notarisations_file" >&2
  75. return 1
  76. fi
  77. }
  78. notarisations_begin() {
  79. notarisations_file=$1
  80. notarisations_commit=$2
  81. notarisations_entries=""
  82. notarisations_count=0
  83. notarisations_write
  84. }
  85. notarisations_entry() {
  86. local id=$1 sum=$2 name=$3 status=$4 bytes
  87. local id_form='^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
  88. local sum_form='^[0-9a-f]{64}$'
  89. local status_form='^[A-Za-z ]{1,32}$'
  90. [[ $id =~ $id_form ]] || return 1
  91. [[ $sum =~ $sum_form ]] || return 1
  92. [[ $status =~ $status_form ]] || return 1
  93. case $name in '' | . | .. | */*) return 1 ;; esac
  94. if [[ $name == *[[:cntrl:]]* ]]; then return 1; fi
  95. bytes=$(printf '%s' "$name" | wc -c) || return 1
  96. [ "$bytes" -le 255 ] || return 1
  97. name=${name//\\/\\\\}
  98. name=${name//\"/\\\"}
  99. printf '{"id":"%s","sha256":"%s","file":"%s","status":"%s"}' "$id" "$sum" "$name" "$status"
  100. }
  101. submit_notary() {
  102. local file=$1 sum name answer status="" entry
  103. local id_re='"id"[[:space:]]*:[[:space:]]*"([^"]*)"'
  104. local status_re='"status"[[:space:]]*:[[:space:]]*"([^"]*)"'
  105. local sum_form='^[0-9a-f]{64}$'
  106. shift
  107. submission=""
  108. name=${file##*/}
  109. if ! sum=$(shasum -a 256 <"$file"); then
  110. echo "build.sh: cannot hash $name; skipping notarisation" >&2
  111. return 1
  112. fi
  113. sum=${sum%%[[:space:]]*}
  114. if ! [[ $sum =~ $sum_form ]]; then
  115. echo "build.sh: unexpected sha256 ($sum) for $name" >&2
  116. return 1
  117. fi
  118. answer=$(xcrun notarytool submit "$file" "$@" --wait --timeout 60m --output-format json 2>&1) || true
  119. echo "$answer"
  120. if [[ $answer =~ $id_re ]]; then submission=${BASH_REMATCH[1]}; fi
  121. if [[ $answer =~ $status_re ]]; then status=${BASH_REMATCH[1]}; fi
  122. if [ -n "$submission" ]; then
  123. if ! entry=$(notarisations_entry "$submission" "$sum" "$name" "$status"); then
  124. echo "build.sh: notary answer out of record schema (id=$submission, status=$status)" >&2
  125. elif [ "$notarisations_count" -ge "$notarisations_most" ]; then
  126. echo "build.sh: notarisations record reached max entries ($notarisations_most)" >&2
  127. elif [[ $notarisations_entries != *"\"id\":\"$submission\""* ]]; then
  128. notarisations_entries+=${notarisations_entries:+,}$entry
  129. notarisations_count=$((notarisations_count + 1))
  130. notarisations_write || return 1
  131. fi
  132. fi
  133. if [ "$status" != "Accepted" ]; then
  134. echo "build.sh: Apple notarisation did not accept $name (status=${status:-unknown})" >&2
  135. return 1
  136. fi
  137. }
  138. push_image() {
  139. local binary=$1 tag=$2 commit=$3 layer digest manifest config
  140. layer="${work}/$(basename "$binary").tar"
  141. COPYFILE_DISABLE=1 tar -C "$(dirname "$binary")" \
  142. --uid 0 --gid 0 --uname root --gname wheel \
  143. -cf "$layer" "$(basename "$binary")"
  144. crane append --oci-empty-base -f "$layer" -t "$tag" >/dev/null
  145. crane mutate "$tag" -t "$tag" --set-platform "$platform" \
  146. --label org.opencontainers.image.revision="$commit" \
  147. --label org.opencontainers.image.description="Roneo static macOS arm64 binary" \
  148. --label org.opencontainers.image.vendor=Cleyrop >/dev/null
  149. digest=$(crane digest "$tag")
  150. manifest=$(crane manifest "${tag%:*}@${digest}")
  151. config=$(crane config "${tag%:*}@${digest}")
  152. if [ "$(jq '.layers | length' <<<"$manifest")" != "1" ]; then
  153. echo "build.sh: $tag does not have exactly one layer" >&2
  154. return 1
  155. fi
  156. if [ "$(jq -r '.os + "/" + .architecture' <<<"$config")" != "$platform" ]; then
  157. echo "build.sh: $tag platform config is not $platform" >&2
  158. return 1
  159. fi
  160. echo "$digest"
  161. }
  162. write_record() {
  163. local file=$1 commit=$2 name=$3 image=$4 digest=$5
  164. printf '{"commit":"%s","platform":"%s","binaries":[{"name":"%s","image":"%s","digest":"%s"}]}\n' \
  165. "$commit" "$platform" "$name" "$image" "$digest" >"$file"
  166. }
  167. main() {
  168. local commit=${1:-$(git rev-parse HEAD)}
  169. local signing_mode=${FORGE_SIGNING:-adhoc}
  170. local team=${FORGE_SIGNING_TEAM:-4SKW2Z97A2}
  171. local pool=${FORGE_MACOS_POOL:-}
  172. local record=${FORGE_MACOS_RECORD:-roneo-macos.json}
  173. local notarisations=${FORGE_MACOS_NOTARISATIONS:-notarisations.json}
  174. local floor=${FORGE_MACOS_MIN_FREE_GIB:-5}
  175. local reg_user=${FORGE_MACOS_REGISTRY_USER:-robot\$forge-mac-1-push}
  176. local robot_file=${FORGE_MACOS_ROBOT_FILE:-$HOME/forge/harbor-robot}
  177. local kind
  178. if ! [[ $commit =~ ^[0-9a-f]{40}$ ]]; then
  179. echo "build.sh: $commit is not a full 40-hex commit SHA" >&2
  180. return 2
  181. fi
  182. notarisations_begin "$notarisations" "$commit" || return 1
  183. kind=$(tag_kind) || return 2
  184. if [ "$(uname -s)/$(uname -m)" != "Darwin/arm64" ]; then
  185. echo "build.sh: Apple Silicon macOS is required, got $(uname -s)/$(uname -m)" >&2
  186. return 1
  187. fi
  188. if [ "$(git rev-parse HEAD)" != "$commit" ]; then
  189. echo "build.sh: checkout is at $(git rev-parse HEAD), expected $commit" >&2
  190. return 1
  191. fi
  192. local tools=(cargo codesign lipo shasum)
  193. if [ "$signing_mode" = "keychain" ]; then
  194. tools+=(security xcrun ditto)
  195. fi
  196. if [ -n "$pool" ]; then
  197. tools+=(crane jq tar)
  198. fi
  199. for tool in "${tools[@]}"; do
  200. command -v "$tool" >/dev/null || {
  201. echo "build.sh: missing required tool on runner: $tool" >&2
  202. return 1
  203. }
  204. done
  205. if [ -n "$pool" ]; then
  206. if [ -z "${FORGE_MACOS_REGISTRY_PASSWORD:-}" ] && ! { [ -r "$robot_file" ] && [ -s "$robot_file" ]; }; then
  207. echo "build.sh: FORGE_MACOS_REGISTRY_PASSWORD unset and $robot_file missing or empty" >&2
  208. return 1
  209. fi
  210. fi
  211. local base=${TMPDIR:-/tmp}
  212. check_free_space "$base" "$floor"
  213. check_free_space "${CARGO_HOME:-$HOME/.cargo}" "$floor"
  214. work=$(mktemp -d "${base%/}/roneo-macos.XXXXXX")
  215. keychain_to_restore=""
  216. original_keychains=""
  217. cleanup() {
  218. if [ -n "$keychain_to_restore" ]; then
  219. # shellcheck disable=SC2086
  220. security list-keychains -d user -s $original_keychains 2>/dev/null || true
  221. security lock-keychain "$keychain_to_restore" 2>/dev/null || true
  222. fi
  223. rm -rf "$work" || echo "build.sh: failed to remove $work" >&2
  224. }
  225. trap cleanup EXIT
  226. trap 'trap - EXIT; cleanup; exit 130' INT
  227. trap 'trap - EXIT; cleanup; exit 143' TERM
  228. mkdir -p "$work/out"
  229. echo "build.sh: building $binary_name (${commit:0:10}) for $target_triple"
  230. local cargo_home="${CARGO_HOME:-$HOME/.cargo}"
  231. if [ "$signing_mode" = "keychain" ]; then
  232. cargo_home="$work/cargo"
  233. fi
  234. CARGO_HOME="$cargo_home" \
  235. CARGO_TARGET_DIR="$work/target" \
  236. RUSTFLAGS="--remap-path-prefix=${HOME}=/build --remap-path-prefix=$(pwd)=/src -C link-arg=-Wl,-S,-x,-dead_strip" \
  237. cargo build --release --locked --target "$target_triple" -p roneo-cli --bin "$binary_name"
  238. cp "$work/target/$target_triple/release/$binary_name" "$work/out/$binary_name"
  239. rm -rf "$work/target"
  240. local bin="$work/out/$binary_name"
  241. if [ "$(lipo -archs "$bin")" != "arm64" ]; then
  242. echo "build.sh: $binary_name is not arm64 alone ($(lipo -archs "$bin"))" >&2
  243. return 1
  244. fi
  245. if [ "$signing_mode" = "keychain" ]; then
  246. local keychain=${FORGE_SIGNING_KEYCHAIN:-$HOME/Library/Keychains/forge-signing.keychain-db}
  247. local unlock=${FORGE_SIGNING_UNLOCK_FILE:-$HOME/forge/keychain-password}
  248. local profile=${FORGE_NOTARY_PROFILE:-forge-notary}
  249. [ -f "$keychain" ] || {
  250. echo "build.sh: signing keychain not found at $keychain" >&2
  251. return 1
  252. }
  253. { [ -r "$unlock" ] && [ -s "$unlock" ]; } || {
  254. echo "build.sh: keychain unlock file $unlock is missing or empty" >&2
  255. return 1
  256. }
  257. original_keychains=$(security list-keychains -d user | sed -e 's/"//g' | xargs)
  258. keychain_to_restore="$keychain"
  259. security unlock-keychain -p "$(cat "$unlock")" "$keychain"
  260. # shellcheck disable=SC2086
  261. security list-keychains -d user -s "$keychain" $original_keychains
  262. local identities identity
  263. identities=$(security find-identity -v -p codesigning "$keychain")
  264. identity=$(printf '%s\n' "$identities" |
  265. awk -v t="($team)\"" '/Developer ID Application/ && $NF == t {print $2; exit}')
  266. if [ -z "$identity" ]; then
  267. echo "build.sh: no valid Developer ID Application identity for team $team in $keychain" >&2
  268. security find-identity "$keychain" >&2 || true
  269. return 1
  270. fi
  271. echo "build.sh: signing $binary_name as $bundle_id (team $team)"
  272. codesign --force --options=runtime --timestamp --keychain "$keychain" \
  273. --sign "$identity" --identifier "$bundle_id" "$bin"
  274. codesign --verify --strict "$bin"
  275. mkdir -p "$work/notarise"
  276. cp -Rp "$bin" "$work/notarise/"
  277. ditto -c -k "$work/notarise" "$work/notarise.zip"
  278. echo "build.sh: submitting $binary_name to Apple notarisation service"
  279. local notary_args=(--keychain-profile "$profile" --keychain "$keychain")
  280. if ! submit_notary "$work/notarise.zip" "${notary_args[@]}"; then
  281. if [ -n "${submission:-}" ]; then
  282. xcrun notarytool log "$submission" "${notary_args[@]}" >&2 || true
  283. fi
  284. return 1
  285. fi
  286. local req="anchor apple generic and certificate 1[field.1.2.840.113635.100.6.2.6] exists"
  287. req+=" and certificate leaf[field.1.2.840.113635.100.6.1.13] exists"
  288. req+=" and certificate leaf[subject.OU] = \"$team\""
  289. codesign --verify --strict -R="$req" "$bin"
  290. echo "build.sh: $binary_name signed, notarised, and verified for team $team"
  291. elif [ "$signing_mode" = "adhoc" ]; then
  292. codesign --force --sign - --identifier "$bundle_id" "$bin"
  293. codesign --verify --strict "$bin"
  294. else
  295. echo "build.sh: unsupported FORGE_SIGNING=$signing_mode (expected keychain or adhoc)" >&2
  296. return 2
  297. fi
  298. local ver
  299. ver=$("$bin" --version)
  300. echo "build.sh: verified executable: $ver"
  301. if [ -n "$pool" ]; then
  302. export DOCKER_CONFIG="$work/docker"
  303. if [ -n "${FORGE_MACOS_REGISTRY_PASSWORD:-}" ]; then
  304. printf '%s' "$FORGE_MACOS_REGISTRY_PASSWORD" |
  305. crane auth login "${pool%%/*}" -u "$reg_user" --password-stdin >/dev/null
  306. else
  307. crane auth login "${pool%%/*}" -u "$reg_user" --password-stdin <"$robot_file" >/dev/null
  308. fi
  309. local digest
  310. digest=$(push_image "$bin" "${pool}:${binary_name}-darwin-arm64-${kind}-${commit}" "$commit")
  311. echo "build.sh: pushed $binary_name as ${pool}@${digest}"
  312. write_record "$record" "$commit" "$binary_name" "$pool" "$digest"
  313. else
  314. local sha size
  315. sha=$(shasum -a 256 <"$bin" | awk '{print $1}')
  316. size=$(stat -f%z "$bin")
  317. printf '{"commit":"%s","platform":"%s","signing":"%s","binaries":[{"name":"%s","sha256":"%s","size":%s}]}\n' \
  318. "$commit" "$platform" "$signing_mode" "$binary_name" "$sha" "$size" >"$record"
  319. fi
  320. echo "build.sh: wrote $record"
  321. }
  322. if [ "${BASH_SOURCE[0]}" = "$0" ]; then
  323. main "$@"
  324. fi