Files
foxhole-guard/.github/workflows/release.yml
T
2026-08-18 12:58:20 +03:00

265 lines
10 KiB
YAML

name: release
on:
push:
branches:
- main
workflow_dispatch:
permissions:
actions: read
attestations: write
contents: write
id-token: write
env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"
concurrency:
group: release-main
cancel-in-progress: false
jobs:
release:
name: publish verified dev bytes
if: github.repository == 'foxhole-team/foxhole-guard'
runs-on: ubuntu-24.04
timeout-minutes: 20
steps:
- name: checkout main
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
persist-credentials: false
- name: verify public signed main authority
id: authority
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
[[ "$GITHUB_REF" == "refs/heads/main" ]] || {
echo "Release must run from refs/heads/main." >&2
exit 1
}
visibility="$(gh api "repos/$GITHUB_REPOSITORY" --jq '.visibility')"
[[ "$visibility" == "public" ]] || {
echo "Refusing to publish while $GITHUB_REPOSITORY is not public." >&2
exit 1
}
verified="$(gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA" --jq '.commit.verification.verified')"
reason="$(gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA" --jq '.commit.verification.reason')"
[[ "$verified" == "true" ]] || {
echo "main commit is not GitHub-verified (reason: $reason). Sign it with the registered release key." >&2
exit 1
}
main_tree="$(git rev-parse 'HEAD^{tree}')"
[[ "$main_tree" =~ ^[0-9a-f]{40}$ ]] || {
echo "Could not resolve the main Git tree." >&2
exit 1
}
echo "main_tree=$main_tree" >> "$GITHUB_OUTPUT"
# Publishable only when an already-successful dev push had byte-for-byte identical source and kept its immutable signed candidate.
- name: find matching successful dev candidate
id: candidate
shell: bash
env:
GH_TOKEN: ${{ github.token }}
MAIN_TREE: ${{ steps.authority.outputs.main_tree }}
run: |
set -euo pipefail
runs_json="$RUNNER_TEMP/dev-runs.json"
gh api \
-X GET \
"repos/$GITHUB_REPOSITORY/actions/workflows/android.yml/runs" \
-f branch=dev \
-f event=push \
-f status=success \
-f per_page=100 \
> "$runs_json"
selected_run=""
selected_sha=""
selected_artifact=""
while IFS=$'\t' read -r run_id dev_sha; do
[[ "$run_id" =~ ^[1-9][0-9]*$ ]] || continue
[[ "$dev_sha" =~ ^[0-9a-f]{40}$ ]] || continue
dev_tree="$(gh api "repos/$GITHUB_REPOSITORY/git/commits/$dev_sha" --jq '.tree.sha')"
[[ "$dev_tree" == "$MAIN_TREE" ]] || continue
artifact_name="foxhole-app-$dev_sha"
artifacts_json="$RUNNER_TEMP/dev-artifacts-$run_id.json"
gh api "repos/$GITHUB_REPOSITORY/actions/runs/$run_id/artifacts" > "$artifacts_json"
artifact_count="$(
jq \
--arg name "$artifact_name" '
[.artifacts[] | select(.name == $name and .expired == false)] | length
' \
"$artifacts_json"
)"
[[ "$artifact_count" == "1" ]] || continue
selected_run="$run_id"
selected_sha="$dev_sha"
selected_artifact="$artifact_name"
break
done < <(jq -r '.workflow_runs[] | [.id, .head_sha] | @tsv' "$runs_json")
[[ -n "$selected_run" ]] || {
echo "No successful, unexpired dev candidate has the same Git tree as main." >&2
exit 1
}
{
echo "run_id=$selected_run"
echo "dev_sha=$selected_sha"
echo "artifact_name=$selected_artifact"
} >> "$GITHUB_OUTPUT"
- name: install APK verification tools
uses: android-actions/setup-android@40fd30fb8d7440372e1316f5d1809ec01dcd3699 # v4
- name: install pinned Android build tools
shell: bash
run: sdkmanager "build-tools;37.0.0"
- name: download exact dev candidate
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
with:
name: ${{ steps.candidate.outputs.artifact_name }}
path: ${{ runner.temp }}/foxhole-release-candidate
github-token: ${{ github.token }}
repository: ${{ github.repository }}
run-id: ${{ steps.candidate.outputs.run_id }}
- name: verify candidate bytes and release policy
id: policy
shell: bash
env:
GH_TOKEN: ${{ github.token }}
DEV_SHA: ${{ steps.candidate.outputs.dev_sha }}
MAIN_TREE: ${{ steps.authority.outputs.main_tree }}
run: |
set -euo pipefail
candidate_dir="$RUNNER_TEMP/foxhole-release-candidate"
scripts/package-release-candidate.sh verify "$candidate_dir" "$DEV_SHA" "$MAIN_TREE"
metadata="$candidate_dir/CANDIDATE.json"
tag="$(jq -er '.tag' "$metadata")"
version_name="$(jq -er '.versionName' "$metadata")"
version_code="$(jq -er '.versionCode' "$metadata")"
[[ "$tag" == "v$version_name" ]] || {
echo "Candidate tag/version mismatch." >&2
exit 1
}
published_code="$(scripts/package-release-candidate.sh published-code "$GITHUB_REPOSITORY")"
(( version_code > published_code )) || {
echo "versionCode $version_code is not newer than published $published_code." >&2
exit 1
}
if gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$tag" >/dev/null 2>&1; then
echo "Tag $tag already exists; release is intentionally non-overwriting." >&2
exit 1
fi
if gh release view "$tag" >/dev/null 2>&1; then
echo "Release $tag already exists; release is intentionally non-overwriting." >&2
exit 1
fi
{
echo "tag=$tag"
echo "version_name=$version_name"
} >> "$GITHUB_OUTPUT"
- name: attest exact release files
uses: actions/attest-build-provenance@96b4a1ef7235a096b17240c259729fdd70c83d45 # v2
with:
subject-path: ${{ runner.temp }}/foxhole-release-candidate/*
# Create as a draft first so a failed upload/byte comparison never exposes a partial release.
- name: create and verify draft release
shell: bash
env:
GH_TOKEN: ${{ github.token }}
RELEASE_TAG: ${{ steps.policy.outputs.tag }}
VERSION_NAME: ${{ steps.policy.outputs.version_name }}
run: |
set -euo pipefail
candidate_dir="$RUNNER_TEMP/foxhole-release-candidate"
assets=()
while IFS= read -r asset; do
assets+=("$asset")
done < <(find "$candidate_dir" -mindepth 1 -maxdepth 1 -type f | LC_ALL=C sort)
[[ ${#assets[@]} -eq 6 ]] || {
echo "Expected exactly six verified release files." >&2
exit 1
}
cleanup_enabled=true
cleanup_draft() {
if [[ "$cleanup_enabled" == true ]]; then
gh release delete "$RELEASE_TAG" --cleanup-tag --yes >/dev/null 2>&1 || true
fi
}
trap cleanup_draft EXIT
gh release create "$RELEASE_TAG" \
"${assets[@]}" \
--draft \
--generate-notes \
--target "$GITHUB_SHA" \
--title "FoxHole Guard $VERSION_NAME"
releases_json="$RUNNER_TEMP/draft-releases.json"
gh api -X GET "repos/$GITHUB_REPOSITORY/releases" -f per_page=100 > "$releases_json"
release_id="$(
jq -er \
--arg tag "$RELEASE_TAG" '
[.[] | select(.tag_name == $tag and .draft == true)] |
if length == 1 then .[0].id else error("expected exactly one draft release") end
' \
"$releases_json"
)"
release_json="$RUNNER_TEMP/draft-release.json"
gh api "repos/$GITHUB_REPOSITORY/releases/$release_id" > "$release_json"
published_dir="$RUNNER_TEMP/published-release"
mkdir -p "$published_dir"
for source in "${assets[@]}"; do
published="$published_dir/$(basename "$source")"
asset_id="$(
jq -er \
--arg name "$(basename "$source")" '
[.assets[] | select(.name == $name)] |
if length == 1 then .[0].id else error("expected exactly one release asset") end
' \
"$release_json"
)"
gh api -H 'Accept: application/octet-stream' \
"repos/$GITHUB_REPOSITORY/releases/assets/$asset_id" > "$published"
[[ -f "$published" ]] || {
echo "Draft release is missing $(basename "$source")." >&2
exit 1
}
cmp --silent "$source" "$published" || {
echo "Published bytes differ for $(basename "$source")." >&2
exit 1
}
done
gh api --method PATCH \
"repos/$GITHUB_REPOSITORY/releases/$release_id" \
-F draft=false >/dev/null
tag_type="$(gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$RELEASE_TAG" --jq '.object.type')"
tag_sha="$(gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$RELEASE_TAG" --jq '.object.sha')"
[[ "$tag_type" == "commit" && "$tag_sha" == "$GITHUB_SHA" ]] || {
echo "Release tag does not point directly at the verified main commit." >&2
exit 1
}
gh release edit "$RELEASE_TAG" --draft=false --latest
is_draft="$(gh release view "$RELEASE_TAG" --json isDraft --jq '.isDraft')"
[[ "$is_draft" == "false" ]] || {
echo "Release was not published after byte verification." >&2
exit 1
}
cleanup_enabled=false