diff --git a/.github/workflows/play-publish.yml b/.github/workflows/play-publish.yml new file mode 100644 index 00000000..db4a6e9d --- /dev/null +++ b/.github/workflows/play-publish.yml @@ -0,0 +1,128 @@ +name: Play publish + +# Puts a tagged release on Google Play: Internal, External (closed testing, +# Play's `alpha`) or production (#1053, #1088). The first run for a tag uploads +# its bundle; later runs move that same bundle to another track, so every track +# gets the same bytes and no new version code. +# +# Run it from the tag: Actions > Play publish > Run workflow, "Use workflow +# from" the v tag, then pick target and rollout. The owner approves play-release. +# +# SECURITY: keyless; no Play credential exists anywhere. Google accepts only +# this workflow, at a v* tag, in the play-release environment, from this repo +# (#1085). `verify` holds no Google access and runs before the approval, so a +# bad tag or bundle is refused without asking anyone. NEVER add a pull_request +# or push trigger. + +on: + workflow_dispatch: + inputs: + target: + description: "Where it goes: internal, external (closed testing) or production" + type: choice + required: true + options: [internal, external, production] + rollout: + description: "draft, or a percentage. Internal and External take draft or 100." + type: choice + required: true + options: ["draft", "10", "25", "50", "100"] + +concurrency: + group: play-publish + cancel-in-progress: false + +permissions: + contents: read + +jobs: + verify: + runs-on: ubuntu-latest + permissions: + contents: read + attestations: read + outputs: + aab_sha256: ${{ steps.bundle.outputs.sha256 }} + steps: + - name: Refuse anything but a v tag + run: | + case "$GITHUB_REF" in + refs/tags/v*) ;; + *) echo "::error::Run this from a v* tag, not $GITHUB_REF"; exit 1 ;; + esac + + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Refuse a tag whose commit isn't on main + run: | + git fetch --no-tags origin +refs/heads/main:refs/remotes/origin/main + if ! git merge-base --is-ancestor "$GITHUB_SHA" origin/main; then + echo "::error::$GITHUB_REF_NAME ($GITHUB_SHA) is not on main" + exit 1 + fi + + - name: Fetch the bundle from the tag's release + env: + GH_TOKEN: ${{ github.token }} + run: gh release download "$GITHUB_REF_NAME" --repo "$GITHUB_REPOSITORY" --pattern app-release.aab + + - name: Refuse a bundle the signed run at this tag didn't build + env: + GH_TOKEN: ${{ github.token }} + run: | + gh attestation verify app-release.aab \ + --repo "$GITHUB_REPOSITORY" \ + --signer-workflow "$GITHUB_REPOSITORY/.github/workflows/release-signed.yml" \ + --source-ref "$GITHUB_REF" \ + --source-digest "$GITHUB_SHA" \ + --deny-self-hosted-runners + + - id: bundle + run: echo "sha256=$(sha256sum app-release.aab | cut -d' ' -f1)" >> "$GITHUB_OUTPUT" + + publish: + needs: verify + runs-on: ubuntu-latest + environment: play-release # required reviewer, v* tags only; do not remove + permissions: + contents: read + id-token: write + steps: + - uses: actions/checkout@v4 + + - name: Fetch the same bundle verify checked + env: + GH_TOKEN: ${{ github.token }} + EXPECTED: ${{ needs.verify.outputs.aab_sha256 }} + run: | + gh release download "$GITHUB_REF_NAME" --repo "$GITHUB_REPOSITORY" --pattern app-release.aab + ACTUAL=$(sha256sum app-release.aab | cut -d' ' -f1) + if [ -z "$EXPECTED" ] || [ "$ACTUAL" != "$EXPECTED" ]; then + echo "::error::The release's bundle changed after verify: $EXPECTED then, $ACTUAL now" + exit 1 + fi + + - id: google + uses: google-github-actions/auth@v2 + with: + workload_identity_provider: projects/166380075966/locations/global/workloadIdentityPools/github/providers/github-actions + service_account: play-publisher@offband-reporting.iam.gserviceaccount.com + token_format: access_token + access_token_scopes: https://www.googleapis.com/auth/androidpublisher + create_credentials_file: false + + - name: Publish + env: + PLAY_ACCESS_TOKEN: ${{ steps.google.outputs.access_token }} + TARGET: ${{ inputs.target }} + ROLLOUT: ${{ inputs.rollout }} + run: >- + python3 tools/play/play_release.py publish --aab app-release.aab + --tag "$GITHUB_REF_NAME" --target "$TARGET" --rollout "$ROLLOUT" + + - name: Tracks after + env: + PLAY_ACCESS_TOKEN: ${{ steps.google.outputs.access_token }} + run: python3 tools/play/play_release.py tracks diff --git a/tools/play/test_play_release.py b/tools/play/test_play_release.py index 72dc4722..7a7abff5 100644 --- a/tools/play/test_play_release.py +++ b/tools/play/test_play_release.py @@ -2,7 +2,7 @@ Each refusal in PLAN.md section 6 that the tool owns has a test here. The workflow-level rows (identity condition, tag on main, release asset present, -provenance) belong to play-publish.yml and are tested there (#1087, #1088). +provenance) belong to play-publish.yml; test_workflows.py guards them (#1087, #1088). """ from __future__ import annotations diff --git a/tools/play/test_workflows.py b/tools/play/test_workflows.py new file mode 100644 index 00000000..5458b0c8 --- /dev/null +++ b/tools/play/test_workflows.py @@ -0,0 +1,97 @@ +"""Guards on the two workflows that put a release on Play (#1087, #1088). + +The refusals themselves were proven live in A3 (#1086): Google refuses a +sign-in from outside play-release, GitHub refuses a branch asking for it, and +`gh attestation verify` refuses a wrong ref, a wrong signer and an unrecorded +file. These tests keep the workflows wired to those checks. +""" + +from __future__ import annotations + +import re +import unittest +from pathlib import Path + +import play_release as p + +WORKFLOWS = Path(__file__).resolve().parents[2] / ".github/workflows" + + +def jobs(text: str) -> dict[str, str]: + body = text.split("\njobs:\n", 1)[1] + parts = re.split(r"^ ([a-z][a-z0-9-]*):\n", body, flags=re.M) + return dict(zip(parts[1::2], parts[2::2])) + + +class ReleaseSigned(unittest.TestCase): + def setUp(self): + self.android = jobs((WORKFLOWS / "release-signed.yml").read_text("utf-8"))["android"] + + def test_android_job_records_the_aab_provenance(self): + self.assertRegex( + self.android, + r"uses: actions/attest-build-provenance@v\d+\s+with:\s+" + r"subject-path: build/app/outputs/bundle/release/app-release\.aab", + ) + + def test_android_job_can_write_the_record(self): + for perm in ("contents: read", "id-token: write", "attestations: write"): + self.assertIn(perm, self.android) + + +class PlayPublish(unittest.TestCase): + def setUp(self): + self.text = (WORKFLOWS / "play-publish.yml").read_text("utf-8") + self.jobs = jobs(self.text) + self.trigger = self.text.split("\non:\n", 1)[1].split("\nconcurrency:", 1)[0] + + def test_dispatch_is_the_only_trigger(self): + self.assertIn("workflow_dispatch:", self.trigger) + for other in ("pull_request", "push:", "schedule", "workflow_run", "release:"): + self.assertNotIn(other, self.trigger) + + def test_targets_and_rollouts_match_the_tool(self): + target = re.search(r"target:.*?options: \[(.*?)\]", self.trigger, re.S).group(1) + rollout = re.search(r"rollout:.*?options: \[(.*?)\]", self.trigger, re.S).group(1) + self.assertEqual(sorted(re.findall(r"[a-z]+", target)), sorted(p.TRACKS)) + self.assertEqual(re.findall(r'"(\w+)"', rollout), list(p.ROLLOUTS)) + + def test_one_queue_for_the_app_never_cancelled(self): + self.assertRegex(self.text, r"concurrency:\s+group: play-publish\s+cancel-in-progress: false") + + def test_only_the_publish_job_can_get_a_google_token(self): + self.assertEqual(set(self.jobs), {"verify", "publish"}) + self.assertNotIn("id-token", self.jobs["verify"]) + self.assertNotIn("environment:", self.jobs["verify"]) + self.assertIn("environment: play-release", self.jobs["publish"]) + self.assertIn("id-token: write", self.jobs["publish"]) + self.assertIn("needs: verify", self.jobs["publish"]) + + def test_verify_refuses_anything_but_a_v_tag_on_main(self): + verify = self.jobs["verify"] + self.assertIn('refs/tags/v*)', verify) + self.assertIn('git merge-base --is-ancestor "$GITHUB_SHA" origin/main', verify) + + def test_verify_checks_the_bundle_came_from_the_signed_run_at_this_tag(self): + verify = self.jobs["verify"] + self.assertIn("gh attestation verify", verify) + self.assertIn('--signer-workflow "$GITHUB_REPOSITORY/.github/workflows/release-signed.yml"', verify) + self.assertIn('--source-ref "$GITHUB_REF"', verify) + self.assertIn('--source-digest "$GITHUB_SHA"', verify) + self.assertIn("--deny-self-hosted-runners", verify) + + def test_publish_sends_only_the_bundle_verify_checked(self): + self.assertIn("aab_sha256", self.jobs["verify"]) + self.assertIn("needs.verify.outputs.aab_sha256", self.jobs["publish"]) + + def test_google_token_is_play_scoped_and_never_a_file(self): + publish = self.jobs["publish"] + self.assertIn("uses: google-github-actions/auth@", publish) + self.assertIn("token_format: access_token", publish) + self.assertIn("access_token_scopes: https://www.googleapis.com/auth/androidpublisher", publish) + self.assertIn("create_credentials_file: false", publish) + self.assertNotIn("credentials_json", self.text) + + +if __name__ == "__main__": + unittest.main()