|
| 1 | +name: PR Preview |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [main] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + pull-requests: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + preview: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@v6 |
| 16 | + |
| 17 | + - uses: subosito/flutter-action@v2 |
| 18 | + with: |
| 19 | + channel: stable |
| 20 | + cache: true |
| 21 | + |
| 22 | + - name: Install dependencies |
| 23 | + run: flutter pub get |
| 24 | + |
| 25 | + - name: Build web |
| 26 | + run: flutter build web --release --pwa-strategy=offline-first --base-href "/" |
| 27 | + |
| 28 | + - name: Upload preview artifact |
| 29 | + uses: actions/upload-artifact@v4 |
| 30 | + id: artifact |
| 31 | + with: |
| 32 | + name: preview-pr-${{ github.event.number }} |
| 33 | + path: build/web |
| 34 | + retention-days: 7 |
| 35 | + |
| 36 | + - name: Comment PR with preview link |
| 37 | + uses: actions/github-script@v7 |
| 38 | + with: |
| 39 | + script: | |
| 40 | + const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
| 41 | + const body = [ |
| 42 | + '### 🔍 Preview build ready', |
| 43 | + '', |
| 44 | + `Download the preview artifact and open \`index.html\` locally, or check the [workflow run](${runUrl}) to download it.`, |
| 45 | + '', |
| 46 | + `Built from \`${context.sha.substring(0, 7)}\` · artifact expires in 7 days.`, |
| 47 | + ].join('\n'); |
| 48 | +
|
| 49 | + const { data: comments } = await github.rest.issues.listComments({ |
| 50 | + owner: context.repo.owner, |
| 51 | + repo: context.repo.repo, |
| 52 | + issue_number: context.issue.number, |
| 53 | + }); |
| 54 | + const existing = comments.find(c => c.body.includes('Preview build ready')); |
| 55 | + if (existing) { |
| 56 | + await github.rest.issues.updateComment({ |
| 57 | + owner: context.repo.owner, |
| 58 | + repo: context.repo.repo, |
| 59 | + comment_id: existing.id, |
| 60 | + body, |
| 61 | + }); |
| 62 | + } else { |
| 63 | + await github.rest.issues.createComment({ |
| 64 | + owner: context.repo.owner, |
| 65 | + repo: context.repo.repo, |
| 66 | + issue_number: context.issue.number, |
| 67 | + body, |
| 68 | + }); |
| 69 | + } |
0 commit comments