Deploy to Cloudflare Pages (Production) #137
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 to Cloudflare Pages (Production) | |
on: | |
push: | |
branches: [main] | |
schedule: | |
# Run every day at 00:00 UTC to fetch latest GitHub data | |
- cron: '0 0 * * *' | |
workflow_dispatch: # Allow manual triggers | |
permissions: | |
contents: read | |
deployments: write | |
pull-requests: write | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Setup Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '20' | |
- name: Install dependencies | |
run: npm ci | |
- name: Build site | |
run: npm run build | |
env: | |
# GitHub token for API access (automatically provided by Actions) | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Upload artifact for Cloudflare Pages | |
uses: actions/upload-artifact@v4 | |
with: | |
name: production-files | |
path: ./dist | |
deploy-cloudflare-pages: | |
needs: build | |
runs-on: ubuntu-latest | |
environment: | |
name: production | |
permissions: | |
contents: read | |
deployments: write | |
pull-requests: write | |
steps: | |
- name: Download artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: production-files | |
path: ./dist | |
- name: List files (debug) | |
run: ls -la ./dist | |
- 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 ./dist --project-name=andrewginns | |
- name: Extract deployment URL | |
id: extract-url | |
run: | | |
# Extract URL from wrangler output using the known pattern | |
DEPLOY_URL=$(echo "${{ steps.cloudflare-deploy.outputs.command-output }}" | grep -o 'https://[a-zA-Z0-9]*\.andrewginns\.pages\.dev' || echo "") | |
echo "deployment-url=$DEPLOY_URL" >> $GITHUB_OUTPUT | |
echo "Extracted deployment URL: $DEPLOY_URL" | |
- name: Find merged PR | |
id: find-pr | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const commit = context.sha; | |
const { data: pulls } = await github.rest.pulls.list({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
state: 'closed', | |
sort: 'updated', | |
direction: 'desc' | |
}); | |
const mergedPR = pulls.find(pr => pr.merge_commit_sha === commit); | |
return mergedPR ? mergedPR.number : null; | |
- name: Comment on merged PR | |
if: steps.find-pr.outputs.result != 'null' && steps.extract-url.outputs.deployment-url != '' | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const prNumber = ${{ steps.find-pr.outputs.result }}; | |
const deploymentUrl = '${{ steps.extract-url.outputs.deployment-url }}'; | |
const comment = `🚀 **Production deployment ready!** | |
📋 **PR #${prNumber}** has been deployed to production: ${deploymentUrl} | |
Built from commit: ${{ github.sha }} | |
--- | |
*Your changes are now live on the production site!*`; | |
await github.rest.issues.createComment({ | |
issue_number: prNumber, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: comment | |
}); |