CI / Secrets and trust
CI environments
Production, the preview of each pull request and your own machine each need their own database URL and API keys. A CI environment keeps the secret and variable values of one place under one name, with rules for which runs can use them.
8 min read
A secret has values only in environments. Every repository starts with three:
| Environment | Deploys from | Lowest trust level | Who can read its values |
|---|---|---|---|
production | protected | protected | nobody |
development | **, every branch | trusted | write |
preview | pull_request | trusted | write |
- Deploys from is a list of refs:
protectedmeans the branches whose branch rule requires a pull request. Nobody pushes to them directly, so their code went through a pull request. The default branch counts only when such a rule covers it. A tag never counts.pull_requestmeans the runs of pull requests.tagsmeans the runs of tag pushes. Any person with write access can push a tag of any commit, so addtagsonly where that is acceptable. A tag run is at mosttrusted, so an environment withtagsneeds the lowest trust leveltrusted. The forge refusestagswithprotected.- Anything else is a branch pattern, like
mainorrelease/**. A pattern takes every push to a branch that it matches, so pair it with a branch rule that requires a pull request.
- Lowest trust level is the trust level that a run needs.
- Who can read its values is
nobody,write(people with write access) oradmin. It applies to its secret values and its variable values. The readers of production are alwaysnobody: its values are write-only for everyone, admins too.
Important
Production takes no run until a branch rule that requires a pull request covers the branch that you deploy from. Only a run of such a branch is protected. For example, src ci rule set main --require-pull.
A CI job that names an environment gets its values, when its run passes the rules of the environment. A job that declares secrets and names no environment uses development. Only the readers of the environment read a value back, and a job never does. The audit trail records each secret value read, with who read it, or the job that it was for. Admins read the trail with src secret audit, or under Activity in the Secrets section of Settings.
1. Look at the environments
People with write access can list them.
src env list
development ** trusted write
preview pull_request trusted write
production protected protected nobody
2. Change one, or add your own
Only admins change, add and delete environments. src env set changes only the settings that you give. A new environment gets the strictest settings: protected, protected and nobody.
src env set canary --ref 'canary/**' --ref main --min-trust protected --readers nobody
src env set canary --ref main --ref 'release/**'
src env set production --ref protected --ref tags --min-trust trusted
src env delete canary
Deleting an environment deletes its secret and variable values too.
On the web, open Settings, then Secrets under Code, then the … menu next to Save. Click Environment settings or Add an environment. In Deploys from, pick each ref from the list:
- Protected branches, Pull requests or Tags.
- A branch of the repository. Type part of its name to find it.
- A branch pattern, like
release/**. Type it and press Enter.
Warning
A change that loosens a write-only environment, like production, deletes its secret values. See When a change deletes the values.
3. Give secrets and variables a value for each environment
Add --env to src secret set and src variable set. A secret needs --env: it has no value outside of an environment. Its bound hosts and its header, if it has them, are the same in every environment.
src secret set DATABASE_URL --env production < prod-url.txt
src secret set DATABASE_URL --env preview < preview-url.txt
src variable set APP_URL --env production --body https://example.com
src variable set APP_URL --env preview --body https://preview.example.com
src secret listandsrc variable listadd a column with the environments that have a value.src secret list --env productionlists the secrets with a value there.src variable list --env previewlists the variable values of preview, for the readers of preview only.src secret delete DATABASE_URL --env previewandsrc variable delete APP_URL --env previewdelete the value of one environment. Without--env, they delete the secret or the variable with all its values.
On the web, admins do the same in Settings, then Secrets under Code (/@<owner>/<repo>/settings/secrets). Pick an environment next to Secrets to see and set its values. Use the … menu next to Save to change, add or delete environments. The variable form, in Settings, then Variables under Code (/@<owner>/<repo>/settings/variables), does the same for variables.

4. Deploy from a job
Name the environment in the job:
.job('deploy', { environment: 'production', secrets: { DATABASE_URL: {} }, vars: ['APP_URL'], egress: ['db.example.com'], steps })
- The job runs only when its run passes the rules of the environment: its ref is one of the refs of the environment, and its trust level is at least the lowest trust level. Otherwise it is skipped, and the run page says why.
- A pull request run passes only
pull_request. Other runs passprotectedon a branch whose rule requires a pull request. They passtagson a tag, and a branch pattern on a branch that it matches. - When a run does not pass
protected, the reason says what an admin can do: add a branch rule that requires a pull request, or addtags. - Each secret takes its value in the environment. Each variable takes its value in the environment, and where the environment has none, its plain value.
- The forge checks the rules again, with the settings of the environment at that time, when the job first needs a secret value.
- An admin can connect an environment to Cloudflare on the web. The connection names the Worker, and gives the protected deploy jobs of the environment
CLOUDFLARE_API_TOKENandCLOUDFLARE_ACCOUNT_ID, with no token to save. See Connect Cloudflare.
A job that declares a secret and names no environment uses the development environment. Its secrets and variables take their values there, and its run must pass the rules of development. Some runs cannot use development: by default, a pull request run and a tag run. In such a run, a job whose secrets are all optional runs without them. A job with a required secret is skipped. A pull request job takes its environment from the base branch. Use a secret in a job has more.
Done
The deploy job gets the production DATABASE_URL and APP_URL, and a preview job gets the preview values, under the same names. To read the values of development or preview on your machine, see Get the values on your machine.
When a change deletes the values
A write-only environment (readers nobody, like production) keeps its secret values only under the rules that they were set under. When you loosen one, the forge deletes its secret values in the same change, and you enter them again. It deletes its Cloudflare connection too. Loosening is any of these:
- Changing its readers.
- Lowering its minimum trust level.
- Adding a ref, or replacing a ref with another.
- Naming a Worker that it did not name before: another account or script, or one where there was none.
Removing a ref, raising the trust level and clearing the Worker keep the values. For a write-only preview, its branch values go too. src env set and the settings page name the secrets whose values went, and src secret audit shows each delete. So nobody can loosen production to read its values, or to hand them to a run that they control. This includes an admin, and someone with the session or the token of an admin.
Rotate the key of an environment
The values of each environment are encrypted under a key of their own. Give the environment a new key:
src secret rotate-key --env production
- The forge encrypts every value of the environment again under the new key, in batches. When no value is under the old key, the forge deletes it.
- The values stay the same, so nothing in your jobs changes.
- Run it when your policy asks for a key rotation, or after a reader of the environment leaves.
- Admins only, and each rotation is in the vault audit.
Reference
- A token with repositories reads secret values only when it holds
secrets:read, which the Write preset of the tokens page leaves out. - A secret can also have a
previewvalue for one branch, which a token withsecrets:previewsets: see Set preview secrets from an integration. - A repository keeps at most 20 environments, and an environment names at most 20 refs. A name has lowercase letters, digits,
-and_, likeproduction.
REST and MCP:
| Task | REST | MCP |
|---|---|---|
| List the environments | GET /ci/environments | list_ci_environments |
| Change or add one | PUT /ci/environments/<name> ({ "refs", "minTrust", "readers", "worker" }, where worker is { "accountId", "script" } or null) | set_ci_environment |
| Delete one | DELETE /ci/environments/<name> | delete_ci_environment |
| Read its values | GET /ci/environments/<name>/values | pull_ci_environment |
| Rotate its key | POST /ci/environments/<name>/rotate-key | rotate_ci_environment_key |
| Read one secret | GET /ci/secrets/<name>?environment=<name> | get_ci_secret |
set_ci_secret takes environments, and the variable tools take environment. To read only some keys, see Read only some keys from a script or an agent. The CI reference has each route.