logo
0
0
WeChat Login

Deploy Cloudflare Workers/Pages Plugin

pipeline crontab

Deploy Cloudflare Workers and Pages projects using the Wrangler CLI.

Prerequisites

  • A Cloudflare account
  • A Cloudflare API Token
  • Your Cloudflare Account ID (found in the Cloudflare Dashboard)
  • For Workers: a wrangler.toml config file or an existing Worker
  • For Pages: a Pages project created in the Cloudflare Dashboard (Direct Upload type)

Secret Repository Configuration

CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID are sensitive credentials. It's recommended to store them in a CNB Secret Repository and inject them into pipeline environment variables via imports.

1. Create a Secret Repository

Go to the CNB platform Create Repository page and select Secret Repository as the repository type.

2. Add a Secret File

Create an envs.yml file in the secret repository via the web interface with the following content:

CLOUDFLARE_API_TOKEN: your_api_token_here
CLOUDFLARE_ACCOUNT_ID: your_account_id_here

# Access control (recommended)
allow_slugs:
  - 'your-org/**'
# allow_events:
#   - push
#   - tag_push
# allow_branches:
#   - main

Access Control Reference:

FieldDescriptionExample
allow_slugsRestrict which repositories can referenceyour-org/** (all repos under the org)
allow_eventsRestrict which event types can referencepush, tag_push, tag_deploy.*
allow_branchesRestrict which branches can referencemain, v*

Once allow_* rules are configured, the system will validate access entirely based on these rules. See File Reference Authorization for details.

Usage

This plugin supports two modes: Settings mode (simple configuration) and Script mode (flexible customization). Authentication is handled exclusively via imports from a secret repository — no credentials should be configured in settings.

⚠️ Security Notice: CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID must only be injected via imports as environment variables. Passing them via settings is not allowed.

Mode 1: Settings Mode

Pass deployment parameters via settings, and the plugin will automatically build and execute the wrangler command. Authentication is provided by environment variables injected via imports.

ParameterRequiredDescription
deploy_typeNoDeployment type: workers (default) or pages
working_directoryNoWorking directory path
extra_argsNoAdditional command-line arguments

Deploy Cloudflare Pages

main:
  push:
    - imports:
        - https://cnb.cool/<org>/<secret-repo>/-/blob/main/envs.yml
      stages:
        - name: build
          image: node:20-alpine
          script: npm install && npm run build
        - name: deploy to cloudflare pages
          image: docker.cnb.cool/wwvo/deploy-cloudflare:latest
          settings:
            deploy_type: pages
            project_name: my-pages-project
            directory: ./dist
            branch: main
ParameterRequiredDescription
directoryYesStatic assets directory path
project_nameYesPages project name
branchNoDeployment branch name

Deploy Cloudflare Workers

main:
  push:
    - imports:
        - https://cnb.cool/<org>/<secret-repo>/-/blob/main/envs.yml
      stages:
        - name: deploy worker
          image: docker.cnb.cool/wwvo/deploy-cloudflare:latest
          settings:
            deploy_type: workers
            worker_name: my-worker
ParameterRequiredDescription
worker_nameNoWorker name (overrides wrangler.toml)

Mode 2: Script Mode

Write wrangler commands directly in the script block for complex or custom deployment scenarios.

Deploy Cloudflare Pages

main:
  push:
    - imports:
        - https://cnb.cool/<org>/<secret-repo>/-/blob/main/envs.yml
      stages:
        - name: build
          image: node:20-alpine
          script: npm install && npm run build
        - name: deploy to cloudflare pages
          image: docker.cnb.cool/wwvo/deploy-cloudflare:latest
          script: wrangler pages deploy ./dist --project-name=my-project

Deploy Cloudflare Workers

main:
  push:
    - imports:
        - https://cnb.cool/<org>/<secret-repo>/-/blob/main/envs.yml
      stages:
        - name: deploy worker
          image: docker.cnb.cool/wwvo/deploy-cloudflare:latest
          script: wrangler deploy

Multi-Environment Deployment

Cloudflare Workers/Pages supports multi-environment deployment, which can be combined with CNB's custom deployment feature by configuring environments in .cnb/tag_deploy.

environments:
  - name: staging
    description: Staging environment deployment
  - name: production
    description: Production environment deployment
    require:
      - environmentName: staging
        after: 60

Staging Environment Deployment

Triggered by tag_deploy.staging:

tag_deploy.staging:
  - imports:
      - https://cnb.cool/<org>/<secret-repo>/-/blob/main/envs.yml
    stages:
      - name: build
        image: node:20-alpine
        script: npm install && npm run build
      - name: Deploy staging environment
        image: docker.cnb.cool/wwvo/deploy-cloudflare:latest
        script: |
          wrangler pages deploy ./dist \
            -n my-project \
            --branch=preview

Production Environment Deployment

Triggered by tag_deploy.production:

tag_deploy.production:
  - imports:
      - https://cnb.cool/<org>/<secret-repo>/-/blob/main/envs.yml
    stages:
      - name: build
        image: node:20-alpine
        script: npm install && npm run build
      - name: Deploy production environment
        image: docker.cnb.cool/wwvo/deploy-cloudflare:latest
        script: |
          wrangler pages deploy ./dist \
            -n my-project

Architecture

Reference Documentation

About

使用 Wrangler CLI 部署 Cloudflare Workers/Pages

wranglercloudflareworkerspages
127.00 KiB
0 forks0 stars1 branches0 TagREADMEUnlicense license
Language
Shell91.1%
Dockerfile8.9%