Retrieve the preview/deployment URL of a Cloudflare Pages project or a Cloudflare Worker from the Cloudflare API, filtered by the repository and branch. The URL can then be used for further end-to-end tests, link checks and other PR integrations/actions.
Navigate to the Cloudflare API Tokens page and create a new token with the following permissions:
- Account - Cloudflare Pages - Read
- Include - [your page]
For a Worker, use Account - Workers Scripts - Read instead.
Copy the token and add it to your repository secrets as CLOUDFLARE_API_TOKEN.
If you cannot use an API Token, you can authenticate with a
Global API Key. Set your
Global API Key as CLOUDFLARE_API_TOKEN and also add CLOUDFLARE_ACCOUNT_EMAIL
with your Cloudflare account email. Do not mix these. If you're using an API
Token, leave CLOUDFLARE_ACCOUNT_EMAIL unset.
name: Cloudflare Preview URL
on:
push:
branches:
- '**'
- '!main'
jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
# Optional: Add a sleep action to wait until the deployment is ready
# or use wait_until_ready: true below
- run: sleep 30
- name: Get Cloudflare Preview URL
uses: zentered/cloudflare-preview-url@v1
id: cloudflare_preview_url
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
with:
cloudflare_project_id: 'your-project-name'
wait_until_ready: true
- name: Use Preview URL
run: echo "Preview URL: ${{ steps.cloudflare_preview_url.outputs.preview_url }}"For a Worker that uses
Previews (Workers Builds
or wrangler preview), set cloudflare_worker_name instead of
cloudflare_project_id. The action reads the Preview named after the branch and
returns its latest deployment.
The API token needs the Account - Workers Scripts - Read permission.
- name: Get Worker Preview URL
uses: zentered/cloudflare-preview-url@v1
id: worker_preview_url
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
with:
cloudflare_worker_name: 'your-worker-name'
commit_hash: ${{ github.event.pull_request.head.sha || github.sha }}
wait_until_ready: truepreview_urlis the deployment URL. It always points to this one deployment.branch_urlis the Preview URL. It always points to the latest deployment of the branch.wait_until_ready: truepolls every 5 seconds until the Preview and a matching deployment exist. Settimeout-minuteson the job to bound the wait.environmentdoes not apply to Workers.- If you set a custom Preview name, pass it as
branch.
In your repository, go to "Settings" → "Secrets and variables" → "Actions" and add the following secrets:
CLOUDFLARE_API_TOKEN- Your Cloudflare API token (see API Token setup above)CLOUDFLARE_ACCOUNT_ID- Your Cloudflare Account ID (find it in the URL:https://dash.cloudflare.com/[account-id]/...)
CLOUDFLARE_ACCOUNT_EMAIL- Your Cloudflare account email address (only for Global API Key authentication)
⚠️ Important: Only setCLOUDFLARE_ACCOUNT_EMAILif you are using a Global API Key. When this variable is present, the action switches from Bearer token authentication to API Key authentication. If you setCLOUDFLARE_ACCOUNT_EMAILwhile using an API Token, authentication will fail. If you're using the recommended API Token approach, do not setCLOUDFLARE_ACCOUNT_EMAIL.
| Name | Requirement | Default | Description |
|---|---|---|---|
cloudflare_project_id |
one of | - | Cloudflare Pages project name (found in your Cloudflare Pages dashboard) |
cloudflare_worker_name |
one of | - | Worker name or id. Reads a Worker Preview instead of a Pages deployment |
wait_until_ready |
optional | false |
Wait until the Cloudflare deployment is ready before returning the URL |
environment |
optional | preview |
Filter by deployment environment (preview or production). Set to empty string to include all environments |
commit_hash |
optional | - | Filter deployments by commit hash. Useful when the same branch has multiple deployments |
branch |
optional | auto-detect | Override branch name for filtering. By default, uses the branch from the GitHub context |
A Pages deployment without a repository name in source.config.repo_name, such
as a direct upload from wrangler pages deploy, is matched on branch,
environment and commit hash only. A direct upload to the same project with the
same branch name can be returned. Set commit_hash to pin the result to one
commit.
| Name | Description |
|---|---|
preview_url |
A string with the unique URL of the deployment. Always set when a deployment is found, regardless of deployment status |
branch_url |
The stable branch URL: the Pages branch alias or the Worker Preview URL. Empty when Cloudflare returns none |
The action searches the newest 250 deployments of the project, newest first. It
stops at the first match. If none of them match, it fails with
no matching builds found.
- End-to-end Testing: Use the preview URL to run automated tests against your deployment
- Visual Regression Testing: Integrate with tools like Percy, Chromatic, or BackstopJS
- Link Checking: Validate that all links work in your preview deployment
- Performance Testing: Run Lighthouse or other performance audits
- Notifications: Comment the preview URL on pull requests or send to Slack
This action uses the official Cloudflare TypeScript SDK v7 and is compatible with:
- The GitHub Actions
node24runtime - Cloudflare API v4: Pages deployments and Worker Previews
- Both API Token (recommended) and Global API Key authentication
See CONTRIBUTING.
See LICENSE.