hex/scripts/release-app.sh

153 lines
7 KiB
Shell
Executable file

#!/bin/sh
set -eu
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
team_id=${VOICE_CONTROL_TEAM_ID:?Set VOICE_CONTROL_TEAM_ID to the Apple Developer signing team}
. "$root/scripts/macos-signing.sh"
mode=${1:-prepare}
version=$(hex_version "$root")
build_number=$(hex_build_number "$version")
bucket=${HEX_RELEASE_BUCKET:-hex-releases}
base_url=${HEX_RELEASE_BASE_URL:-https://downloads.hex.anoma.ly}
# The appcast this release extends; a staging prefix starts from the live one.
appcast_url=${HEX_RELEASE_APPCAST_URL:-$base_url/appcast.xml}
release_notes=${HEX_RELEASE_NOTES:-$root/docs/releases/$version.md}
dist="$root/dist"
updates="$dist/updates"
artifact="HEX-$version-arm64.dmg"
update_artifact="HEX-$version-arm64.zip"
latest_artifact="HEX-latest-arm64.dmg"
if [ "$mode" != "prepare" ] && [ "$mode" != "publish" ]; then
echo "Usage: $0 [prepare|publish]" >&2
exit 1
fi
if [ -n "$(git -C "$root" status --porcelain)" ]; then
echo "Commit or remove all working-tree changes before releasing." >&2
exit 1
fi
# GitHub Actions checks out the exact pushed commit, detached from any branch.
if [ "${GITHUB_ACTIONS:-}" != true ]; then
upstream=$(git -C "$root" rev-parse --abbrev-ref '@{upstream}' 2>/dev/null || true)
if [ -z "$upstream" ] || [ "$(git -C "$root" rev-parse HEAD)" != "$(git -C "$root" rev-parse "$upstream")" ]; then
echo "Push the release commit to its upstream branch before releasing." >&2
exit 1
fi
fi
case "$build_number" in
''|*[!0-9]*)
echo "HEX_BUILD_NUMBER must be a positive integer." >&2
exit 1
;;
esac
published_build=$(curl --fail --silent --show-error "$appcast_url" \
| sed -n 's|.*<sparkle:version>\([0-9][0-9]*\)</sparkle:version>.*|\1|p' \
| head -1)
if [ -z "$published_build" ]; then
echo "Could not read the published build from $appcast_url." >&2
exit 1
fi
# A staging rehearsal may rebuild the published version; Sparkle offers only newer builds.
if [ "$build_number" -le "$published_build" ] && [ "${HEX_RELEASE_REQUIRE_NEWER:-1}" != 0 ]; then
echo "Build $build_number must be newer than published build $published_build." >&2
exit 1
fi
if [ "$mode" = "publish" ]; then
if [ ! -f "$dist/$artifact" ] || [ ! -f "$dist/$update_artifact" ] || [ ! -f "$updates/appcast.xml" ]; then
echo "Prepare and validate version $version before publishing it." >&2
exit 1
fi
if ! grep -Fq "$base_url/releases/$update_artifact" "$updates/appcast.xml" \
|| ! grep -Fq 'sparkle:edSignature=' "$updates/appcast.xml"; then
echo "The prepared appcast does not contain a signed $update_artifact update." >&2
exit 1
fi
xcrun stapler validate "$dist/$artifact"
spctl --assess --type open --context context:primary-signature --verbose=2 "$dist/$artifact"
verification=$(mktemp -d "${TMPDIR:-/tmp}/hex-release-validation.XXXXXX")
mountpoint="$verification/dmg"
mkdir -p "$mountpoint" "$verification/update"
trap 'hdiutil detach -quiet "$mountpoint" 2>/dev/null || true; rm -rf "$verification"' EXIT HUP INT TERM
hdiutil attach -quiet -nobrowse -readonly -mountpoint "$mountpoint" "$dist/$artifact"
"$root/scripts/validate-app.sh" "$mountpoint/Hex.app" Hex.app "$version" "$build_number" >/dev/null
/usr/bin/ditto -x -k "$dist/$update_artifact" "$verification/update"
"$root/scripts/validate-app.sh" "$verification/update/HEX.app" HEX.app "$version" "$build_number" >/dev/null
xcrun stapler validate "$verification/update/HEX.app"
diff -qr "$mountpoint/Hex.app" "$verification/update/HEX.app"
hdiutil detach -quiet "$mountpoint"
rm -rf "$verification"
trap - EXIT HUP INT TERM
wrangler r2 object put "$bucket/releases/$artifact" --remote \
--file "$dist/$artifact" \
--content-type application/x-apple-diskimage \
--cache-control 'public, max-age=31536000, immutable'
wrangler r2 object put "$bucket/releases/$update_artifact" --remote \
--file "$dist/$update_artifact" \
--content-type application/zip \
--cache-control 'public, max-age=31536000, immutable'
verification=$(mktemp -d "${TMPDIR:-/tmp}/hex-release-downloads.XXXXXX")
trap 'rm -rf "$verification"' EXIT HUP INT TERM
for file in "$artifact" "$update_artifact"; do
curl --fail --silent --show-error --proto '=https' \
--connect-timeout 15 --max-time 600 "$base_url/releases/$file" -o "$verification/$file"
cmp -s "$dist/$file" "$verification/$file"
done
sh "$root/scripts/publish-app-mirror.sh" "$version" "$dist/$artifact" \
"$release_notes" "$(git -C "$root" rev-parse HEAD)"
# Both artifact origins must serve the exact bytes before moving public pointers.
wrangler r2 object put "$bucket/releases/$latest_artifact" --remote \
--file "$dist/$artifact" \
--content-type application/x-apple-diskimage \
--cache-control 'no-cache'
curl --fail --silent --show-error --connect-timeout 15 --max-time 600 \
"$base_url/releases/$latest_artifact" -o "$verification/$latest_artifact"
cmp -s "$dist/$artifact" "$verification/$latest_artifact"
wrangler r2 object put "$bucket/appcast.xml" --remote \
--file "$updates/appcast.xml" \
--content-type application/xml \
--cache-control 'no-cache'
curl --fail --silent --show-error --connect-timeout 15 --max-time 60 \
"$base_url/appcast.xml" -o "$verification/appcast.xml"
cmp -s "$updates/appcast.xml" "$verification/appcast.xml"
echo "$base_url/releases/$artifact"
exit 0
fi
if [ ! -f "$release_notes" ]; then
echo "Add release notes at $release_notes or set HEX_RELEASE_NOTES." >&2
exit 1
fi
export HEX_VERSION="$version" HEX_BUILD_NUMBER="$build_number"
bundle=$("$root/scripts/build-app.sh")
"$root/scripts/validate-app.sh" "$bundle" Hex.app "$version" "$build_number" >/dev/null
rm -rf "$dist/update-staging" "$updates"
rm -f "$dist/$artifact" "$dist/$update_artifact"
mkdir -p "$dist/update-staging" "$updates"
"$root/scripts/notarize-app.sh" "$bundle" "$dist/$artifact" Hex
# Sparkle 2.9.4 discovers updates by exact app filename or matching bundle ID.
# Retain the old Rust archive name so 2.0.x can find the new bundle identity.
/usr/bin/ditto "$bundle" "$dist/update-staging/HEX.app"
/usr/bin/ditto -c -k --sequesterRsrc --keepParent "$dist/update-staging/HEX.app" "$dist/$update_artifact"
cp "$dist/$update_artifact" "$updates/$update_artifact"
cp "$release_notes" "$updates/HEX-$version-arm64.md"
curl --fail --silent --show-error "$appcast_url" -o "$updates/appcast.xml"
sparkle_dir=$("$root/scripts/setup-sparkle.sh")
# CI passes the exported key file; a local release reads Kit's login keychain.
if [ -n "${HEX_SPARKLE_KEY_FILE:-}" ]; then
cat "$HEX_SPARKLE_KEY_FILE"
else
security find-generic-password -s 'https://sparkle-project.org' -a ed25519 -w
fi | "$sparkle_dir/bin/generate_appcast" \
--ed-key-file - \
--download-url-prefix "$base_url/releases/" \
--embed-release-notes \
--maximum-deltas 0 \
--maximum-versions 5 \
"$updates"
grep -Fq "$base_url/releases/$update_artifact" "$updates/appcast.xml"
grep -Fq 'sparkle:edSignature=' "$updates/appcast.xml"
echo "$dist/$artifact"
echo "Prepared but not published. Run $0 publish after validation." >&2