diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7a2534c6f..fc8dce2ab 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -33,9 +33,14 @@ on: description: Exact committed release candidate (defaults to the triggering commit) required: false type: string + reuse_run: + description: Verified Release run to reuse when only its phone-check receipt was added + required: false + type: string permissions: contents: write + actions: read concurrency: group: ${{ github.workflow }}-${{ inputs.revision || github.sha }} @@ -45,6 +50,8 @@ jobs: secret-scan: name: Secret Scan runs-on: ubuntu-latest + outputs: + reuse_artifacts: ${{ steps.reuse.outputs.reuse_artifacts }} steps: - name: Require an immutable candidate env: @@ -61,6 +68,79 @@ jobs: ref: ${{ inputs.revision || github.sha }} fetch-depth: 0 + - name: Verify identical source and every gate before reusing artifacts + id: reuse + env: + GH_TOKEN: ${{ github.token }} + VERIFIED_RUN: ${{ inputs.reuse_run }} + CANDIDATE: ${{ inputs.revision || github.sha }} + run: | + python3 - <<'PY' + import json + import os + from pathlib import Path + import re + import subprocess + + run_id = os.environ.get('VERIFIED_RUN', '') + output = Path(os.environ['GITHUB_OUTPUT']) + if not run_id: + with output.open('a') as stream: + stream.write('reuse_artifacts=false\n') + raise SystemExit(0) + if not re.fullmatch(r'[1-9][0-9]*', run_id): + raise SystemExit('A numeric verified Release run ID is required') + repository = os.environ['GITHUB_REPOSITORY'] + + def api(path): + return json.loads(subprocess.check_output(['gh', 'api', path], text=True)) + + run = api(f'repos/{repository}/actions/runs/{run_id}') + if (run['path'] != '.github/workflows/release.yml' or + run['event'] != 'push' or run['head_branch'] != 'master' or + run['repository']['full_name'] != repository): + raise SystemExit('Artifacts must come from this repository master Release workflow') + source = run['head_sha'] + candidate = os.environ['CANDIDATE'] + if (not re.fullmatch(r'[0-9a-f]{40}', source) or + not re.fullmatch(r'[0-9a-f]{40}', candidate)): + raise SystemExit('The verified source must be an immutable commit') + subprocess.run(['git', 'merge-base', '--is-ancestor', source, candidate], check=True) + version = subprocess.check_output( + ['python3', 'scripts/check-version.py', '--print-version'], text=True).strip() + receipt = f'releases/phone-checks/{version}.json' + changed = subprocess.check_output( + ['git', 'diff', '--name-only', '--no-renames', source, candidate], text=True).splitlines() + if changed != [receipt]: + raise SystemExit('Reuse requires identical source with only the phone-check receipt added') + if subprocess.run(['git', 'cat-file', '-e', source + ':' + receipt], + capture_output=True).returncode == 0: + raise SystemExit('The source run already had a phone receipt; do not race its publication') + subprocess.run(['python3', 'scripts/check-phone-check.py', version, + '--revision', candidate], check=True) + required = { + 'Secret Scan', 'Test (0/3)', 'Test (1/3)', 'Test (2/3)', 'Visual Tests', + 'F-Droid / F-Droid security and clean build', 'Verify F-Droid reproducibility', + 'Build Web', 'Build Chrome Extension', 'Build Firefox Add-on', 'Build Windows', + 'Build Android', + 'Build Linux AppImage (x86_64, ubuntu-22.04, x86_64)', + 'Build Linux AppImage (aarch64, ubuntu-22.04-arm, aarch64)', + 'Build Linux Deb (amd64, ubuntu-22.04)', 'Build Linux Deb (arm64, ubuntu-22.04-arm)', + 'Build Linux RPM (x86_64, ubuntu-22.04)', 'Build Linux RPM (aarch64, ubuntu-22.04-arm)', + 'Build Linux Snap (x86_64, ubuntu-22.04)', 'Build Linux Snap (aarch64, ubuntu-22.04-arm)', + 'Build Linux Flatpak (x86_64, ubuntu-22.04)', 'Build Linux Flatpak (aarch64, ubuntu-22.04-arm)', + } + jobs = api(f'repos/{repository}/actions/runs/{run_id}/jobs?per_page=100')['jobs'] + for name in required: + matches = [job for job in jobs if job['name'] == name] + if (len(matches) != 1 or matches[0]['status'] != 'completed' or + matches[0]['conclusion'] != 'success'): + raise SystemExit(name + ' must have passed for the identical source') + with output.open('a') as stream: + stream.write('reuse_artifacts=true\n') + print('All source, test, visual, platform and F-Droid gates passed; reusing their immutable artifacts') + PY + - name: Fetch Ziran packages uses: ./.github/actions/ziran-packages with: @@ -81,6 +161,7 @@ jobs: python3 scripts/check-secrets.py --history test: + if: needs.secret-scan.outputs.reuse_artifacts != 'true' name: Test (${{ matrix.shard }}/3) runs-on: ubuntu-latest needs: secret-scan @@ -139,6 +220,7 @@ jobs: visual-test: + if: needs.secret-scan.outputs.reuse_artifacts != 'true' name: Visual Tests runs-on: ubuntu-latest needs: secret-scan @@ -213,6 +295,7 @@ jobs: retention-days: 7 fdroid: + if: needs.secret-scan.outputs.reuse_artifacts != 'true' name: F-Droid needs: secret-scan uses: ./.github/workflows/fdroid.yml @@ -222,18 +305,23 @@ jobs: verify-fdroid: name: Verify F-Droid reproducibility runs-on: ubuntu-latest - needs: [fdroid, build-android] + needs: [secret-scan, fdroid, build-android] + if: needs.secret-scan.outputs.reuse_artifacts != 'true' timeout-minutes: 15 steps: - name: Download independent F-Droid build uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: + github-token: ${{ github.token }} + run-id: ${{ inputs.reuse_run || github.run_id }} name: fdroid-unsigned path: fdroid - name: Download signed Android release uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: + github-token: ${{ github.token }} + run-id: ${{ inputs.reuse_run || github.run_id }} name: release-android path: signed @@ -251,6 +339,7 @@ jobs: apksigcopier compare --unsigned "${signed[0]}" "${rebuilt[0]}" build-web: + if: needs.secret-scan.outputs.reuse_artifacts != 'true' name: Build Web runs-on: ubuntu-latest timeout-minutes: 60 @@ -307,6 +396,7 @@ jobs: if-no-files-found: error build-chrome-extension: + if: needs.secret-scan.outputs.reuse_artifacts != 'true' name: Build Chrome Extension runs-on: ubuntu-latest timeout-minutes: 60 @@ -363,6 +453,7 @@ jobs: if-no-files-found: error build-firefox-addons: + if: needs.secret-scan.outputs.reuse_artifacts != 'true' name: Build Firefox Add-on runs-on: ubuntu-latest timeout-minutes: 60 @@ -420,6 +511,7 @@ jobs: if-no-files-found: error build-linux-appimage: + if: needs.secret-scan.outputs.reuse_artifacts != 'true' name: Build Linux AppImage runs-on: ${{ matrix.runner }} timeout-minutes: 60 @@ -538,6 +630,7 @@ jobs: if-no-files-found: error build-linux-deb: + if: needs.secret-scan.outputs.reuse_artifacts != 'true' name: Build Linux Deb runs-on: ${{ matrix.runner }} timeout-minutes: 60 @@ -599,6 +692,7 @@ jobs: if-no-files-found: error build-linux-rpm: + if: needs.secret-scan.outputs.reuse_artifacts != 'true' name: Build Linux RPM runs-on: ${{ matrix.runner }} timeout-minutes: 60 @@ -660,6 +754,7 @@ jobs: if-no-files-found: error build-linux-snap: + if: needs.secret-scan.outputs.reuse_artifacts != 'true' name: Build Linux Snap runs-on: ${{ matrix.runner }} timeout-minutes: 60 @@ -718,6 +813,7 @@ jobs: if-no-files-found: error build-linux-flatpak: + if: needs.secret-scan.outputs.reuse_artifacts != 'true' name: Build Linux Flatpak runs-on: ${{ matrix.runner }} timeout-minutes: 60 @@ -776,6 +872,7 @@ jobs: if-no-files-found: error build-windows: + if: needs.secret-scan.outputs.reuse_artifacts != 'true' name: Build Windows runs-on: ubuntu-latest timeout-minutes: 60 @@ -841,6 +938,7 @@ jobs: if-no-files-found: error build-android: + if: needs.secret-scan.outputs.reuse_artifacts != 'true' name: Build Android runs-on: ubuntu-latest timeout-minutes: 60 @@ -994,10 +1092,16 @@ jobs: if-no-files-found: error release: + if: >- + always() && !cancelled() && needs.secret-scan.result == 'success' && + (needs.secret-scan.outputs.reuse_artifacts == 'true' || + (!contains(needs.*.result, 'failure') && !contains(needs.*.result, 'cancelled') && + !contains(needs.*.result, 'skipped'))) name: Release runs-on: ubuntu-latest timeout-minutes: 60 needs: + - secret-scan - test - visual-test - build-web @@ -1027,6 +1131,8 @@ jobs: - name: Download Release Artifacts uses: actions/download-artifact@v4 with: + github-token: ${{ github.token }} + run-id: ${{ inputs.reuse_run || github.run_id }} pattern: release-* path: build/release merge-multiple: true @@ -1198,7 +1304,7 @@ jobs: needs: - build-web - release - if: needs.release.outputs.tag_pushed == 'true' + if: always() && !cancelled() && needs.release.result == 'success' && needs.release.outputs.tag_pushed == 'true' env: BUTLER_API_KEY: ${{ secrets.ITCH_API_KEY }} @@ -1219,6 +1325,8 @@ jobs: - name: Download Release Artifacts uses: actions/download-artifact@v4 with: + github-token: ${{ github.token }} + run-id: ${{ inputs.reuse_run || github.run_id }} pattern: release-* path: build/release merge-multiple: true @@ -1297,7 +1405,7 @@ jobs: needs: - build-android - release - if: needs.release.outputs.tag_pushed == 'true' + if: always() && !cancelled() && needs.release.result == 'success' && needs.release.outputs.tag_pushed == 'true' steps: - name: Check Google Play Secret @@ -1313,6 +1421,8 @@ jobs: - name: Download Android Release Artifact uses: actions/download-artifact@v4 with: + github-token: ${{ github.token }} + run-id: ${{ inputs.reuse_run || github.run_id }} name: release-android path: build/release/android @@ -1333,7 +1443,7 @@ jobs: needs: - build-android - release - if: needs.release.outputs.tag_pushed == 'true' + if: always() && !cancelled() && needs.release.result == 'success' && needs.release.outputs.tag_pushed == 'true' env: AMAZON_CLIENT_ID: ${{ secrets.AMAZON_CLIENT_ID }} AMAZON_CLIENT_SECRET: ${{ secrets.AMAZON_CLIENT_SECRET }} @@ -1362,6 +1472,8 @@ jobs: if: steps.amazon_secrets.outputs.configured == 'true' uses: actions/download-artifact@v4 with: + github-token: ${{ github.token }} + run-id: ${{ inputs.reuse_run || github.run_id }} name: release-android path: build/release/android @@ -1389,7 +1501,7 @@ jobs: needs: - build-chrome-extension - release - if: needs.release.outputs.tag_pushed == 'true' + if: always() && !cancelled() && needs.release.result == 'success' && needs.release.outputs.tag_pushed == 'true' steps: - name: Check Chrome Web Store Secrets @@ -1421,6 +1533,8 @@ jobs: if: steps.chrome_secrets.outputs.configured == 'true' uses: actions/download-artifact@v4 with: + github-token: ${{ github.token }} + run-id: ${{ inputs.reuse_run || github.run_id }} name: release-chrome-web-store path: build/release/chrome-web-store @@ -1515,7 +1629,7 @@ jobs: needs: - build-firefox-addons - release - if: needs.release.outputs.tag_pushed == 'true' + if: always() && !cancelled() && needs.release.result == 'success' && needs.release.outputs.tag_pushed == 'true' steps: - name: Check Firefox Add-ons Secrets @@ -1541,6 +1655,8 @@ jobs: if: steps.firefox_secrets.outputs.configured == 'true' uses: actions/download-artifact@v4 with: + github-token: ${{ github.token }} + run-id: ${{ inputs.reuse_run || github.run_id }} name: release-firefox-addons path: build/release/firefox-addons @@ -1576,7 +1692,7 @@ jobs: timeout-minutes: 15 continue-on-error: true needs: release - if: needs.release.outputs.tag_pushed == 'true' + if: always() && !cancelled() && needs.release.result == 'success' && needs.release.outputs.tag_pushed == 'true' env: WINGET_TOKEN: ${{ secrets.WINGET_TOKEN }} diff --git a/docs/cells.md b/docs/cells.md index 74cb169b2..bf56993b0 100644 --- a/docs/cells.md +++ b/docs/cells.md @@ -839,6 +839,13 @@ A release names one fixed commit while peers keep working on master: Subsequent master commits cannot cancel its release. Only the workflow creates the release tag after all required gates pass. +When an earlier Release run already passed every test, visual, platform and +F-Droid reproducibility job for the source, supply its run ID as `reuse_run`. +The workflow admits reuse only when the release commit adds that version's +valid phone-check receipt and changes no other file. It downloads the original +immutable build artifacts for publication and store delivery. A changed source, +unfinished gate or missing result requires the normal complete build. + Harmony Projects shows this wrapper release, every cell version and its Moto screenshots. Harmony's `make inbe-release-verify` checks the latest published APK on the development Moto and writes private evidence under diff --git a/tests/fdroid_build_test.py b/tests/fdroid_build_test.py index cba50973d..040a0da2f 100644 --- a/tests/fdroid_build_test.py +++ b/tests/fdroid_build_test.py @@ -1,6 +1,7 @@ """Guard the isolated source scanner and the pre-publication release gate.""" import json import os +import re from pathlib import Path import subprocess import sys @@ -85,7 +86,8 @@ class FDroidBuildTest(unittest.TestCase): release = workflow.split("\n release:\n", 1)[1].split("\n outputs:", 1)[0] self.assertIn(" - verify-fdroid", release.splitlines()) verifier = workflow.split("\n verify-fdroid:\n", 1)[1].split("\n build-web:\n", 1)[0] - self.assertIn("needs: [fdroid, build-android]", verifier) + dependencies = re.search(r"needs: \[([^]]+)\]", verifier)[1].split(",") + self.assertTrue({"fdroid", "build-android"} <= {value.strip() for value in dependencies}) self.assertIn("apksigcopier compare --unsigned", verifier) self.assertIn("apksigner verify --print-certs", verifier) diff --git a/tests/release_test.py b/tests/release_test.py index ab3ae33a3..6aafe7951 100644 --- a/tests/release_test.py +++ b/tests/release_test.py @@ -1,9 +1,11 @@ """make release keeps one unpushed release commit and checks what CI will build.""" import importlib.util +import ast import contextlib import io import json import os +import re from pathlib import Path import subprocess import tempfile @@ -250,5 +252,150 @@ class MotoWorkflowGateTest(unittest.TestCase): self.check_run(self.passing_jobs(), path=".github/workflows/ci.yml") +class ReleaseArtifactReuseTest(unittest.TestCase): + def setUp(self): + workflow = (ROOT / ".github/workflows/release.yml").read_text() + section = workflow.split(" - name: Verify identical source and every gate before reusing artifacts\n", 1)[1] + script = section.split(" python3 - <<'PY'\n", 1)[1].split("\n PY", 1)[0] + self.script = "\n".join(line[10:] for line in script.splitlines()) + tree = ast.parse(self.script) + self.required = next(ast.literal_eval(node.value) for node in ast.walk(tree) + if isinstance(node, ast.Assign) and any( + isinstance(target, ast.Name) and target.id == "required" + for target in node.targets)) + self.workflow = workflow + + def evaluate(self, *, jobs=None, changed=None, run_changes=None, source_receipt=False, + ancestor=True, phone_passes=True, run_id="123"): + source = "1" * 40 + candidate = "2" * 40 + run = {"path": ".github/workflows/release.yml", "event": "push", "head_branch": "master", + "repository": {"full_name": "waozixyz/inbe"}, "head_sha": source} + run.update(run_changes or {}) + if jobs is None: + jobs = [{"name": name, "status": "completed", "conclusion": "success"} + for name in sorted(self.required)] + if changed is None: + changed = ["releases/phone-checks/3.0.0.json"] + + def read(command, **kwargs): + if command[:2] == ["gh", "api"]: + return json.dumps({"jobs": jobs} if command[-1].endswith("/jobs?per_page=100") else run) + if command[:2] == ["git", "diff"]: + self.assertEqual(command[-2:], [source, candidate]) + return "\n".join(changed) + "\n" + if command == ["python3", "scripts/check-version.py", "--print-version"]: + return "3.0.0\n" + self.fail("Unexpected read: " + repr(command)) + + def check(command, **kwargs): + if command[:2] == ["git", "merge-base"]: + self.assertEqual(command[-2:], [source, candidate]) + if not ancestor: + raise subprocess.CalledProcessError(1, command) + elif command[:2] == ["git", "cat-file"]: + self.assertEqual(command[-1], source + ":releases/phone-checks/3.0.0.json") + return subprocess.CompletedProcess(command, 0 if source_receipt else 1) + elif command[:2] == ["python3", "scripts/check-phone-check.py"]: + self.assertEqual(command[-2:], ["--revision", candidate]) + if not phone_passes: + raise subprocess.CalledProcessError(1, command) + else: + self.fail("Unexpected command: " + repr(command)) + return subprocess.CompletedProcess(command, 0) + + with tempfile.TemporaryDirectory(prefix="inbe-reuse-gate-") as temporary: + output = Path(temporary) / "output" + environment = {"VERIFIED_RUN": run_id, "CANDIDATE": candidate, + "GITHUB_REPOSITORY": "waozixyz/inbe", "GITHUB_OUTPUT": str(output)} + with mock.patch.dict(os.environ, environment), \ + mock.patch("subprocess.check_output", side_effect=read) as reads, \ + mock.patch("subprocess.run", side_effect=check) as commands, \ + contextlib.redirect_stdout(io.StringIO()): + try: + exec(compile(self.script, "release artifact reuse", "exec"), {}) + except SystemExit as error: + if error.code != 0: + raise + if not run_id: + reads.assert_not_called() + commands.assert_not_called() + return output.read_text() + + def test_receipt_only_candidate_reuses_every_passing_platform(self): + self.assertEqual(self.evaluate(), "reuse_artifacts=true\n") + + def test_normal_build_does_not_need_a_previous_run(self): + self.assertEqual(self.evaluate(run_id=""), "reuse_artifacts=false\n") + + def test_missing_failed_skipped_cancelled_or_duplicate_gates_are_rejected(self): + jobs = [{"name": name, "status": "completed", "conclusion": "success"} + for name in sorted(self.required)] + for index in range(len(jobs)): + with self.subTest(job=jobs[index]["name"], state="missing"), self.assertRaises(SystemExit): + self.evaluate(jobs=jobs[:index] + jobs[index + 1:]) + for state in ("failure", "cancelled", "skipped", None): + damaged = [dict(job) for job in jobs] + damaged[index]["conclusion"] = state + with self.subTest(job=jobs[index]["name"], state=state), self.assertRaises(SystemExit): + self.evaluate(jobs=damaged) + with self.subTest(job=jobs[index]["name"], state="duplicate"), self.assertRaises(SystemExit): + self.evaluate(jobs=jobs + [jobs[index]]) + damaged = [dict(job) for job in jobs] + damaged[index]["status"] = "in_progress" + with self.subTest(job=jobs[index]["name"], state="unfinished"), self.assertRaises(SystemExit): + self.evaluate(jobs=damaged) + + def test_changed_application_tests_tooling_or_unrelated_source_are_rejected(self): + for extra in ("src/main.zi", "ziran.lock", "droid/app/build.gradle", "tests/new_test.py", + ".github/workflows/release.yml", "CHANGELOG.md", "docs/notes.md"): + with self.subTest(path=extra), self.assertRaises(SystemExit): + self.evaluate(changed=["releases/phone-checks/3.0.0.json", extra]) + for changed in ([], ["releases/phone-checks/2.9.9.json"]): + with self.subTest(changed=changed), self.assertRaises(SystemExit): + self.evaluate(changed=changed) + + def test_wrong_repository_workflow_event_branch_or_run_is_rejected(self): + for change in ({"repository": {"full_name": "elsewhere/inbe"}}, + {"path": ".github/workflows/ci.yml"}, {"event": "workflow_dispatch"}, + {"head_branch": "other"}, {"head_sha": "master"}): + with self.subTest(change=change), self.assertRaises(SystemExit): + self.evaluate(run_changes=change) + for run_id in ("0", "123;echo", "master"): + with self.subTest(run_id=run_id), self.assertRaises(SystemExit): + self.evaluate(run_id=run_id) + + def test_invalid_phone_result_unrelated_history_and_existing_receipt_are_rejected(self): + with self.assertRaises(subprocess.CalledProcessError): + self.evaluate(phone_passes=False) + with self.assertRaises(subprocess.CalledProcessError): + self.evaluate(ancestor=False) + with self.assertRaises(SystemExit): + self.evaluate(source_receipt=True) + + def test_reuse_policy_covers_every_declared_build_and_reproducibility_gate(self): + builds = set() + for identifier, body in re.findall(r"^ (build-[a-z-]+):\n(.*?)(?=^ [a-z][a-z-]*:|\Z)", + self.workflow, re.M | re.S): + name = re.search(r"^ name: (.+)$", body, re.M)[1] + matrix = re.findall(r"^\s+- arch: (.+)\n\s+runner: (.+)(?:\n\s+tool_arch: (.+))?", body, re.M) + if matrix: + builds.update(name + " (" + ", ".join(value for value in row if value) + ")" for row in matrix) + else: + builds.add(name) + self.assertIn("if: needs.secret-scan.outputs.reuse_artifacts != 'true'", body) + self.assertEqual({name for name in self.required if name.startswith("Build ")}, builds) + self.assertTrue({"Secret Scan", "Visual Tests", "Test (0/3)", "Test (1/3)", "Test (2/3)", + "F-Droid / F-Droid security and clean build", "Verify F-Droid reproducibility"} + <= self.required) + release_job = self.workflow.split("\n release:\n", 1)[1].split("\n notify-waozi-site:", 1)[0] + for state in ("failure", "cancelled", "skipped"): + self.assertIn("!contains(needs.*.result, '" + state + "')", release_job) + for download in self.workflow.split("uses: actions/download-artifact@")[1:]: + settings = download.split("\n - name:", 1)[0] + self.assertIn("run-id: ${{ inputs.reuse_run || github.run_id }}", settings) + self.assertIn("github-token: ${{ github.token }}", settings) + + if __name__ == "__main__": unittest.main()