diff --git a/.github/workflows/pkg-pr-new.yml b/.github/workflows/pkg-pr-new.yml new file mode 100644 index 0000000..02bc5db --- /dev/null +++ b/.github/workflows/pkg-pr-new.yml @@ -0,0 +1,111 @@ +name: Publish Any Commit + +# Publishes preview packages via pkg.pr.new. Gated so untrusted/fork PRs can't +# trigger a build+publish just by opening a PR - only: +# - push: pushing to a branch already requires repo write access +# - pull_request: only when opened/updated by an author with write access +# (e.g. a maintainer's own branch) +# - pull_request_review: for everyone else, once a maintainer approves the PR +# tags/releases are excluded so version-tag pushes don't trigger a duplicate publish. +on: + push: + branches: ['**'] + tags: ['!**'] + pull_request: + types: [opened, synchronize, reopened] + pull_request_review: + types: [submitted] + +permissions: {} + +jobs: + check: + name: Check permissions + if: github.event_name == 'push' || github.event_name == 'pull_request' || github.event.review.state == 'approved' + runs-on: ubuntu-latest + permissions: + pull-requests: read + outputs: + approved: ${{ steps.check.outputs.approved }} + steps: + - name: Push events are already gated by branch write access + if: github.event_name == 'push' + id: push-approved + run: echo "approved=true" >> "$GITHUB_OUTPUT" + - name: Check PR author permission + uses: actions-cool/check-user-permission@c21884f3dda18dafc2f8b402fe807ccc9ec1aa5e # v2.4.0 + if: github.event_name == 'pull_request' + id: checkAuthor + with: + username: ${{ github.event.pull_request.user.login }} + require: write + - name: Check reviewer permission + uses: actions-cool/check-user-permission@c21884f3dda18dafc2f8b402fe807ccc9ec1aa5e # v2.4.0 + if: github.event_name == 'pull_request_review' + id: checkPermissions + with: + require: write + - name: Combine results + id: check + # Exactly one of the three steps above runs per trigger, the others are + # skipped and their outputs are empty strings, so this picks whichever ran. + env: + APPROVED: ${{ steps.push-approved.outputs.approved || steps.checkAuthor.outputs.require-result || steps.checkPermissions.outputs.require-result }} + run: echo "approved=${APPROVED}" >> "$GITHUB_OUTPUT" + + build: + name: Build + needs: check + if: needs.check.outputs.approved == 'true' + runs-on: ubuntu-latest + permissions: + contents: read + steps: + # For PR-related events this checks out the PR head commit (not a merge + # commit with main), matching the sha pkg-pr-new publishes/comments with. + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + persist-credentials: false + - uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0 + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + package-manager-cache: false + - run: pnpm install --frozen-lockfile + - run: pnpm run build + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: dist + path: dist/ + + publish: + name: Publish + needs: build + runs-on: ubuntu-latest + # Separate job from `build` so only this one - the one that actually needs + # to comment on the PR - carries pull-requests: write. + permissions: + pull-requests: write + steps: + # For PR-related events this checks out the PR head commit (not a merge + # commit with main), matching the sha pkg-pr-new publishes/comments with. + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + persist-credentials: false + - uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0 + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + package-manager-cache: false + - run: pnpm install --frozen-lockfile --ignore-scripts + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: dist + path: dist/ + # pkg-pr-new publishes previews through its own GitHub App, not to npm, so there is + # no npm token to replace with trusted publishing. + # --commentWithSha uses the commit sha in install URLs/comments instead + # of a PR number, and pnpm exec (not npx/dlx) runs the pinned devDependency. + - run: pnpm exec pkg-pr-new publish --commentWithSha # zizmor: ignore[use-trusted-publishing] diff --git a/package.json b/package.json index b889e7b..e29b4b4 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "knip": "^6.43.0", "oxfmt": "^0.74.0", "oxlint": "^1.89.0", + "pkg-pr-new": "^0.0.92", "playwright": "^1.64.1", "publint": "^0.3.25", "tsdown": "^0.23.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f6fb1fd..c2b0f3c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -228,6 +228,9 @@ importers: oxlint: specifier: ^1.89.0 version: 1.89.0 + pkg-pr-new: + specifier: ^0.0.92 + version: 0.0.92 playwright: specifier: ^1.64.1 version: 1.64.1 @@ -1346,6 +1349,10 @@ packages: resolution: {integrity: sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA==} engines: {node: '>=12'} + pkg-pr-new@0.0.92: + resolution: {integrity: sha512-l9HE4y0LmS5n3pw9vKby7m9O+SBzjWF5HXGH6Z8jdPWW7OW7hFMKsFOrgubc0JuPz8gYEuIw1mLaU3GEmPuukQ==} + hasBin: true + playwright-core@1.64.1: resolution: {integrity: sha512-BC4s8y6SEVA6Q1rSxZTto8hI1KUI3xXlHGZgvGqP4IqPdI/a6FX9H29IDNz7vKo9l21b6LfYosScPL6M3xxyDQ==} engines: {node: '>=20'} @@ -2338,6 +2345,8 @@ snapshots: picomatch@4.0.7: {} + pkg-pr-new@0.0.92: {} + playwright-core@1.64.1: {} playwright@1.64.1: