ci: simplify preview alias derivation #9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CI | |
| on: | |
| push: | |
| branches: [main] | |
| pull_request: | |
| branches: [main] | |
| jobs: | |
| lint: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | |
| - uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271 # v6.0.9 | |
| - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 | |
| with: | |
| node-version: 24 | |
| cache: pnpm | |
| - run: pnpm ci | |
| - run: pnpm lint | |
| test: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | |
| - uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271 # v6.0.9 | |
| - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 | |
| with: | |
| node-version: 24 | |
| cache: pnpm | |
| - run: pnpm ci | |
| - run: pnpm test | |
| preview: | |
| needs: [lint, test] | |
| # PRs targeting main, excluding forks (which can't access the deploy secrets). | |
| if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| steps: | |
| - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | |
| - uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271 # v6.0.9 | |
| - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 | |
| with: | |
| node-version: 24 | |
| cache: pnpm | |
| - run: pnpm ci | |
| - run: pnpm build | |
| # First 8 chars of the PR head commit form the preview alias, so the URL is | |
| # reproducible from a commit (and matches `pnpm deploy:preview` locally). | |
| # Cloudflare requires the alias to start with a lowercase letter (a bare SHA | |
| # beginning with a hex digit is rejected — API code 10021), so prefix `preview-`. | |
| - id: meta | |
| env: | |
| HEAD_SHA: ${{ github.event.pull_request.head.sha }} | |
| run: echo "sha=${HEAD_SHA:0:8}" >> "$GITHUB_OUTPUT" | |
| # Upload a new Worker version without promoting it to production. The | |
| # --preview-alias mints a stable URL (preview-<sha>-audiotrail.<subdomain>.workers.dev). | |
| - id: preview | |
| uses: cloudflare/wrangler-action@ebbaa1584979971c8614a24965b4405ff95890e0 # v4.0.0 | |
| with: | |
| apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
| packageManager: pnpm | |
| command: versions upload --preview-alias preview-${{ steps.meta.outputs.sha }} | |
| - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 | |
| env: | |
| PREVIEW_URL: ${{ steps.preview.outputs.deployment-url }} | |
| SHA: ${{ steps.meta.outputs.sha }} | |
| with: | |
| script: | | |
| const marker = '<!-- audiotrail-preview -->'; | |
| // wrangler-action returns the per-version URL (https://<8-hex>-<worker>.<sub>.workers.dev); | |
| // swap the version prefix for our commit alias to get the URL deploy:preview reproduces. | |
| const sha = process.env.SHA; | |
| const versionUrl = process.env.PREVIEW_URL; | |
| const aliasUrl = versionUrl | |
| ? versionUrl.replace(/^https:\/\/[0-9a-f]{8}-/, `https://preview-${sha}-`) | |
| : ''; | |
| const body = [ | |
| marker, | |
| '### ⛅ Preview deployment', | |
| '', | |
| aliasUrl ? `This PR is live at ${aliasUrl}` : 'Preview upload finished but no URL was reported.', | |
| '', | |
| `_Commit \`${sha}\` — reproduce locally with \`pnpm deploy:preview\`._`, | |
| ].join('\n'); | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| }); | |
| const existing = comments.find((c) => c.body?.includes(marker)); | |
| if (existing) { | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: existing.id, | |
| body, | |
| }); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| body, | |
| }); | |
| } | |
| deploy: | |
| needs: [lint, test] | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | |
| - uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271 # v6.0.9 | |
| - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 | |
| with: | |
| node-version: 24 | |
| cache: pnpm | |
| - run: pnpm ci | |
| - run: pnpm build | |
| - uses: cloudflare/wrangler-action@ebbaa1584979971c8614a24965b4405ff95890e0 # v4.0.0 | |
| with: | |
| apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
| packageManager: pnpm |