|
| 1 | +# Notarizes the macOS binaries with Apple. release.yml runs it after the binaries are built and signed, and publishes |
| 2 | +# the release only once it passes. Without notarization, macOS refuses to run a binary that was downloaded in a browser |
| 3 | +# ("Apple could not verify..."). A bare binary can't carry its notarization ticket (only apps, disk images and |
| 4 | +# installer packages can be stapled), so macOS looks the ticket up online, by the binary's code hash, the first time it |
| 5 | +# runs. Nothing here changes the archives. |
| 6 | +# |
| 7 | +# It signs in to Apple's notary service with an App Store Connect API key: the APPLE_NOTARY_KEY (the .p8 file's text), |
| 8 | +# APPLE_NOTARY_KEY_ID and APPLE_NOTARY_ISSUER_ID secrets (internal_docs/sdk-generation.md in DocSpring's monorepo). |
| 9 | +name: Notarize the macOS binaries |
| 10 | + |
| 11 | +on: |
| 12 | + workflow_call: |
| 13 | + inputs: |
| 14 | + plan: |
| 15 | + required: true |
| 16 | + type: string |
| 17 | + |
| 18 | +jobs: |
| 19 | + notarize: |
| 20 | + runs-on: macos-15 |
| 21 | + timeout-minutes: 90 |
| 22 | + steps: |
| 23 | + - name: Fetch the macOS builds |
| 24 | + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 |
| 25 | + with: |
| 26 | + pattern: artifacts-build-local-*-apple-darwin |
| 27 | + path: builds/ |
| 28 | + merge-multiple: true |
| 29 | + - name: Notarize |
| 30 | + env: |
| 31 | + APPLE_NOTARY_KEY: ${{ secrets.APPLE_NOTARY_KEY }} |
| 32 | + APPLE_NOTARY_KEY_ID: ${{ secrets.APPLE_NOTARY_KEY_ID }} |
| 33 | + APPLE_NOTARY_ISSUER_ID: ${{ secrets.APPLE_NOTARY_ISSUER_ID }} |
| 34 | + run: | |
| 35 | + set -euo pipefail |
| 36 | + for name in APPLE_NOTARY_KEY APPLE_NOTARY_KEY_ID APPLE_NOTARY_ISSUER_ID; do |
| 37 | + if [ -z "${!name}" ]; then |
| 38 | + echo "::error::The $name secret isn't set on this repository" |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | + done |
| 42 | +
|
| 43 | + shopt -s nullglob |
| 44 | + archives=(builds/docspring-cli-*-apple-darwin.tar.xz) |
| 45 | + if [ ${#archives[@]} -eq 0 ]; then |
| 46 | + echo "::error::No macOS archives in builds/: $(ls builds)" |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | +
|
| 50 | + # One submission with every macOS binary, each in a folder named after its target |
| 51 | + mkdir -p notarize |
| 52 | + for archive in "${archives[@]}"; do |
| 53 | + target=$(basename "$archive" .tar.xz) |
| 54 | + target=${target#docspring-cli-} |
| 55 | + mkdir -p "extracted/$target" "notarize/$target" |
| 56 | + tar -xJf "$archive" -C "extracted/$target" |
| 57 | + binary="extracted/$target/docspring-cli-$target/docspring" |
| 58 | + codesign --verify --strict --verbose=2 "$binary" |
| 59 | + # Apple refuses a binary without the hardened runtime (build-setup.yml asks dist's codesign for it) |
| 60 | + if ! codesign --display --verbose=2 "$binary" 2>&1 | grep -q '^CodeDirectory .*flags=0x[0-9a-f]*([^)]*runtime'; then |
| 61 | + echo "::error::$archive's binary isn't signed with the hardened runtime:" |
| 62 | + codesign --display --verbose=2 "$binary" |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | + cp "$binary" "notarize/$target/docspring" |
| 66 | + done |
| 67 | + ditto -c -k --keepParent notarize notarize.zip |
| 68 | +
|
| 69 | + key="$RUNNER_TEMP/notary_key.p8" |
| 70 | + printf '%s\n' "$APPLE_NOTARY_KEY" > "$key" |
| 71 | + auth=(--key "$key" --key-id "$APPLE_NOTARY_KEY_ID" --issuer "$APPLE_NOTARY_ISSUER_ID") |
| 72 | +
|
| 73 | + # notarytool's exit status alone doesn't say whether Apple accepted the binaries: the submission's status does |
| 74 | + set +e |
| 75 | + xcrun notarytool submit notarize.zip "${auth[@]}" --wait --timeout 1h --output-format json > submission.json |
| 76 | + submit_status=$? |
| 77 | + set -e |
| 78 | + cat submission.json |
| 79 | + id=$(jq -r '.id // empty' submission.json) |
| 80 | + status=$(jq -r '.status // empty' submission.json) |
| 81 | + if [ -n "$id" ]; then |
| 82 | + # Apple's report: lists any problems, and warnings even when it accepts the binaries |
| 83 | + xcrun notarytool log "$id" "${auth[@]}" |
| 84 | + fi |
| 85 | + if [ "$status" != Accepted ]; then |
| 86 | + echo "::error::Apple didn't notarize the binaries (notarytool exited $submit_status, status: ${status:-none}). Its report above says why." |
| 87 | + exit 1 |
| 88 | + fi |
| 89 | + echo "Notarized: ${archives[*]}" |
0 commit comments