cloudflare/pages-action

repository·main·Indexed 19 days ago

https://github.com/cloudflare/pages-action

A GitHub Action for creating Cloudflare Pages deployments using Direct Upload and Wrangler integration. This project is deprecated in favor of wrangler-action. Version 1.5.0 supports configuration for API tokens, account IDs, and project names, and provides deployment outputs such as the deployment ID, URL, alias, and environment.

Tokens
1.6K
Snippets
1
Records
8
Agent score
17%

What's inside pages-action

  1. Retrieve Cloudflare Account ID

    main

    You can find your Cloudflare Account ID using one of these methods:

    1. Cloudflare Dashboard: Log in > Select your zone in Account Home > Look for the Account ID in the Overview section under API on the right-side menu.
    2. URL Method: If you don't have a zone registered, the ID is present in your Pages dashboard URL: https://dash.cloudflare.com/<ACCOUNT_ID>/pages.
  2. Set up Cloudflare Pages deployment with pages-action

    main

    To deploy your project to Cloudflare Pages using this GitHub Action, follow these steps:

    1. Generate an API Token: Create a token in the Cloudflare dashboard with the Cloudflare Pages — Edit permission.
    2. Add GitHub Secret: Add the token to your GitHub repository secrets as CLOUDFLARE_API_TOKEN.
    3. Configure Workflow: Create a .github/workflows/publish.yml file. You must provide apiToken, accountId, projectName, and directory (the build output folder).

    Example Workflow:

    on: [push]
    
    jobs:
      publish:
        runs-on: ubuntu-latest
        permissions:
          contents: read
          deployments: write
        name: Publish to Cloudflare Pages
        steps:
          - name: Checkout
            uses: actions/checkout@v3
    
          # Run your build step here (e.g., npm run build)
    
          - name: Publish to Cloudflare Pages
            uses: cloudflare/pages-action@v1
            with:
              apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
              accountId: YOUR_ACCOUNT_ID
              projectName: YOUR_PROJECT_NAME
              directory: YOUR_BUILD_OUTPUT_DIRECTORY
              # Optional: Enable GitHub Deployments tracking
              gitHubToken: ${{ secrets.GITHUB_TOKEN }}
              # Optional: Specify the branch (determines production vs preview)
              branch: main
              # Optional: Specify directory for monorepos
              workingDirectory: my-site
              # Optional: Specify Wrangler version
              wranglerVersion: '3'
    on: [push]
    
    jobs:
      publish:
        runs-on: ubuntu-latest
        permissions:
          contents: read
          deployments: write
        name: Publish to Cloudflare Pages
        steps:
          - name: Checkout
            uses: actions/checkout@v3
    
          - name: Publish to Cloudflare Pages
            uses: cloudflare/pages-action@v1
            with:
              apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
              accountId: YOUR_ACCOUNT_ID
              projectName: YOUR_PROJECT_NAME
              directory: YOUR_BUILD_OUTPUT_DIRECTORY
              gitHubToken: ${{ secrets.GITHUB_TOKEN }}
              branch: main
              workingDirectory: my-site
              wranglerVersion: '3'
  3. Generate a Cloudflare API Token for Pages

    main

    To authorize the action, generate a custom token with the following steps:

    1. Log in to the Cloudflare dashboard.
    2. Select My Profile from the user icon menu (top right).
    3. Select API Tokens > Create Token.
    4. Under Custom Token, select Get started.
    5. Name your token.
    6. Under Permissions, select:
      • Account > Cloudflare Pages > Edit
    7. Select Continue to summary > Create Token.
  4. Migrate from pages-action to wrangler-action

    main

    [DEPRECATED]

    pages-action is deprecated (latest version v1.5.0). It is highly recommended to migrate to wrangler-action, which provides a unified tool for managing both Workers and Pages.

    Migration considerations:

    • YAML Syntax: The syntax for wrangler-action differs from pages-action. Refer to the wrangler-action Pages deployment instructions.
    • Outputs: wrangler-action does not currently provide the same deployment outputs as pages-action. If your workflows rely on these, you may need temporary adjustments.
    • Deployment Feature: The ability to view past deployments directly in GitHub is not yet available in wrangler-action but is planned for a future release.
  5. Configure Cloudflare Pages GitHub Action inputs

    main

    The Cloudflare Pages GitHub Action requires several inputs to perform a deployment. These inputs are passed via the GitHub Actions workflow YAML file.

    Required Inputs

    • apiToken: Your Cloudflare API Token.
    • accountId: Your Cloudflare Account ID.
    • projectName: The name of your Cloudflare Pages project.
    • directory: The directory containing the assets to deploy (e.g., dist or public).

    Optional Inputs

    • gitHubToken: A GitHub token used to create GitHub Deployments and Deployment Statuses.
    • branch: The branch to publish to. If not provided, it defaults to the branch that triggered the workflow.
    • workingDirectory: The directory in which to run the deployment command.
    • wranglerVersion: The version of wrangler to use for the deployment (e.g., a specific version number or a tag like beta).
  6. Access deployment outputs from pages-action

    main

    When the cloudflare/pages-action completes, it provides the following outputs that can be used in subsequent workflow steps:

    NameDescription
    idThe ID of the pages deployment
    urlThe URL of the pages deployment
    aliasThe alias if it exists; otherwise, the deployment URL
    environmentThe environment that was deployed to
  7. Configure cloudflare/pages-action with options

    main

    The cloudflare/pages-action@v1 action accepts several configuration options in the with block:

    OptionDescription
    apiTokenRequired. Your Cloudflare API token with Cloudflare Pages — Edit permission.
    accountIdRequired. Your Cloudflare Account ID.
    projectNameRequired. The name of your Cloudflare Pages project.
    directoryRequired. The directory containing your build output.
    gitHubTokenOptional. If provided, enables GitHub Deployments tracking.
    branchOptional. The branch name used to determine if the deployment is production or preview. Defaults to the triggering branch.
    workingDirectoryOptional. The directory where Wrangler should run (useful for monorepos).
    wranglerVersionOptional. Specifies the Wrangler version (e.g., '3').
  8. Outputs from the Cloudflare Pages GitHub Action

    main

    After a successful execution, the action provides the following outputs which can be used in subsequent steps of your GitHub workflow:

    • id: The unique ID of the Cloudflare Pages deployment.
    • url: The URL of the deployed site.
    • environment: The environment associated with the deployment (e.g., production or preview).
    • alias: The alias URL for the deployment. For preview deployments, this is typically the first available alias; for production, it is the main deployment URL.