|
| 1 | +name: Deploy to SST |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + pull_request: |
| 7 | + types: [opened, synchronize, reopened, closed] |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + pull-requests: write |
| 12 | + issues: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + deploy-production: |
| 16 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 17 | + environment: 'action-env' |
| 18 | + runs-on: ubuntu-latest |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Checkout code |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + fetch-depth: 0 |
| 25 | + |
| 26 | + - name: Setup Node.js |
| 27 | + uses: actions/setup-node@v4 |
| 28 | + with: |
| 29 | + node-version: '20' |
| 30 | + cache: 'npm' |
| 31 | + |
| 32 | + - name: Install dependencies |
| 33 | + run: npm ci |
| 34 | + |
| 35 | + - name: Configure AWS credentials |
| 36 | + uses: aws-actions/configure-aws-credentials@v4 |
| 37 | + with: |
| 38 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 39 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 40 | + aws-region: ${{ secrets.AWS_REGION }} |
| 41 | + |
| 42 | + - name: Deploy to production |
| 43 | + run: | |
| 44 | + npx sst deploy --stage production |
| 45 | +
|
| 46 | + deploy-preview: |
| 47 | + if: github.event_name == 'pull_request' && github.event.action != 'closed' |
| 48 | + environment: 'action-env' |
| 49 | + runs-on: ubuntu-latest |
| 50 | + |
| 51 | + steps: |
| 52 | + - name: Checkout code |
| 53 | + uses: actions/checkout@v4 |
| 54 | + with: |
| 55 | + fetch-depth: 0 |
| 56 | + |
| 57 | + - name: Setup Node.js |
| 58 | + uses: actions/setup-node@v4 |
| 59 | + with: |
| 60 | + node-version: '20' |
| 61 | + cache: 'npm' |
| 62 | + |
| 63 | + - name: Install dependencies |
| 64 | + run: npm ci |
| 65 | + |
| 66 | + - name: Configure AWS credentials |
| 67 | + uses: aws-actions/configure-aws-credentials@v4 |
| 68 | + with: |
| 69 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 70 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 71 | + aws-region: ${{ secrets.AWS_REGION }} |
| 72 | + |
| 73 | + - name: Deploy preview |
| 74 | + id: deploy |
| 75 | + run: | |
| 76 | + npx sst deploy --stage pr-${{ github.event.number }} |
| 77 | +
|
| 78 | + - name: Get deployment URL |
| 79 | + id: get-url |
| 80 | + run: | |
| 81 | + # Extract the URL from SST state export |
| 82 | + URL=$(npx sst state export --stage pr-${{ github.event.number }} | jq -r '.latest.resources[] | select(.type == "sst:aws:Nextjs") | .outputs._metadata.url') |
| 83 | + echo "url=$URL" >> $GITHUB_OUTPUT |
| 84 | +
|
| 85 | + - name: Comment on PR |
| 86 | + uses: actions/github-script@v7 |
| 87 | + with: |
| 88 | + script: | |
| 89 | + // Find existing bot comment |
| 90 | + const { data: comments } = await github.rest.issues.listComments({ |
| 91 | + owner: context.repo.owner, |
| 92 | + repo: context.repo.repo, |
| 93 | + issue_number: context.issue.number, |
| 94 | + }); |
| 95 | + |
| 96 | + const botComment = comments.find(comment => |
| 97 | + comment.user.type === 'Bot' && |
| 98 | + comment.body.includes('## Preview Deployment') |
| 99 | + ); |
| 100 | + |
| 101 | + const commentBody = `## Preview Deployment |
| 102 | + |
| 103 | + | Property | Value | |
| 104 | + |----------|-------| |
| 105 | + | Status | Deployed successfully | |
| 106 | + | Preview URL | [${{ steps.get-url.outputs.url }}](${{ steps.get-url.outputs.url }}) | |
| 107 | + | Stage | \`pr-${{ github.event.number }}\` | |
| 108 | + |
| 109 | + This preview deployment will be automatically removed when the PR is closed.`; |
| 110 | + |
| 111 | + if (botComment) { |
| 112 | + await github.rest.issues.updateComment({ |
| 113 | + owner: context.repo.owner, |
| 114 | + repo: context.repo.repo, |
| 115 | + comment_id: botComment.id, |
| 116 | + body: commentBody |
| 117 | + }); |
| 118 | + } else { |
| 119 | + await github.rest.issues.createComment({ |
| 120 | + owner: context.repo.owner, |
| 121 | + repo: context.repo.repo, |
| 122 | + issue_number: context.issue.number, |
| 123 | + body: commentBody |
| 124 | + }); |
| 125 | + } |
| 126 | +
|
| 127 | + remove-preview: |
| 128 | + if: github.event_name == 'pull_request' && github.event.action == 'closed' |
| 129 | + environment: 'action-env' |
| 130 | + runs-on: ubuntu-latest |
| 131 | + |
| 132 | + steps: |
| 133 | + - name: Checkout code |
| 134 | + uses: actions/checkout@v4 |
| 135 | + with: |
| 136 | + fetch-depth: 0 |
| 137 | + |
| 138 | + - name: Setup Node.js |
| 139 | + uses: actions/setup-node@v4 |
| 140 | + with: |
| 141 | + node-version: '20' |
| 142 | + cache: 'npm' |
| 143 | + |
| 144 | + - name: Install dependencies |
| 145 | + run: npm ci |
| 146 | + |
| 147 | + - name: Configure AWS credentials |
| 148 | + uses: aws-actions/configure-aws-credentials@v4 |
| 149 | + with: |
| 150 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 151 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 152 | + aws-region: ${{ secrets.AWS_REGION }} |
| 153 | + |
| 154 | + - name: Remove preview deployment |
| 155 | + run: | |
| 156 | + npx sst remove --stage pr-${{ github.event.number }} |
| 157 | +
|
| 158 | + - name: Comment on PR |
| 159 | + uses: actions/github-script@v7 |
| 160 | + with: |
| 161 | + script: | |
| 162 | + // Find existing bot comment |
| 163 | + const { data: comments } = await github.rest.issues.listComments({ |
| 164 | + owner: context.repo.owner, |
| 165 | + repo: context.repo.repo, |
| 166 | + issue_number: context.issue.number, |
| 167 | + }); |
| 168 | + |
| 169 | + const botComment = comments.find(comment => |
| 170 | + comment.user.type === 'Bot' && |
| 171 | + comment.body.includes('## Preview Deployment') |
| 172 | + ); |
| 173 | + |
| 174 | + if (botComment) { |
| 175 | + await github.rest.issues.updateComment({ |
| 176 | + owner: context.repo.owner, |
| 177 | + repo: context.repo.repo, |
| 178 | + comment_id: botComment.id, |
| 179 | + body: `## Preview Deployment |
| 180 | + |
| 181 | + | Property | Value | |
| 182 | + |----------|-------| |
| 183 | + | Status | Removed | |
| 184 | + | Stage | \`pr-${{ github.event.number }}\` | |
| 185 | + | Cleanup | Completed | |
| 186 | + |
| 187 | + The preview deployment has been automatically removed.` |
| 188 | + }); |
| 189 | + } |
0 commit comments