Skip to content
You're viewing an older version of this GitHub Action. Do you want to see the latest version instead?
upload-cloud

GitHub Action

GitHub Action for Cloudflare Pages

v1.1.2

GitHub Action for Cloudflare Pages

upload-cloud

GitHub Action for Cloudflare Pages

Deploy to Cloudflare Pages

Installation

Copy and paste the following snippet into your .yml file.

              

- name: GitHub Action for Cloudflare Pages

uses: andykenward/[email protected]

Learn more about this action in andykenward/github-actions-cloudflare-pages

Choose a version

test Check dist/ release

GitHub Action 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.

Usage

See the GitHub Workflow examples below or deploy.yml & deploy-delete.yml

push & pull_request

# 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: 18.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)' }}

pull_request closed

# 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)' }}

Inputs

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

Outputs

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 }}

Comment Example

pull request comment example

Deleting Deployments

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.

GitHub Deployment payload example response

{
  "payload": {
    "cloudflareId": "1234",
    "url": "https://example.com",
    "commentId": "1234"
  }
}

Debugging

Action Debugging

How to Access Step Debug Logs

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.

How to Access Runner Diagnostic 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.

Docs

GitHub Action Variables GitHub Action Default Environment variables

ESM

TypeScript ESM Node