diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fb923dca9..e0f4889ad 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -80,6 +80,10 @@ jobs: permissions: contents: read packages: write + outputs: + # Whether the flavored builds can pull the cache image. False on a fork + # pull request, where the kernel is compiled but cannot be published. + available: ${{ steps.cache.outputs.available }} steps: - name: Checkout uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5 @@ -91,20 +95,46 @@ jobs: - name: Log in to GHCR env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: echo "$GITHUB_TOKEN" | podman login ghcr.io -u "${{ github.actor }}" --password-stdin + run: | + # Anonymous pulls are enough to check whether the tag is published; + # only the push needs credentials, and a fork PR has none to give. + echo "$GITHUB_TOKEN" | podman login ghcr.io -u "${{ github.actor }}" --password-stdin \ + || echo "::warning title=ghcr login failed::continuing; a cache hit can still be read anonymously" - name: Build the kernel cache image if it is not published yet + id: cache env: REPO_ORGANIZATION: ${{ github.repository_owner }} + # Empty head repository means this is not a pull request at all, so + # the comparison holds for pushes, schedules and dispatches too. + CAN_PUBLISH: ${{ github.event.pull_request.head.repo.full_name == '' + || github.event.pull_request.head.repo.full_name == github.repository }} run: | set -euo pipefail ref="ghcr.io/${REPO_ORGANIZATION,,}/utah-kernel-cache:$(./scripts/kernel-cache-tag.sh)" if podman pull -q "$ref" >/dev/null 2>&1; then echo "Cache hit: $ref is already published" + echo "available=true" >> "$GITHUB_OUTPUT" exit 0 fi echo "Cache miss: building $ref" podman build --tag "$ref" --file Containerfile.kernel . + # A pull request from a fork gets a read-only GITHUB_TOKEN, so the + # push fails no matter how well the kernel built: + # + # denied: installation not allowed to Write organization package + # + # That is what #154, #157 and #158 have been red for -- all three + # compiled the kernel and died publishing it, twenty minutes in, on a + # permission no contributor can be granted from a fork. The compile is + # the part that validates their change, so run it and say plainly that + # the flavored images cannot be built from here. + if [ "${CAN_PUBLISH}" != "true" ]; then + echo "::notice title=kernel cache not published::The kernel compiled, but a fork pull request cannot write to ghcr.io/${REPO_ORGANIZATION,,}. The flavored images are not built for this PR; push the branch to projectbluefin/utah to validate them." + echo "available=false" >> "$GITHUB_OUTPUT" + exit 0 + fi podman push "$ref" + echo "available=true" >> "$GITHUB_OUTPUT" # main starts the moment the contract check passes. It never pulls the kernel # cache, so there is no reason for it to wait on that job; before this split @@ -157,7 +187,7 @@ jobs: # digest artifact, which post-testing-e2e matches by glob. build_kernel: needs: [contract, kernel_cache] - if: ${{ !cancelled() && needs.contract.result == 'success' && needs.kernel_cache.result == 'success' && needs.contract.outputs.kernel_flavors != '[]' }} + if: ${{ !cancelled() && needs.contract.result == 'success' && needs.kernel_cache.result == 'success' && needs.kernel_cache.outputs.available == 'true' && needs.contract.outputs.kernel_flavors != '[]' }} uses: projectbluefin/actions/.github/workflows/reusable-build.yml@8895d09342174927b950abc75dccd6da2c8ba36f # v1 permissions: contents: read diff --git a/tests/test_iso_ci.py b/tests/test_iso_ci.py index 314b16619..ae01fc097 100644 --- a/tests/test_iso_ci.py +++ b/tests/test_iso_ci.py @@ -227,3 +227,54 @@ def test_harness_delegates_the_decision_and_prints_the_transcript_on_failure(sel self.assertIn("fastfetch-ocr-match.sh", script) self.assertNotIn("grep -qi 'UTAH.E2E.FASTFETCH'", script) self.assertIn("last OCR transcript", script) + + +class ForkPullRequestKernelCacheTests(unittest.TestCase): + """A fork PR cannot publish the kernel cache, and must not go red for it. + + #154, #157 and #158 were all red on the same thing: the kernel compiled and + the push then failed with "denied: installation not allowed to Write + organization package", twenty minutes in, on a permission no contributor can + be granted from a fork. Three PRs blocked by CI plumbing rather than by + anything in their diffs. + """ + + WORKFLOW = ROOT / ".github/workflows/build.yml" + + def setUp(self): + import yaml + self.text = self.WORKFLOW.read_text() + self.jobs = yaml.safe_load(self.text)["jobs"] + + def test_the_push_is_conditional_but_the_build_is_not(self): + step = next(s for s in self.jobs["kernel_cache"]["steps"] + if s.get("id") == "cache") + run = step["run"] + # The compile is what validates a kernel change, so it always runs. + self.assertIn("podman build --tag", run) + build = run.index("podman build --tag") + guard = run.index('if [ "${CAN_PUBLISH}" != "true" ]') + push = run.index("podman push") + self.assertLess(build, guard, "the build must not be behind the guard") + self.assertLess(guard, push, "the push must be behind the guard") + + def test_can_publish_is_true_for_everything_that_is_not_a_fork_pr(self): + step = next(s for s in self.jobs["kernel_cache"]["steps"] + if s.get("id") == "cache") + env = step["env"]["CAN_PUBLISH"] + # An empty head repository is a push, a schedule or a dispatch. + self.assertIn("github.event.pull_request.head.repo.full_name == ''", env) + self.assertIn("== github.repository", env) + + def test_the_flavored_builds_are_skipped_rather_than_left_to_fail(self): + # Without this they would try to pull an image that was never pushed. + self.assertIn("needs.kernel_cache.outputs.available == 'true'", + self.jobs["build_kernel"]["if"]) + self.assertEqual(self.jobs["kernel_cache"]["outputs"]["available"], + "${{ steps.cache.outputs.available }}") + + def test_a_cache_hit_still_reports_the_image_as_available(self): + step = next(s for s in self.jobs["kernel_cache"]["steps"] + if s.get("id") == "cache") + hit = step["run"].index("Cache hit") + self.assertIn("available=true", step["run"][hit:hit + 200])