Sub topic 16
Managing environments with the API and GitHub CLI
This chapter assumes you can read workflow YAML, know what an environment and a protection rule are, and have the GitHub CLI (gh) installed and authenticated. The CLI token needs the repo scope, and you need admin permission on the repository.
Clicking through Settings works for one repository. For many repositories, or for a setup you want to review and repeat, use the REST API. The gh api command sends authenticated requests to it without extra tooling.
Creating or updating an environment
One endpoint handles both cases: PUT /repos/{owner}/{repo}/environments/{environment_name}. If the environment does not exist, GitHub creates it. If it does exist, GitHub updates it.
gh api --method PUT repos/OWNER/REPO/environments/production --input - <<'EOF'
{
"wait_timer": 10,
"prevent_self_review": true,
"reviewers": [{ "type": "User", "id": 12345 }],
"deployment_branch_policy": {
"protected_branches": false,
"custom_branch_policies": true
}
}
EOFThe fields map to the settings you already know:
| Field | Setting |
|---|---|
| `wait_timer` | Minutes to wait before the job starts (0 to 43200). |
| `prevent_self_review` | Blocks the user who triggered the run from approving it. |
| `reviewers` | Up to six users or teams. Each entry has a `type` (`User` or `Team`) and a numeric `id`. |
| `deployment_branch_policy` | Which branches or tags may deploy. |
Replace 12345 with a real ID. You can look one up with gh api users/USERNAME --jq .id. For a team, use gh api orgs/ORG/teams/TEAM-SLUG --jq .id.
Restricting branches
Setting custom_branch_policies to true means no branch is allowed until you add a policy. Add one per allowed name or pattern:
gh api --method POST repos/OWNER/REPO/environments/production/deployment-branch-policies \
-f name=main -f type=branchUse type=tag for tags. List the current policies with a GET on the same path.
Verifying and reading back
Read the result with gh api repos/OWNER/REPO/environments/production. The response lists the environment's protection_rules, so you can check them in a script or a review.
gh api repos/OWNER/REPO/environments/production \
--jq '.protection_rules[].type'Secrets and variables
The same approach works for values. With the CLI you can set them directly:
gh secret set DEPLOY_TOKEN --env production --repo OWNER/REPO
gh variable set REGION --env production --body us-east-1 --repo OWNER/REPOgh secret set prompts for the value, so it stays out of your shell history.
Common errors
- 404 Not Found. Your token lacks access, or the repository does not exist. GitHub returns 404 instead of 403 for resources you may not see.
- 422 Unprocessable Entity. The payload is invalid. Typical causes are a nonexistent reviewer ID or more than six reviewers.
- Rules silently missing. On private repositories, some rules need a paid plan. Check the plan table from the availability chapter.
- Settings reset. A
PUTreplaces the rules it is given. Send the full configuration each time, not only the field you want to change.
Exercise
You run a PUT to update only the wait timer on production, sending {"wait_timer": 30}. What risk do you take, and how do you avoid it?
Show answer
The PUT can overwrite settings you did not include, such as reviewers or branch policy, because the request describes the whole configuration. Send the complete payload, ideally from a version-controlled file, and read the environment back with a GET to confirm.