blog.dopana

Back

To connect GitHub Actions environment variables and secrets with Cloudflare (Workers, Pages, or Cloudflare API deployments), you configure secrets in your GitHub repository and pass them into the wrangler-action or custom CI/CD workflow steps.

Architecture Flow#

flowchart LR
    subgraph GitHub["GitHub Ecosystem"]
        G_SEC["GitHub Secrets & Vars<br>(CLOUDFLARE_API_TOKEN, DB_PASS, APP_ENV)"]
        G_ACT["GitHub Actions Runner<br>(CI/CD Pipeline)"]
        G_SEC -->|Inject via env / with| G_ACT
    end

    subgraph CF["Cloudflare Platform"]
        WRANGLER["Cloudflare Wrangler / Deploy Action"]
        CF_WORKER["Cloudflare Worker / Pages / Resource"]
        G_ACT -->|Authenticate & Deploy| WRANGLER
        WRANGLER -->|Push Code, Vars & Secrets| CF_WORKER
    end

Step 1: Obtain Cloudflare Credentials#

Here is how to locate and generate both credentials from your Cloudflare dashboard:

1. Get CLOUDFLARE_ACCOUNT_ID#

Your Account ID is a 32-character hexadecimal string tied to your Cloudflare account.

  • Method A: From Workers & Pages (Fastest)
    1. Log in to the Cloudflare Dashboard.
    2. In the left sidebar, click Compute (Workers & Pages) (or Workers & Pages).
    3. Look at the right-hand sidebar under Account details — click to copy your Account ID.
  • Method B: From the URL
    1. When logged into the dashboard, select any domain or section.
    2. Look at your browser’s address bar: https://dash.cloudflare.com/<ACCOUNT_ID>/workers-and-pages
    3. The string immediately following dash.cloudflare.com/ is your Account ID.

2. Generate CLOUDFLARE_API_TOKEN#

Do not use your Global API Key. Create a scoped API Token with minimal necessary permissions.

  1. In the top-right corner of the dashboard, click your User Profile icon -> My Profile.
  2. Select API Tokens from the left navigation.
  3. Click Create Token.
  4. Use a pre-made template or custom setup:
    • Using a Template: Find Edit Cloudflare Workers and click Use template.
    • Custom Token: Select Create Custom Token and configure:
      • Permissions:
        • Account -> Workers Scripts -> Edit
        • Account -> Workers KV Storage -> Edit (if using KV)
        • Account -> Pages -> Edit (if deploying Pages)
      • Account Resources: Include -> All accounts (or select your specific account).
  5. Scroll to the bottom and click Continue to summary -> Create Token.
  6. Copy the token immediately. Cloudflare only displays it once.

Quick Verification via CLI#

You can verify that your credentials work correctly by testing them with curl in your terminal:

curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \
  -H "Authorization: Bearer <YOUR_CLOUDFLARE_API_TOKEN>"
bash

A response of "status": "active" confirms the token is valid and ready to be added to your GitHub Secrets.

Step 2: Configure Secrets in GitHub#

Go to Repository Settings > Secrets and variables > Actions and define:

1. Repository Secrets (Encrypted / Sensitive)#

  • CLOUDFLARE_API_TOKEN: Cloudflare API token with Worker:Edit or Pages:Edit permissions.
  • CLOUDFLARE_ACCOUNT_ID: Your Cloudflare Account ID.
  • APP_SECRET_KEY: Sensitive keys used inside the application runtime.

2. Repository Variables (Plain text / Non-sensitive)#

  • ENVIRONMENT: production or staging.
  • API_BASE_URL: https://api.example.com.

Step 3: GitHub Actions Workflow Configuration#

Create .github/workflows/deploy.yml to pass variables and secrets during deployment:

Step 4: Accessing Variables in Cloudflare#

Depending on whether you are using plaintext variables or encrypted runtime secrets, configure wrangler.jsonc / wrangler.toml or set them directly:

1. Plaintext Variables (wrangler.jsonc)#

Non-sensitive variables can be bound directly in the config file:

wrangler.jsonc
{
  "name": "my-worker",
  "main": "src/index.ts",
  "compatibility_date": "2026-08-01",
  "vars": {
    "ENVIRONMENT": "production",
    "API_BASE_URL": "https://api.example.com"
  }
}
json

2. Runtime Encrypted Secrets (Pushed via Wrangler)#

To upload secrets directly from GitHub Actions to Cloudflare without checking them into source code:

      - name: Push Secrets to Cloudflare Worker
        run: |
          echo "${{ secrets.APP_SECRET_KEY }}" | npx wrangler secret put APP_SECRET_KEY
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
yaml

Comparison of Variable Types#

TypeRecommended StorageAccess Method in Worker
Auth Tokens (CI/CD)GitHub Secretssecrets.CLOUDFLARE_API_TOKEN
Sensitive Runtime ValuesGitHub Secrets -> Cloudflare Secretsenv.APP_SECRET_KEY
Environment FlagsGitHub Variables / wrangler.jsoncenv.ENVIRONMENT

[!TIP] Never hardcode secrets in wrangler.jsonc or commit them to Git. Always rely on encrypted GitHub Secrets and wrangler secret put.

References#