diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d2a57fc18..b405aa991 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,35 +29,60 @@ concurrency: cancel-in-progress: ${{ github.event_name == 'push' && github.ref != 'refs/heads/main' }} # A slash-command dispatch supplies a fork repository and immutable commit SHA. -# Normal push runs use the upstream repository and pushed ref. +# Only fork-checks checks out a foreign repository. Every other job needs a +# shared runner, passwordless sudo and registry credentials, so it is skipped +# when the dispatch names a fork and otherwise checks out the dispatched commit +# in this repository (or the pushed ref for a push run), never plain main. env: TEST_PR_HEAD_REPO: ${{ inputs.pr_head_repo || github.repository }} - TEST_SOURCE_REPO: ${{ inputs.pr_head_repo || github.repository }} - TEST_SOURCE_REF: ${{ inputs.pr_head_sha || github.ref }} permissions: contents: read jobs: + # Untrusted fork commits run only here: a disposable hosted runner with no + # secrets, no sudo and no registry login. Nothing it builds is reused. + fork-checks: + if: ${{ github.event_name == 'workflow_dispatch' && inputs.pr_head_repo != '' && inputs.pr_head_repo != github.repository }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4.4.0 + with: + repository: ${{ inputs.pr_head_repo }} + ref: ${{ inputs.pr_head_sha }} + persist-credentials: false + - uses: actions/setup-go@v6.5.0 + with: + go-version-file: go.mod + cache: false + - name: Build + run: make build + test: + # Fork code must never reach a runner that has passwordless sudo. + if: ${{ github.event_name != 'workflow_dispatch' || inputs.pr_head_repo == '' || inputs.pr_head_repo == github.repository }} runs-on: [self-hosted, linux, x64, kvm] + outputs: + tested_sha: ${{ steps.tested-sha.outputs.sha }} steps: - uses: actions/checkout@v4.4.0 with: - clean: false - repository: ${{ env.TEST_SOURCE_REPO }} - ref: ${{ env.TEST_SOURCE_REF }} + # Test the dispatched PR commit; otherwise the commit that triggered the run. + ref: ${{ inputs.pr_head_sha || github.sha }} persist-credentials: false - - name: Clean workspace (preserve binary caches) + - name: Record tested commit + id: tested-sha + run: echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" + + # Checkout already resets the tree; this second pass exists because a + # previous run on this shared runner may have left anything behind, and + # nothing untracked (bin/, downloaded binaries) is trusted across runs. + - name: Clean workspace run: | git reset --hard HEAD - git clean -ffdx \ - -e lib/vmm/binaries/ \ - -e lib/hypervisor/firecracker/binaries/ \ - -e lib/ingress/binaries/ \ - -e bin/ - + git clean -ffdx + # Tests create a bridge and route per VM and clean up on success, but a # run that dies mid-test (crash, timeout, superseded-run cancellation) # leaks them, and enough leaked routes fail new tests on subnet @@ -284,7 +309,7 @@ jobs: report-pr-test: if: ${{ always() && github.event_name == 'workflow_dispatch' && inputs.pr_number != '' }} - needs: test + needs: [test, fork-checks] runs-on: ubuntu-latest permissions: contents: read @@ -298,11 +323,21 @@ jobs: PR_NUMBER: ${{ inputs.pr_number }} EXPECTED_REPO: ${{ inputs.pr_head_repo }} EXPECTED_SHA: ${{ inputs.pr_head_sha }} - TEST_RESULT: ${{ needs.test.result }} + # The commit the privileged job actually checked out, so a run that + # fell back to main's code can never be reported as the PR's result. + TESTED_SHA: ${{ needs.test.outputs.tested_sha }} + # Fork dispatches skip the privileged jobs; report what actually ran. + TEST_RESULT: ${{ needs.test.result == 'skipped' && needs.fork-checks.result || needs.test.result }} + FORK_ONLY: ${{ needs.test.result == 'skipped' }} run: | set -euo pipefail [[ "$PR_NUMBER" =~ ^[0-9]+$ ]] [[ "$EXPECTED_SHA" =~ ^[0-9a-f]{40}$ ]] + # Fork-checks fetches EXPECTED_SHA itself; a privileged run must show it did. + if [[ "$FORK_ONLY" != "true" && "$TESTED_SHA" != "$EXPECTED_SHA" ]]; then + echo "::notice::Skipping test status because the run tested ${TESTED_SHA:-nothing}, not $EXPECTED_SHA" + exit 0 + fi pr="$(gh api "repos/$REPO/pulls/$PR_NUMBER" --jq '[.state, .base.ref, .head.repo.full_name, .head.sha] | @tsv')" read -r state base_ref head_repo head_sha <<< "$pr" if [[ "$state" != open || "$base_ref" != main || "$head_repo" != "$EXPECTED_REPO" || "$head_sha" != "$EXPECTED_SHA" ]]; then @@ -323,6 +358,8 @@ jobs: --field target_url="$GITHUB_SERVER_URL/$REPO/actions/runs/$GITHUB_RUN_ID" test-darwin: + # Same rule as test: a macOS runner is shared and holds registry credentials. + if: ${{ github.event_name != 'workflow_dispatch' || inputs.pr_head_repo == '' || inputs.pr_head_repo == github.repository }} runs-on: [self-hosted, macos, arm64] concurrency: group: macos-ci-test-${{ inputs.pr_head_sha || github.ref }} @@ -332,8 +369,8 @@ jobs: steps: - uses: actions/checkout@v4.4.0 with: - repository: ${{ env.TEST_SOURCE_REPO }} - ref: ${{ env.TEST_SOURCE_REF }} + # Same commit as test: the dispatched PR commit, else the triggering commit. + ref: ${{ inputs.pr_head_sha || github.sha }} persist-credentials: false - uses: actions/setup-go@v6.5.0 with: @@ -438,6 +475,8 @@ jobs: make clean e2e-install: + # Same rule as test: a macOS runner is shared and holds registry credentials. + if: ${{ github.event_name != 'workflow_dispatch' || inputs.pr_head_repo == '' || inputs.pr_head_repo == github.repository }} runs-on: [self-hosted, macos, arm64] needs: test-darwin concurrency: @@ -446,8 +485,8 @@ jobs: steps: - uses: actions/checkout@v4.4.0 with: - repository: ${{ env.TEST_SOURCE_REPO }} - ref: ${{ env.TEST_SOURCE_REF }} + # Same commit as test: the dispatched PR commit, else the triggering commit. + ref: ${{ inputs.pr_head_sha || github.sha }} persist-credentials: false - uses: actions/setup-go@v6.5.0 with: