feat: Automate example deploys #19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy PR stack | |
| on: | |
| pull_request: | |
| branches: ['main'] | |
| types: [opened, synchronize] | |
| concurrency: | |
| group: pr-deploy-${{ github.event.number }} | |
| jobs: | |
| deploy-pr-stack: | |
| name: Deploy PR stack | |
| environment: SST Deployments | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| submodules: 'true' | |
| - uses: pnpm/action-setup@v4 | |
| with: | |
| version: 9 | |
| - uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| cache: 'pnpm' | |
| - name: Install dependencies | |
| run: pnpm install --frozen-lockfile | |
| - name: Cache SST state | |
| uses: actions/cache@v4 | |
| with: | |
| path: .sst | |
| key: sst-cache-${{ github.event.number }}-${{ runner.os }} | |
| restore-keys: | | |
| sst-cache-${{ runner.os }} | |
| - name: Deploy stack | |
| env: | |
| CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
| AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| NEON_API_KEY: ${{ secrets.NEON_API_KEY }} | |
| run: | | |
| pnpm sst deploy --stage "pr-${{ github.event.number }}" | |
| - name: Read specific outputs from output.json | |
| run: | | |
| if [ -f ".sst/outputs.json" ]; then | |
| frontend=$(jq -r '.frontend' .sst/outputs.json) | |
| echo "frontend=$frontend" >> $GITHUB_ENV | |
| backend=$(jq -r '.backend' .sst/outputs.json) | |
| echo "backend=$backend" >> $GITHUB_ENV | |
| else | |
| echo "sst outputs file not found. Exiting." | |
| exit 1 | |
| fi | |
| - name: Add comment to PR | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const frontend = process.env.frontend; | |
| const backend = process.env.backend; | |
| const prNumber = context.issue.number; | |
| const commentBody = `- frontend: ${frontend} | |
| - backend: ${backend} | |
| `; | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| }); | |
| const existingComment = comments.find(comment => comment.user.login ==='github-actions[bot]' && comment.body.startsWith("- frontend:")); | |
| if (existingComment) { | |
| // Update the existing comment | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: existingComment.id, | |
| body: commentBody, | |
| }); | |
| } else { | |
| // Create a new comment if none exists | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| body: commentBody, | |
| }); | |
| } |