Deploy PR Preview #214
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 Preview | |
| on: | |
| workflow_run: | |
| workflows: ["Build PR Preview"] | |
| types: | |
| - completed | |
| permissions: | |
| actions: read | |
| deployments: write | |
| contents: read | |
| pull-requests: write | |
| jobs: | |
| deploy-preview: | |
| if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 15 | |
| steps: | |
| - name: Download artifact from Build Job | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: preview-dist | |
| path: artifact | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| run-id: ${{ github.event.workflow_run.id }} | |
| - name: Read PR number | |
| id: pr | |
| run: | | |
| PR_NUMBER=$(cat artifact/pr_number.txt) | |
| echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT | |
| - name: Read PR head SHA | |
| id: sha | |
| run: | | |
| PR_HEAD_SHA=$(cat artifact/pr_head_sha.txt) | |
| echo "pr_head_sha=$PR_HEAD_SHA" >> $GITHUB_OUTPUT | |
| - name: Create GitHub Deployment | |
| id: deployment | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const deployment = await github.rest.repos.createDeployment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| ref: '${{ steps.sha.outputs.pr_head_sha }}', | |
| environment: 'pr-preview-${{ steps.pr.outputs.pr_number }}', | |
| auto_merge: false, | |
| required_contexts: [], | |
| transient_environment: true, | |
| description: 'PR Preview Deployment' | |
| }); | |
| core.setOutput('deployment_id', deployment.data.id); | |
| await github.rest.repos.createDeploymentStatus({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| deployment_id: deployment.data.id, | |
| state: 'in_progress', | |
| description: 'Deploying preview to Cloudflare Pages...' | |
| }); | |
| - name: Publish to Cloudflare Pages | |
| id: cloudflare_deploy | |
| uses: cloudflare/wrangler-action@v3 | |
| with: | |
| apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
| accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | |
| command: > | |
| pages deploy artifact/dist | |
| --project-name=${{ vars.CLOUDFLARE_PROJECT_NAME }} | |
| --branch=pr-${{ steps.pr.outputs.pr_number }} | |
| - name: Set deployment status to success | |
| if: success() | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| await github.rest.repos.createDeploymentStatus({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| deployment_id: ${{ steps.deployment.outputs.deployment_id }}, | |
| state: 'success', | |
| environment_url: '${{ steps.cloudflare_deploy.outputs.pages-deployment-alias-url }}', | |
| description: 'Preview deployed successfully' | |
| }); | |
| - name: Set deployment status to failure | |
| if: failure() | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| await github.rest.repos.createDeploymentStatus({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| deployment_id: ${{ steps.deployment.outputs.deployment_id }}, | |
| state: 'failure', | |
| log_url: '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}', | |
| description: 'Preview deployment failed' | |
| }); | |
| - name: Comment PR with Preview Link | |
| if: success() | |
| uses: thollander/actions-comment-pull-request@v3 | |
| with: | |
| pr-number: ${{ steps.pr.outputs.pr_number }} | |
| message: | | |
| ### Preview Deployed! | |
| | Item | Status | | |
| | :--- | :--- | | |
| | **Latest Deploy** | [Visit Preview](${{ steps.cloudflare_deploy.outputs.pages-deployment-alias-url }}) | | |
| | **Environment** | `Preview (PR-${{ steps.pr.outputs.pr_number }})` | | |
| | **Action** | [View Logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) | | |
| _Last updated at ${{ github.event.workflow_run.updated_at }}_ | |
| comment-tag: preview_deploy_status | |
| - name: Comment PR with failure notice | |
| if: failure() | |
| uses: thollander/actions-comment-pull-request@v3 | |
| with: | |
| pr-number: ${{ steps.pr.outputs.pr_number }} | |
| message: | | |
| ### ⚠️ Preview Deploy Failed! | |
| | Item | Status | | |
| | :--- | :--- | | |
| | **Latest Deploy** | ❌ Failed | | |
| | **Environment** | `Preview (PR-${{ steps.pr.outputs.pr_number }})` | | |
| | **Action** | [View Logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) | | |
| _Last updated at ${{ github.event.workflow_run.updated_at }}_ | |
| comment-tag: preview_deploy_status |