|
| 1 | +name: preview |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - dev |
| 7 | + types: [opened, synchronize, reopened, closed] |
| 8 | + |
| 9 | +concurrency: |
| 10 | + group: preview-${{ github.event.pull_request.number }} |
| 11 | + cancel-in-progress: true |
| 12 | + |
| 13 | +jobs: |
| 14 | + deploy-preview: |
| 15 | + if: github.event.action != 'closed' |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Checkout code |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Setup Bun |
| 22 | + uses: oven-sh/setup-bun@v1 |
| 23 | + with: |
| 24 | + bun-version: latest |
| 25 | + |
| 26 | + - name: Install dependencies |
| 27 | + run: bun install --frozen-lockfile |
| 28 | + |
| 29 | + - name: Deploy preview |
| 30 | + id: deploy |
| 31 | + run: | |
| 32 | + DEPLOY_OUTPUT=$(bun sst deploy --stage=pr-${{ github.event.pull_request.number }} 2>&1) |
| 33 | + echo "$DEPLOY_OUTPUT" |
| 34 | + # Look for custom subdomain preview URLs |
| 35 | + PREVIEW_URL=$(echo "$DEPLOY_OUTPUT" | grep -oE 'https://[0-9]+\.dev\.natepapes\.com' | head -1) |
| 36 | + echo "url=$PREVIEW_URL" >> $GITHUB_OUTPUT |
| 37 | + env: |
| 38 | + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 39 | + |
| 40 | + - name: Comment PR with preview URL |
| 41 | + uses: actions/github-script@v7 |
| 42 | + with: |
| 43 | + script: | |
| 44 | + const { data: comments } = await github.rest.issues.listComments({ |
| 45 | + owner: context.repo.owner, |
| 46 | + repo: context.repo.repo, |
| 47 | + issue_number: context.issue.number, |
| 48 | + }); |
| 49 | +
|
| 50 | + const botComment = comments.find(comment => |
| 51 | + comment.user.type === 'Bot' && comment.body.includes('🚀 Preview deployed') |
| 52 | + ); |
| 53 | +
|
| 54 | + const body = `🚀 Preview deployed to: ${{ steps.deploy.outputs.url }} |
| 55 | +
|
| 56 | + This preview will be automatically deleted when the PR is closed.`; |
| 57 | +
|
| 58 | + if (botComment) { |
| 59 | + await github.rest.issues.updateComment({ |
| 60 | + owner: context.repo.owner, |
| 61 | + repo: context.repo.repo, |
| 62 | + comment_id: botComment.id, |
| 63 | + body: 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: body |
| 71 | + }); |
| 72 | + } |
| 73 | +
|
| 74 | + cleanup-preview: |
| 75 | + if: github.event.action == 'closed' |
| 76 | + runs-on: ubuntu-latest |
| 77 | + steps: |
| 78 | + - name: Checkout code |
| 79 | + uses: actions/checkout@v4 |
| 80 | + |
| 81 | + - name: Setup Bun |
| 82 | + uses: oven-sh/setup-bun@v1 |
| 83 | + with: |
| 84 | + bun-version: latest |
| 85 | + |
| 86 | + - name: Install dependencies |
| 87 | + run: bun install --frozen-lockfile |
| 88 | + |
| 89 | + - name: Remove preview deployment |
| 90 | + run: bun sst remove --stage=pr-${{ github.event.pull_request.number }} |
| 91 | + env: |
| 92 | + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 93 | + |
| 94 | + - name: Comment PR about cleanup |
| 95 | + uses: actions/github-script@v7 |
| 96 | + with: |
| 97 | + script: | |
| 98 | + await github.rest.issues.createComment({ |
| 99 | + owner: context.repo.owner, |
| 100 | + repo: context.repo.repo, |
| 101 | + issue_number: context.issue.number, |
| 102 | + body: 'Preview deployment has been cleaned up.' |
| 103 | + }); |
0 commit comments