Add PR preview workflow for testing changes before merge #2
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: PR Preview | |
| on: | |
| pull_request: | |
| types: [opened, synchronize, reopened] | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| jobs: | |
| build-preview: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Inject tokens | |
| env: | |
| APIFY_TOKEN: ${{ secrets.APIFY_TOKEN }} | |
| GOOGLE_TOKEN: ${{ secrets.GOOGLE_TOKEN }} | |
| run: | | |
| sed -i "s/__APIFY_TOKEN__/${APIFY_TOKEN}/g" map.js | |
| sed -i "s/__GOOGLE_TOKEN__/${GOOGLE_TOKEN}/g" index.html | |
| - name: Upload preview artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: pr-preview-${{ github.event.pull_request.number }} | |
| path: . | |
| retention-days: 7 | |
| - name: Comment on PR | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const prNumber = context.payload.pull_request.number; | |
| const runId = context.runId; | |
| const repo = context.repo; | |
| const comment = `## 🚀 Preview Build Ready | |
| A preview build has been created for this pull request. | |
| ### Download Preview | |
| [Download the preview artifact](https://github.com/${repo.owner}/${repo.repo}/actions/runs/${runId}) | |
| ### Test Locally | |
| 1. Download and extract the artifact | |
| 2. Run a local server in the extracted directory: | |
| \`\`\`bash | |
| python -m http.server 8000 | |
| # or | |
| npx http-server -p 8000 | |
| \`\`\` | |
| 3. Open http://localhost:8000 in your browser | |
| --- | |
| *Preview artifact will be available for 7 days*`; | |
| // Find existing comment | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: repo.owner, | |
| repo: repo.repo, | |
| issue_number: prNumber, | |
| }); | |
| const botComment = comments.find(comment => | |
| comment.user.type === 'Bot' && | |
| comment.body.includes('🚀 Preview Build Ready') | |
| ); | |
| if (botComment) { | |
| // Update existing comment | |
| await github.rest.issues.updateComment({ | |
| owner: repo.owner, | |
| repo: repo.repo, | |
| comment_id: botComment.id, | |
| body: comment | |
| }); | |
| } else { | |
| // Create new comment | |
| await github.rest.issues.createComment({ | |
| owner: repo.owner, | |
| repo: repo.repo, | |
| issue_number: prNumber, | |
| body: comment | |
| }); | |
| } |