GitHub Action
GitHub Action for Cloudflare Pages
This action deploys your build output to Cloudflare Pages using Wrangler. GitHub Environments and Deployments are used to track these deployments.
When used in context of a pull request, the action will create a deployment for the pull request and add a comment with the URL of the deployment. On closing the pull request, all the deployments for that pull request will be deleted from Cloudflare Pages, GitHub Deployment and the related comment. The action is only able to delete deployments & comments that it created, as it requires a certain payload in a GitHub deployment.
- Deploy to Cloudflare Pages.
- Use GitHub Environments & Deployments.
- Comment on pull requests with deployment URL.
- On pull request close, deletes Cloudflare Pages, GitHub deployments & comments
- Production branch keeps latest 5 deployments.
See the GitHub Workflow examples below or deploy.yml & deploy-delete.yml
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
publish:
permissions:
contents: read
deployments: write
pull-requests: write
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Node.js & pnpm
uses: unlike-ltd/github-actions/[email protected]
with:
node-version: 20.x
- name: Build
run: pnpm run build
- name: Publish to Cloudflare Pages
uses: unlike-ltd/[email protected]
id: pages
with:
cloudflare-api-token: ${{ secrets.CLOUDFLARE_API_TOKEN }}
cloudflare-account-id: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
cloudflare-project-name: ${{ vars.CLOUDFLARE_PROJECT_NAME }}
directory: dist
github-token: ${{ secrets.GITHUB_TOKEN }}
github-environment: ${{ vars.CLOUDFLARE_PROJECT_NAME }} ${{ (github.ref == 'refs/heads/main' && '(Production)') || '(Preview)' }}
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: 'delete deployments'
on:
pull_request:
types:
- closed
branches:
- main
jobs:
delete:
permissions:
contents: read
deployments: write
pull-requests: write
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: 'Delete Cloudflare Pages deployments'
uses: unlike-ltd/[email protected]
with:
cloudflare-api-token: ${{ secrets.CLOUDFLARE_API_TOKEN }}
cloudflare-account-id: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
cloudflare-project-name: ${{ vars.CLOUDFLARE_PROJECT_NAME }}
directory: 'example/dist'
github-token: ${{ secrets.GITHUB_TOKEN }}
github-environment: ${{ vars.CLOUDFLARE_PROJECT_NAME }} ${{ (github.ref == 'refs/heads/main' && '(Production)') || '(Preview)' }}
cloudflare-api-token:
description: 'Cloudflare API Token'
required: true
cloudflare-account-id:
description: 'Cloudflare Account ID'
required: true
cloudflare-project-name:
description: 'Cloudflare Pages project to upload to'
required: true
directory:
description: 'Directory of static files to upload'
required: true
github-token:
description: 'Github API key'
required: true
github-environment:
description: 'GitHub environment to deploy to. You need to manually create this for the github repo'
required: true
id:
description: 'Cloudflare Pages deployed id'
value: ${{ steps.action.outputs.id }}
url:
description: 'Cloudflare Pages deployed url'
value: ${{ steps.action.outputs.url }}
environment:
description: 'Cloudflare Pages deployed environment "production" or "preview"'
value: ${{ steps.action.outputs.environment }}
alias:
description: 'Cloudflare Pages deployed alias. Fallsback to deployed url if deployed alias is null'
value: ${{ steps.action.outputs.alias }}
Deployments are only deleted when the GitHub Action Event triggered is pull_request
and the event payload action is closed
.
It will only delete deployments that it created for that pull request. This is because it requires a certain payload in a GitHub deployment response.
{
"payload": {
"cloudflareId": "1234",
"url": "https://example.com",
"commentId": "1234"
}
}
This flag can be enabled by setting the secret ACTIONS_STEP_DEBUG
to true
.
All actions ran while this secret is enabled will show debug events in the Downloaded Logs and Web Logs.
These log files are enabled by setting the secret ACTIONS_RUNNER_DEBUG
to true
.
All actions ran while this secret is enabled contain additional diagnostic log files in the runner-diagnostic-logs
folder of the log archive.
GitHub Action Variables GitHub Action Default Environment variables