#env-var #cloudflare #variables #pages #projects #environment #command-line-tool

app cf-pages-cli

A simple command line utility for managing Cloudflare Pages projects

2 unstable releases

0.2.1 Apr 25, 2023
0.2.0 Apr 25, 2023
0.1.0 Apr 25, 2023

#22 in #pages

MIT/Apache

20KB
427 lines

cf-pages-cli

Command line utility for managing Cloudflare Pages projects

crates-badge

What is cf-pages-cli

cf-pages-cli is a line utility for managing Cloudflare Pages projects. Currently, its only capability is managing environment variables, which is useful in CI/CD workflows to synchronize projects with variables stored in Git.

Installation

With cargo installed, install from crates.io:

cargo install --locked --version 0.2.1 cf-pages-cli

Usage

Synchronize variables

First, make sure you have your Cloudflare account ID, as well as a valid Cloudflare API token (with the Cloudflare Pages:Edit permission). Export them as environment variables:

$ export CLOUDFLARE_ACCOUNT="YOUR_ACCOUNT_ID"
$ export CLOUDFLARE_TOKEN="YOUR_API_TOKEN"

(It's also possible to use them as command line options via --account and --token, respectively, but it's easier to just export them as they're used in many commands.)

By default, the command exports the latest settings for both the production and preview environments. You can also export the variables from a specific deployment by adding a --deployment DEPLOYMENT_ID option. Note that since each deployment only targets one environment, the other environment will be left as null in the resulting JSON file.

Then, export the environment variables of your project:

$ cf-pages get-env-vars --project YOUR_PROJECT_NAME --output ./vars.json
Environment variables written to: ./vars.json

(It's also possible to set the project name and file path via the CF_PAGES_PROJECT and CF_PAGES_OUTPUT environment variables, respectively.)

You can also print the generated file content to stdout by omitting the --output option.

Now, make changes to the vars.json file, and upload to Cloudflare:

$ cf-pages set-env-vars --project YOUR_PROJECT_NAME --file ./vars.json
Environment variables successfully updated

Generate .env files

The vars.json file exported with the get-env-vars can also be used to generate .env files, which are useful for front-end development:

$ cf-pages to-env-file --output ./.env ./vars.json
Environment variables written to: ./.env

You can also print the generated file content to stdout by omitting the --output option.

By default, environment variables for the production environment are exported. To export the preview environment instead, add the --environment preview option.

License

Licensed under either of

at your option.

Dependencies

~5–18MB
~233K SLoC