|
| 1 | +name: Preview Deployment |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, reopened, synchronize, closed] |
| 6 | + |
| 7 | +env: |
| 8 | + FLY_API_TOKEN: ${{ secrets.FLY_PREVIEW_API_TOKEN }} |
| 9 | + FLY_REGION: iad |
| 10 | + FLY_ORG: ${{ vars.FLY_PREVIEW_ORG }} |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + pull-requests: write |
| 15 | + |
| 16 | +jobs: |
| 17 | + preview: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + outputs: |
| 20 | + url: ${{ steps.deploy.outputs.url }} |
| 21 | + concurrency: |
| 22 | + group: pr-${{ github.event.number }} |
| 23 | + |
| 24 | + environment: |
| 25 | + name: preview |
| 26 | + url: ${{ steps.deploy.outputs.url }} |
| 27 | + |
| 28 | + steps: |
| 29 | + - name: Checkout |
| 30 | + uses: actions/checkout@v4 |
| 31 | + |
| 32 | + - name: Deploy preview app |
| 33 | + id: deploy |
| 34 | + |
| 35 | + with: |
| 36 | + config: fly.preview.toml |
| 37 | + vmsize: shared-cpu-1x |
| 38 | + memory: 256 |
| 39 | + |
| 40 | + - name: Comment on PR |
| 41 | + if: github.event.action != 'closed' |
| 42 | + continue-on-error: true |
| 43 | + uses: actions/github-script@v7 |
| 44 | + with: |
| 45 | + script: | |
| 46 | + const url = '${{ steps.deploy.outputs.url }}'; |
| 47 | + const sha = context.sha.substring(0, 7); |
| 48 | + const body = `### Preview Deployment\n\n| Name | URL |\n|------|-----|\n| Preview | ${url} |\n\nCommit: \`${sha}\``; |
| 49 | +
|
| 50 | + // Find existing comment |
| 51 | + const { data: comments } = await github.rest.issues.listComments({ |
| 52 | + owner: context.repo.owner, |
| 53 | + repo: context.repo.repo, |
| 54 | + issue_number: context.issue.number, |
| 55 | + }); |
| 56 | + const existing = comments.find(c => c.body.includes('### Preview Deployment')); |
| 57 | +
|
| 58 | + if (existing) { |
| 59 | + await github.rest.issues.updateComment({ |
| 60 | + owner: context.repo.owner, |
| 61 | + repo: context.repo.repo, |
| 62 | + comment_id: existing.id, |
| 63 | + body, |
| 64 | + }); |
| 65 | + } else { |
| 66 | + await github.rest.issues.createComment({ |
| 67 | + owner: context.repo.owner, |
| 68 | + repo: context.repo.repo, |
| 69 | + issue_number: context.issue.number, |
| 70 | + body, |
| 71 | + }); |
| 72 | + } |
0 commit comments