diff --git a/.github/workflows/gitlab-e2e.yml b/.github/workflows/gitlab-e2e.yml index 2f73f31..85c3443 100644 --- a/.github/workflows/gitlab-e2e.yml +++ b/.github/workflows/gitlab-e2e.yml @@ -1,10 +1,7 @@ name: GitLab E2E -run-name: "GitLab E2E: ${{ github.event.pull_request.head.sha || inputs.setup_ref || github.sha }} / ${{ inputs.suite || 'full' }}" +run-name: "GitLab E2E: ${{ inputs.setup_ref || github.sha }} / ${{ inputs.suite || 'full' }}" on: - pull_request: - branches: [main] - types: [labeled] workflow_dispatch: inputs: pull_request: @@ -32,50 +29,23 @@ permissions: jobs: gitlab-e2e: name: GitLab E2E - if: >- - (github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main') || - (github.event_name == 'pull_request' && github.event.action == 'labeled' && - github.event.label.name == 'run-e2e' && - github.event.pull_request.head.repo.full_name == github.repository) + if: github.ref == 'refs/heads/main' + environment: gitlab-e2e runs-on: ubuntu-latest timeout-minutes: 35 - # Fork PRs use manual dispatch on main. Never check out or execute PR files. + # Dispatch on main with the reviewed SHA. Keep PR code off this runner. steps: - name: Resolve test parameters id: parameters env: - EVENT_NAME: ${{ github.event_name }} - EVENT_ACTION: ${{ github.event.action }} - EVENT_LABEL: ${{ github.event.label.name }} - LABEL_ACTOR: ${{ github.event.sender.login }} - PR_NUMBER: ${{ github.event.pull_request.number || inputs.pull_request }} - SETUP_REF: ${{ github.event.pull_request.head.sha || inputs.setup_ref }} + PR_NUMBER: ${{ inputs.pull_request }} + SETUP_REF: ${{ inputs.setup_ref }} TEST_SUITE: ${{ inputs.suite }} VITE_PLUS_VERSION: ${{ inputs.vite_plus_version }} GH_TOKEN: ${{ github.token }} run: | set -euo pipefail - case "$EVENT_NAME" in - workflow_dispatch) ;; - pull_request) - if [ "$EVENT_ACTION" != labeled ] || [ "$EVENT_LABEL" != run-e2e ] || - ! [[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ && "$SETUP_REF" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::GitLab E2E requires a run-e2e label event with an exact PR head SHA." - exit 1 - fi - # Check the original labeler, including when someone reruns this job. - permission="$(gh api "repos/${GITHUB_REPOSITORY}/collaborators/${LABEL_ACTOR}/permission" --jq '.permission')" - case "$permission" in - admin | write) ;; - *) echo "::error::Adding run-e2e requires repository write access."; exit 1 ;; - esac - TEST_SUITE=full - VITE_PLUS_VERSION=latest - ;; - *) echo "::error::Unsupported GitLab E2E event."; exit 1 ;; - esac - setup_vp_ref="${SETUP_REF:-$GITHUB_SHA}" suite="${TEST_SUITE:-full}" vite_plus_version="${VITE_PLUS_VERSION:-latest}" @@ -106,12 +76,6 @@ jobs: echo "::error::The PR changed or no longer targets main. Review its current head and start a new run." exit 1 fi - if [ "$EVENT_NAME" = pull_request ] && ! jq -e --arg repo "$GITHUB_REPOSITORY" \ - '.head.repo.full_name == $repo and any(.labels[]; .name == "run-e2e")' \ - <<< "$pull_request" > /dev/null; then - echo "::error::Label requests require a same-repository PR with run-e2e. Use manual dispatch on main for forks." - exit 1 - fi fi { @@ -124,7 +88,7 @@ jobs: id: trigger env: GITLAB_PROJECT_ID: "85578524" - GITLAB_TRIGGER_TOKEN: ${{ secrets.GITLAB_TRIGGER_TOKEN }} + GITLAB_TRIGGER_TOKEN: ${{ secrets.SETUP_VP_GITLAB_TRIGGER_TOKEN }} SETUP_VP_REF: ${{ steps.parameters.outputs.setup_vp_ref }} TEST_SUITE: ${{ steps.parameters.outputs.suite }} VITE_PLUS_VERSION: ${{ steps.parameters.outputs.vite_plus_version }} @@ -132,7 +96,7 @@ jobs: set -euo pipefail if [ -z "$GITLAB_TRIGGER_TOKEN" ]; then - echo "::error::This repository does not have the GITLAB_TRIGGER_TOKEN secret." + echo "::error::Set SETUP_VP_GITLAB_TRIGGER_TOKEN in the protected gitlab-e2e environment." exit 1 fi diff --git a/.github/workflows/publish-bundle.yml b/.github/workflows/publish-bundle.yml index e52cc32..858109e 100644 --- a/.github/workflows/publish-bundle.yml +++ b/.github/workflows/publish-bundle.yml @@ -15,6 +15,7 @@ permissions: {} jobs: publish: if: github.ref == 'refs/heads/main' + environment: bundle-publish runs-on: ubuntu-latest timeout-minutes: 5 permissions: @@ -175,8 +176,8 @@ jobs: if: steps.prepare.outputs.changed == 'true' id: app-token with: - client-id: ${{ secrets.APP_ID }} - private-key: ${{ secrets.APP_PRIVATE_KEY }} + client-id: ${{ vars.SETUP_VP_BUNDLE_APP_CLIENT_ID }} + private-key: ${{ secrets.SETUP_VP_BUNDLE_APP_PRIVATE_KEY }} repositories: ${{ github.event.repository.name }} permission-contents: write diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f1bedc2..6697622 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -70,23 +70,24 @@ Installers that support this protocol resolve an explicit environment override o The temporary result is cleared before each installer attempt and removed after installation. Older installers do not write a result, so setup-vp retains the original selector and existing behavior. Updating setup-vp alone does not enable runtime reuse for an older Vite+ release. -## GitLab End-to-End Tests +## Protected Workflow Credentials -Use the dedicated [GitLab test project](https://gitlab.com/fengmk2/setup-vp-gitlab-test) to test the remote integration. The [GitLab E2E workflow](.github/workflows/gitlab-e2e.yml) accepts `run-e2e` label requests for same-repository PRs and manual `workflow_dispatch` requests on `main`. The job uses the GitLab trigger secret without checking out or executing PR files on the GitHub runner. +Create two [GitHub Actions environments](https://docs.github.com/en/actions/how-tos/deploy/configure-and-manage-deployments/manage-environments) before enabling the credentialed workflows below. In each environment, allow deployments only from the `main` branch. Use required reviewers and prevent self-review to require a separate approval for credential access. -### Request a Run +| Environment | Configuration | +| ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `gitlab-e2e` | Set the environment secret `SETUP_VP_GITLAB_TRIGGER_TOKEN` to a dedicated GitLab test project trigger token. | +| `bundle-publish` | Set the environment variable `SETUP_VP_BUNDLE_APP_CLIENT_ID` and the environment secret `SETUP_VP_BUNDLE_APP_PRIVATE_KEY`. Use a dedicated App with `contents: write` installed only on `setup-vp`. | -For a same-repository PR, review its current head commit, including workflow changes, then add `run-e2e`: +Keep these credentials at environment scope. During migration, remove the old repository secret `GITLAB_TRIGGER_TOKEN` and remove this repository's access to the old organization secret `APP_PRIVATE_KEY`. Rotate credentials that previously had broader access. -```bash -gh pr edit 123 --add-label run-e2e -``` +## GitLab End-to-End Tests -The workflow checks that the labeler has repository write access and runs the full suite at the label event's head SHA. After each new commit, review the changes, then remove and re-add `run-e2e`. Keeping the label on a PR does not approve later pushes. The handler rejects stale heads, closed PRs, and requests whose label was removed. +Use the dedicated [GitLab test project](https://gitlab.com/fengmk2/setup-vp-gitlab-test) to test the remote integration. Start the [GitLab E2E workflow](.github/workflows/gitlab-e2e.yml) with `workflow_dispatch` on `main`. The protected `gitlab-e2e` environment supplies the trigger credential. The GitHub job does not check out or execute PR files. -### Manual Runs and Fork PRs +### Request a Run -Fork PRs use manual dispatch because `pull_request` workflows do not receive the GitLab secret from the base repository. For a manual PR run, set the PR number and record its current head SHA. Review that commit before starting the workflow: +For a manual PR run, set the PR number and record its current head SHA. Review that commit before starting the workflow: ```bash pr_number=123 @@ -102,7 +103,7 @@ gh workflow run gitlab-e2e.yml --ref main \ -f suite=full ``` -Manual dispatch works for same-repository and fork PRs. The workflow rejects a request if the PR head changes before validation. Each new commit needs another review and request. For both label and manual requests, check the workflow's logs and job summary for the tested commit, suite, pipeline link, and result. The workflow does not write PR comments. +Manual dispatch works for same-repository and fork PRs. The workflow rejects a request if the PR head changes before validation. Each new commit needs another review and request. Check the workflow's logs and job summary for the tested commit, suite, pipeline link, and result. The workflow does not write PR comments. For a standalone run, omit `pull_request`. Set `setup_ref` to an exact commit SHA or release tag, or leave it empty to test the workflow commit. Select `suite` (`full` by default) and `vite_plus_version` (`latest` by default). Manual runs selected on branches other than `main` skip the job. @@ -136,7 +137,7 @@ Publish releases as Git tags, not as an npm package. Keep `package.json.version` Run the [required checks](#before-committing) and commit the release changes, including any updated bundles. The bootstrap and template tests compare these defaults with `package.json.version`, so an omitted update fails CI. Do not resolve `latest` at runtime or reuse the frozen `v1` tag. -2. Review the release PR's current head commit, then add `run-e2e` for a same-repository PR or dispatch the full suite from `main` for a fork. Follow the [GitLab E2E procedures](#gitlab-end-to-end-tests). After any new commit, including an update from `main`, review the changes and remove and re-add the label or dispatch another run for the new SHA. +2. Review the release PR's current head commit, then dispatch the full suite from `main`. Follow the [GitLab E2E procedures](#gitlab-end-to-end-tests). After any new commit, review the changes and dispatch another run for the new SHA. 3. Merge the release PR only after GitHub CI and the full GitLab suite pass. Check the GitLab E2E job summary: its result must be `success`, its suite must be `full`, and its tested commit must still match the PR head. diff --git a/src/gitlab/workflow.test.ts b/src/gitlab/workflow.test.ts index 1275057..ca289f7 100644 --- a/src/gitlab/workflow.test.ts +++ b/src/gitlab/workflow.test.ts @@ -43,7 +43,6 @@ set -euo pipefail printf '%s\\n' "$*" >> "$MOCK_GH_CALLS" if [ "$MOCK_GH_FAILURE" = true ] || [[ -n "$MOCK_GH_FAILURE" && "$*" == *"$MOCK_GH_FAILURE"* ]]; then exit 1; fi case "$*" in - "api repos/upstream/setup-vp/collaborators/reviewer/permission --jq .permission") printf '%s\\n' "$MOCK_PERMISSION" ;; "api repos/upstream/setup-vp/pulls/123") printf '%s\\n' "$MOCK_PR" ;; *) exit 90 ;; esac @@ -59,17 +58,12 @@ esac GITHUB_OUTPUT: output, GITHUB_REPOSITORY: "upstream/setup-vp", GITHUB_SHA: baseSha, - EVENT_NAME: "workflow_dispatch", - EVENT_ACTION: "", - EVENT_LABEL: "", - LABEL_ACTOR: "reviewer", PR_NUMBER: "", SETUP_REF: "", TEST_SUITE: "", VITE_PLUS_VERSION: "", MOCK_GH_CALLS: calls, MOCK_GH_FAILURE: "false", - MOCK_PERMISSION: "write", ...overrides, }, }); @@ -87,28 +81,19 @@ esac } describe("GitLab E2E requests", () => { - it("limits label runs to same-repository PRs and manual runs to main", () => { - expect(Object.keys(workflow.on)).toEqual(["pull_request", "workflow_dispatch"]); - expect(workflow.on.pull_request).toEqual({ branches: ["main"], types: ["labeled"] }); + it("runs only from main with a dedicated environment credential", () => { + expect(Object.keys(workflow.on)).toEqual(["workflow_dispatch"]); expect(workflow.on.workflow_dispatch.inputs.pull_request).toEqual( expect.objectContaining({ required: false, default: "" }), ); expect(Object.keys(workflow.jobs)).toEqual(["gitlab-e2e"]); - expect(workflow.jobs["gitlab-e2e"].if.replace(/\s+/g, " ")).toBe( - "(github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main') || " + - "(github.event_name == 'pull_request' && github.event.action == 'labeled' && " + - "github.event.label.name == 'run-e2e' && " + - "github.event.pull_request.head.repo.full_name == github.repository)", - ); + expect(workflow.jobs["gitlab-e2e"].if).toBe("github.ref == 'refs/heads/main'"); + expect(workflow.jobs["gitlab-e2e"].environment).toBe("gitlab-e2e"); expect(workflow.permissions).toEqual({ contents: "read", "pull-requests": "read" }); expect(steps.every((step) => !step.uses && !step.run.includes("${{"))).toBe(true); expect(parameters.env).toEqual({ - EVENT_NAME: "${{ github.event_name }}", - EVENT_ACTION: "${{ github.event.action }}", - EVENT_LABEL: "${{ github.event.label.name }}", - LABEL_ACTOR: "${{ github.event.sender.login }}", - PR_NUMBER: "${{ github.event.pull_request.number || inputs.pull_request }}", - SETUP_REF: "${{ github.event.pull_request.head.sha || inputs.setup_ref }}", + PR_NUMBER: "${{ inputs.pull_request }}", + SETUP_REF: "${{ inputs.setup_ref }}", TEST_SUITE: "${{ inputs.suite }}", VITE_PLUS_VERSION: "${{ inputs.vite_plus_version }}", GH_TOKEN: "${{ github.token }}", @@ -117,7 +102,7 @@ describe("GitLab E2E requests", () => { false, ); const trigger = steps.find((step) => step.id === "trigger")!; - expect(trigger.env?.GITLAB_TRIGGER_TOKEN).toBe("${{ secrets.GITLAB_TRIGGER_TOKEN }}"); + expect(trigger.env?.GITLAB_TRIGGER_TOKEN).toBe("${{ secrets.SETUP_VP_GITLAB_TRIGGER_TOKEN }}"); expect(trigger.run).toContain("$GITHUB_STEP_SUMMARY"); expect(steps.at(-1)!.run).toContain("GitLab pipeline finished with status"); }); @@ -159,90 +144,6 @@ describe("GitLab E2E requests", () => { }); }); -describe("GitLab E2E label approval", () => { - const approvedPr = { - state: "open", - head: { sha: headSha, repo: { full_name: "upstream/setup-vp" } }, - base: { ref: "main", repo: { full_name: "upstream/setup-vp" } }, - labels: [{ name: "run-e2e" }], - }; - function resolveLabel(overrides: Record = {}) { - return resolveParameters({ - EVENT_NAME: "pull_request", - EVENT_ACTION: "labeled", - EVENT_LABEL: "run-e2e", - PR_NUMBER: "123", - SETUP_REF: headSha, - MOCK_PR: JSON.stringify(approvedPr), - ...overrides, - }); - } - - it.each(["write", "admin"])("accepts a labeler with %s access", (permission) => { - const result = resolveLabel({ - MOCK_PERMISSION: permission, - TEST_SUITE: "required", - VITE_PLUS_VERSION: "0.3.1", - GITHUB_ACTOR: "rerunner", - GITHUB_TRIGGERING_ACTOR: "rerunner", - }); - expect(result.status, result.stderr).toBe(0); - expect(result.outputs).toEqual({ - setup_vp_ref: headSha, - suite: "full", - vite_plus_version: "latest", - }); - expect(result.calls).toBe( - "api repos/upstream/setup-vp/collaborators/reviewer/permission --jq .permission\n" + - "api repos/upstream/setup-vp/pulls/123\n", - ); - }); - - it.each(["read", "triage", "none", ""])("rejects a labeler with %s access", (permission) => { - const result = resolveLabel({ MOCK_PERMISSION: permission }); - expect(result.status).toBe(1); - expect(result.outputs).toEqual({}); - expect(result.calls).not.toContain("/pulls/"); - }); - - it.each([ - { EVENT_NAME: "push" }, - { EVENT_NAME: "pull_request_target" }, - { EVENT_ACTION: "synchronize" }, - { EVENT_ACTION: "unlabeled" }, - { EVENT_LABEL: "bug" }, - { PR_NUMBER: "" }, - { PR_NUMBER: "123\n" }, - { SETUP_REF: "" }, - { SETUP_REF: "v1.20.0" }, - { SETUP_REF: `${headSha}\nextra=true` }, - ])("rejects an invalid label event %j before API access", (env) => { - const result = resolveLabel(env); - expect(result.status).toBe(1); - expect(result.outputs).toEqual({}); - expect(result.calls).toBe(""); - }); - - it.each([ - ["new commit", { head: { ...approvedPr.head, sha: baseSha } }], - ["fork", { head: { ...approvedPr.head, repo: { full_name: "contributor/setup-vp" } } }], - ["deleted repository", { head: { ...approvedPr.head, repo: null } }], - ["base change", { base: { ...approvedPr.base, ref: "release" } }], - ["closed PR", { state: "closed" }], - ["removed label", { labels: [] }], - ])("rejects a label request after a %s", (_reason, changes) => { - const result = resolveLabel({ MOCK_PR: JSON.stringify({ ...approvedPr, ...changes }) }); - expect(result.status).toBe(1); - expect(result.outputs).toEqual({}); - }); - - it.each(["/permission", "/pulls/"])("fails closed if the %s API fails", (endpoint) => { - const result = resolveLabel({ MOCK_GH_FAILURE: endpoint }); - expect(result.status).toBe(1); - expect(result.outputs).toEqual({}); - }); -}); - describe.each(["upstream/setup-vp", "contributor/setup-vp"])( "GitLab E2E reviewed commit from %s", (headRepository) => { diff --git a/src/rebuild-bundle-workflow.test.ts b/src/rebuild-bundle-workflow.test.ts index 2fa068f..4265951 100644 --- a/src/rebuild-bundle-workflow.test.ts +++ b/src/rebuild-bundle-workflow.test.ts @@ -155,6 +155,7 @@ describe("bundle rebuild credential isolation", () => { type: "string", }); expect(publisher.jobs.publish.if).toBe("github.ref == 'refs/heads/main'"); + expect(publisher.jobs.publish.environment).toBe("bundle-publish"); expect(publisher.permissions).toEqual({}); expect(publisher.jobs.publish.permissions).toEqual({ actions: "read", @@ -177,8 +178,8 @@ describe("bundle rebuild credential isolation", () => { }); expect(buildSteps.at(-1)!.with!.name).toBe("rebuilt-bundles-${{ github.run_attempt }}"); expect(commitSteps[3].with).toEqual({ - "client-id": "${{ secrets.APP_ID }}", - "private-key": "${{ secrets.APP_PRIVATE_KEY }}", + "client-id": "${{ vars.SETUP_VP_BUNDLE_APP_CLIENT_ID }}", + "private-key": "${{ secrets.SETUP_VP_BUNDLE_APP_PRIVATE_KEY }}", repositories: "${{ github.event.repository.name }}", "permission-contents": "write", });