Skip to content

Deploy preview

Deploy preview #37

# Deploy preview to Cloudflare Pages
name: Deploy preview
on:
workflow_run:
workflows: ["Build preview"]
types:
- completed
jobs:
deploy:
runs-on: ubuntu-latest
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
permissions: write-all
steps:
- name: Download artifact
uses: actions/github-script@v6
with:
script: |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id }},
});
const matchArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "build"
})[0];
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
const fs = require('fs');
fs.writeFileSync('${{github.workspace}}/build.zip', Buffer.from(download.data));
- run: unzip build.zip -d build
- name: Deploy preview to Cloudflare Pages
id: deploy
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: wiki
directory: build
gitHubToken: ${{ secrets.GITHUB_TOKEN }} # GitHub Deployments
wranglerVersion: "3"
- name: Find PR number
id: find_pr_number
run: |
echo "pr_number=$(cat ./build/PR_NR)" >> $GITHUB_OUTPUT
- name: Find comment
uses: peter-evans/find-comment@v2
id: find_comment
with:
issue-number: ${{ steps.find_pr_number.outputs.pr_number }}
comment-author: "github-actions[bot]"
body-includes: "Preview URL:"
- name: Create or update comment
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.find_comment.outputs.comment-id }}
issue-number: ${{ steps.find_pr_number.outputs.pr_number }}
body: |
Preview URL: ${{ steps.deploy.outputs.alias }} :rocket:
edit-mode: replace