Deploy to Cloudflare Pages #46
This file contains 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: Cloudflare Preview Deployment | |
on: | |
workflow_run: | |
workflows: ["Build"] | |
types: | |
- completed | |
jobs: | |
deploy-to-cloudflare: | |
name: Deploy to Cloudflare Pages | |
runs-on: ubuntu-22.04 | |
permissions: | |
contents: read | |
pull-requests: write | |
if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Setup Node | |
uses: actions/setup-node@v3 | |
with: | |
node-version: "18.14.1" | |
- name: Wrangler Install | |
run: npm install -g wrangler | |
- name: Download build artifact | |
uses: dawidd6/action-download-artifact@v2 | |
with: | |
name: pr | |
workflow: build.yml | |
path: pr | |
- name: Publish to Cloudflare | |
id: publish-to-cloudflare | |
env: | |
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
REPOSITORY: ${{ github.repository }} | |
PRODUCTION_BRANCH: "development" | |
OUTPUT_DIRECTORY: "pr" | |
run: | | |
IFS='/' read -ra fields <<< "$REPOSITORY" | |
projectName="${fields[1]}" | |
projectName=$(echo $projectName | sed 's/\./-/g') | |
echo $projectName | |
wrangler pages project list > project_list.txt | |
if grep -q $projectName project_list.txt; then | |
echo "Project found" | |
else | |
echo "Project not found" | |
wrangler pages project create "$projectName" --production-branch "$PRODUCTION_BRANCH" | |
fi | |
echo DEPLOY_URL=$(wrangler pages publish "$OUTPUT_DIRECTORY" --project-name "$projectName" | grep -Eo "(http|https)://[a-zA-Z0-9./?=_%:-]*") >> $GITHUB_OUTPUT | |
- name: Print deploy URL | |
run: echo ${{ steps.publish-to-cloudflare.outputs.DEPLOY_URL }} | |
- name: Download PR number artifact | |
uses: dawidd6/action-download-artifact@v2 | |
with: | |
name: pr-num | |
workflow: build.yml | |
path: pr-num | |
- name: Read PR number from file | |
id: pr-num-reader | |
uses: juliangruber/[email protected] | |
with: | |
path: pr-num/pr-num.txt | |
- name: Print PR number from file | |
run: echo ${{ steps.pr-num-reader.outputs.content }} | |
- name: Comment PR | |
# PR number exists only on "pull_request" event so comment only when it exists | |
if: ${{ steps.pr-num-reader.outputs.content }} | |
uses: thollander/actions-comment-pull-request@v2 | |
with: | |
message: ${{ steps.publish-to-cloudflare.outputs.DEPLOY_URL }} | |
pr_number: ${{ steps.pr-num-reader.outputs.content }} |