|
| 1 | +name: Pixel Snapshot |
| 2 | + |
| 3 | +# Snapshots the Storybook that the Pixel workflow built for a pull request. |
| 4 | +# This runs from the default branch with PIXEL_KEY and never checks out or runs |
| 5 | +# the pull request's code: its Storybook is only served to Chromium. |
| 6 | +on: |
| 7 | + workflow_run: |
| 8 | + workflows: [Pixel] |
| 9 | + types: [completed] |
| 10 | + |
| 11 | +permissions: {} |
| 12 | + |
| 13 | +cache-mode: read |
| 14 | + |
| 15 | +concurrency: |
| 16 | + group: ${{ github.workflow }}-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }} |
| 17 | + cancel-in-progress: true |
| 18 | + |
| 19 | +jobs: |
| 20 | + # Statuses are per commit, so only snapshot when exactly one open PR has this |
| 21 | + # head commit, and it targets main. A fork can't share another PR's commit, |
| 22 | + # or pick a stale base, to post statuses on it. |
| 23 | + resolve: |
| 24 | + name: Resolve pull request |
| 25 | + runs-on: ubuntu-24.04 |
| 26 | + timeout-minutes: 5 |
| 27 | + if: >- |
| 28 | + github.repository_owner == 'coder' && |
| 29 | + github.event.workflow_run.event == 'pull_request' && |
| 30 | + github.event.workflow_run.conclusion == 'success' && |
| 31 | + github.event.workflow_run.actor.login != 'dependabot[bot]' |
| 32 | + permissions: |
| 33 | + pull-requests: read |
| 34 | + outputs: |
| 35 | + number: ${{ steps.pr.outputs.number }} |
| 36 | + base_sha: ${{ steps.pr.outputs.base_sha }} |
| 37 | + steps: |
| 38 | + - name: Find PR |
| 39 | + id: pr |
| 40 | + run: | |
| 41 | + gh pr list --repo "$GITHUB_REPOSITORY" --state open --limit 1000 \ |
| 42 | + --json number,headRefOid,headRefName,headRepositoryOwner,baseRefName,baseRefOid \ |
| 43 | + --jq '[.[] | select(.headRefOid == env.HEAD_SHA)] | select(length == 1) | .[] |
| 44 | + | select(.baseRefName == "main" and .headRepositoryOwner.login == env.HEAD_OWNER and .headRefName == env.HEAD_BRANCH) |
| 45 | + | "number=\(.number)\nbase_sha=\(.baseRefOid)"' >> "$GITHUB_OUTPUT" |
| 46 | + env: |
| 47 | + GH_TOKEN: ${{ github.token }} |
| 48 | + HEAD_SHA: ${{ github.event.workflow_run.head_sha }} |
| 49 | + HEAD_OWNER: ${{ github.event.workflow_run.head_repository.owner.login }} |
| 50 | + HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} |
| 51 | + |
| 52 | + snapshot: |
| 53 | + name: Snapshot |
| 54 | + needs: resolve |
| 55 | + if: needs.resolve.outputs.number != '' |
| 56 | + runs-on: ubuntu-24.04 |
| 57 | + timeout-minutes: 15 |
| 58 | + permissions: |
| 59 | + actions: read |
| 60 | + contents: read |
| 61 | + statuses: write |
| 62 | + env: |
| 63 | + HEAD_SHA: ${{ github.event.workflow_run.head_sha }} |
| 64 | + steps: |
| 65 | + - name: Set pending status |
| 66 | + run: >- |
| 67 | + gh api "repos/$GITHUB_REPOSITORY/statuses/$HEAD_SHA" |
| 68 | + -f state=pending -f context="Pixel Snapshot" |
| 69 | + -f description="Snapshotting Storybook" |
| 70 | + -f target_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" |
| 71 | + env: |
| 72 | + GH_TOKEN: ${{ github.token }} |
| 73 | + |
| 74 | + # The default branch, never the pull request. |
| 75 | + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 76 | + with: |
| 77 | + persist-credentials: false |
| 78 | + - name: Setup pnpm, Node.js, and dependencies |
| 79 | + uses: ./.github/actions/pnpm-setup |
| 80 | + |
| 81 | + - name: Install Chromium |
| 82 | + uses: ./.github/actions/playwright-chromium |
| 83 | + |
| 84 | + # Outside the checkout, so the artifact can't replace trusted files. |
| 85 | + - name: Download Storybook |
| 86 | + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 |
| 87 | + with: |
| 88 | + name: storybook |
| 89 | + path: ${{ runner.temp }}/storybook-static |
| 90 | + run-id: ${{ github.event.workflow_run.id }} |
| 91 | + github-token: ${{ github.token }} |
| 92 | + |
| 93 | + # serve-handler follows symlinked directories in a request path, which |
| 94 | + # would let the PR's Storybook read files such as /proc/self/environ. |
| 95 | + # Serve regular files and directories only. |
| 96 | + - name: Reject symlinks |
| 97 | + run: | |
| 98 | + if find "$RUNNER_TEMP/storybook-static" ! -type f ! -type d | grep -q .; then |
| 99 | + echo "::error::Storybook artifact contains links or special files" |
| 100 | + exit 1 |
| 101 | + fi |
| 102 | +
|
| 103 | + # Fork branches are reported as fork:<owner>/<branch>; git branch names |
| 104 | + # can't contain ":", so they never match this repository's. GITHUB_REF is |
| 105 | + # how pixel-storybook links the build to the PR. |
| 106 | + - name: Snapshot |
| 107 | + run: GITHUB_REF="refs/pull/$PR_NUMBER/merge" pnpm pixel-storybook:ci |
| 108 | + env: |
| 109 | + PR_NUMBER: ${{ needs.resolve.outputs.number }} |
| 110 | + PIXEL_KEY: ${{ secrets.PIXEL_KEY }} |
| 111 | + PIXEL_STORYBOOK_DIR: ${{ runner.temp }}/storybook-static |
| 112 | + PIXEL_COMMIT: ${{ github.event.workflow_run.head_sha }} |
| 113 | + PIXEL_BASELINE_COMMIT: ${{ needs.resolve.outputs.base_sha }} |
| 114 | + PIXEL_BRANCH: ${{ github.event.workflow_run.head_repository.full_name == github.repository && github.event.workflow_run.head_branch || format('fork:{0}/{1}', github.event.workflow_run.head_repository.owner.login, github.event.workflow_run.head_branch) }} |
| 115 | + PIXEL_AUTO_REVIEW: "false" |
| 116 | + |
| 117 | + - name: Set final status |
| 118 | + if: ${{ !cancelled() }} |
| 119 | + run: >- |
| 120 | + gh api "repos/$GITHUB_REPOSITORY/statuses/$HEAD_SHA" |
| 121 | + -f state=${{ job.status == 'success' && 'success' || 'failure' }} |
| 122 | + -f context="Pixel Snapshot" |
| 123 | + -f description="${{ job.status == 'success' && 'Snapshot complete' || 'Snapshot failed' }}" |
| 124 | + -f target_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" |
| 125 | + env: |
| 126 | + GH_TOKEN: ${{ github.token }} |
0 commit comments