Test /preview comment #7
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: Preview Pages Management | |
| on: | |
| issue_comment: | |
| types: [created] | |
| jobs: | |
| check-permission: | |
| name: Check Author & Command | |
| if: github.event.issue.pull_request && contains(github.event.comment.body, '/preview') | |
| runs-on: ubuntu-latest | |
| outputs: | |
| authorized: ${{ steps.check.outputs.authorized }} | |
| steps: | |
| - name: Check user permissions | |
| id: check | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const username = context.actor; | |
| // Check commenter's permission on the repository | |
| const { data: permission } = await github.rest.repos.getCollaboratorPermissionLevel({ | |
| owner, | |
| repo, | |
| username, | |
| }); | |
| const allowed = ['admin', 'write'].includes(permission.permission); | |
| if (!allowed) { | |
| core.setFailed(`User @${username} is not authorized to trigger preview deployments.`); | |
| } | |
| core.setOutput('authorized', allowed); | |
| deploy-preview: | |
| name: Deploy Preview to Cloudflare Pages | |
| needs: check-permission | |
| if: github.event.action != 'closed' | |
| runs-on: ubuntu-latest | |
| permissions: | |
| pull-requests: write # Required to post/update the preview link comment | |
| contents: read | |
| steps: | |
| - name: Checkout Curriculum (PR Code) | |
| uses: actions/checkout@v4 | |
| with: | |
| path: Curriculum | |
| - name: Checkout Engine | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: ThePythonLedger/Docusaurus-engine | |
| path: engine | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| cache: 'npm' | |
| cache-dependency-path: engine/package-lock.json | |
| - name: Build Site Assets | |
| env: | |
| DOCUSAURUS_URL: https://pr-${{ github.event.number }}.python-ledger-preview.pages.dev | |
| DOCUSAURUS_BASE_URL: / | |
| run: | | |
| cd engine | |
| npm ci | |
| npm run build | |
| - name: Deploy to Cloudflare Pages | |
| uses: cloudflare/wrangler-action@v4 | |
| id: cloudflare_deploy | |
| with: | |
| apiToken: ${{ secrets.CF_API_TOKEN }} | |
| accountId: ${{ secrets.CF_ACCOUNT_ID }} | |
| command: pages deploy engine/build --project-name=python-ledger-preview --branch=pr-${{ github.event.number }} | |
| - name: Comment Preview Link on PR | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const prNumber = context.issue.number; | |
| const previewUrl = `https://pr-${prNumber}.python-ledger-preview.pages.dev`; | |
| const message = `🚀 **Preview Deployment Ready!**\n\nContributors can view changes live at:\n${previewUrl}`; | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| }); | |
| const botComment = comments.find(c => c.body.includes('Preview Deployment Ready!')); | |
| if (botComment) { | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: botComment.id, | |
| body: message, | |
| }); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| body: message, | |
| }); | |
| } | |
| # CLEANUP PREVIEW: Triggered when the PR is closed or merged | |
| cleanup-preview: | |
| name: Delete Cloudflare Pages Preview | |
| if: github.event.action == 'closed' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| - name: Delete Cloudflare Pages Branch | |
| env: | |
| CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }} | |
| CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }} | |
| run: | | |
| BRANCH_NAME="pr-${{ github.event.number }}" | |
| echo "Deleting Cloudflare Pages deployment alias for branch: $BRANCH_NAME" | |
| # Force delete the alias/branch deployments | |
| npx wrangler pages deployment delete --project-name=python-ledger-preview "pr-${{ github.event.number }}" --force |