---
title: Environments
description: The three GitHub Environments, who approves what, every variable and the Terraform output behind it, and the first release.
sidebar:
  icon: layers
---

Each GitHub Environment maps to one AWS account. The OIDC trust in `infrastructure/bootstrap/iam.tf` matches the Environment name exactly, so the names are fixed: **`dev`**, **`staging`**, **`prod`**.

| Environment | AWS account | App deploy | Rollback and unfreeze | Terraform apply |
|---|---|---|---|---|
| `dev` | dev account | Automatic after merge | No approval | No approval |
| `staging` | staging account | Automatic after dev passes | No approval | No approval |
| `prod` | prod account | After a required reviewer approves | Same reviewers | Same reviewers, once for the plan job and once for the apply job |

**Who approves production:** the required reviewers of the `prod` Environment, set in `infrastructure/github/terraform.tfvars` (`reviewer_users`, `reviewer_teams`; a placeholder today). `prevent_self_review` is `false`, so a lone on-call engineer can approve their own rollback. The deploy role trusts only `environment:prod`, so the same reviewers gate deploys, rollbacks and unfreezes; separate rollback reviewers would need a change to the trust policy.

:::warning[GitHub plan requirement]
For private repositories on GitHub Free, Pro and Team, required reviewers and `can_admins_bypass` do nothing; GitHub offers them only for public repositories there. For a private repository, the prod approval gate needs GitHub Enterprise.
:::

## Environments as code

`infrastructure/github/` is a small Terraform stack that uses the `integrations/github` provider. It creates:

- the three Environments, with reviewers, `can_admins_bypass = false`, and a deployment branch policy of `main` only. Without that policy, a workflow on any branch could name an Environment and get its OIDC subject, and with it the deploy and apply roles.
- every Environment and repository variable listed below, from Terraform outputs.
- a ruleset on `main`: pull request with approvals, no force push, no deletion, and `ci-ok` required, pinned to the GitHub Actions app (`integration_id = 15368`), with "branch up to date" on, so the commit CI tested is the commit `deploy.yml` builds.

1. **Fill in the values**

    Replace each `PLACEHOLDER` in `infrastructure/github/terraform.tfvars` with the outputs of `infrastructure/bootstrap` (one workspace for each account) and `infrastructure/envs/<env>`.

2. **Apply with an admin token**

    ```sh
    cd infrastructure/github
    export GITHUB_TOKEN="$(gh auth token)"   # needs admin on the repository
    terraform init && terraform plan -out=tfplan && terraform apply tfplan
    ```
    CI does not run this stack: the workflow token cannot change repository settings. State is local, like bootstrap, and holds no secret values.

3. **Set the one secret by hand**

    ```sh
    gh secret set SLACK_WEBHOOK_URL --env prod   # repeat for dev and staging if wanted
    ```
    Kept out of Terraform so that the webhook URL never lands in state.

## Variables

Environment-specific values are **variables**, not secrets, and none is written in a workflow file.

### Environment variables (one set per Environment)

| Variable | Source | Used by |
|---|---|---|
| `AWS_DEPLOY_ROLE_ARN` | bootstrap output `deploy_role_arn` | `_release.yml`, `unfreeze.yml` |
| `AWS_TERRAFORM_APPLY_ROLE_ARN` | bootstrap output `terraform_apply_role_arn` | `_terraform.yml` |
| `ECR_REPOSITORY_URL` | envs output `ecr_repository_url` | push, task definition image |
| `ECS_CLUSTER` | envs output `ecs_cluster_name` | service update, wait, verify |
| `ECS_SERVICE` | envs output `ecs_service_name` | service update, wait, verify |
| `TASK_FAMILY` | envs output `task_definition_family` | task definition registration |
| `SITE_BUCKET` | envs output `site_bucket_name` | client archive and publish |
| `DISTRIBUTION_ID` | envs output `distribution_id` | invalidation |
| `PUBLIC_URL` | `https://` + `domain_name` in `envs/<env>/terraform.tfvars` | smoke tests, CloudFront check |
| `FREEZE_PARAMETER` | derived: `/patent-<env>/deploy/freeze` | freeze flag (the deploy role may touch `/patent-<env>/deploy/*` only) |

The container name, `server`, is fixed in `modules/service/main.tf` and written once in `_release.yml`.

### Repository variables

Pull-request plan jobs must not name an Environment: the plan role trusts only the subject `repo:OWNER/REPO:pull_request`, which GitHub gives only to pull-request jobs without one. So those jobs cannot read Environment variables, and their settings are repository variables.

| Variable | Source |
|---|---|
| `AWS_REGION` | `aws_region` in `envs/*/terraform.tfvars` (`eu-west-2`) |
| `TF_PLAN_ROLE_ARN_DEV`, `_STAGING`, `_PROD` | bootstrap output `terraform_plan_role_arn` |
| `TF_STATE_BUCKET_DEV`, `_STAGING`, `_PROD` | bootstrap output `state_bucket_name` |
| `TF_STATE_KMS_KEY_ARN_DEV`, `_STAGING`, `_PROD` | bootstrap output `backend_config.kms_key_id` |

### Secrets

| Secret | Scope | Purpose |
|---|---|---|
| `SLACK_WEBHOOK_URL` | Environment, optional | Posts each release result to `#deploys` (placeholder). Without it the step is skipped. A failed post is a warning, not a failed release. |

There are no AWS keys. Every AWS call uses OIDC through `aws-actions/configure-aws-credentials`.

## Which role each job uses

| Job | Environment named | OIDC subject | Role |
|---|---|---|---|
| `_release.yml` release, `unfreeze.yml` | `<env>` (`deployment: false`) | `repo:OWNER/REPO:environment:<env>` | deploy |
| `terraform.yml` PR plan | none | `repo:OWNER/REPO:pull_request` | plan (read-only) |
| `_terraform.yml` plan and apply on `main` | `<env>` | `repo:OWNER/REPO:environment:<env>` | apply. On `main` this is the only role a job can assume; the plan role trusts pull requests only. |
| `ci.yml`, `deploy.yml` build, `rollback.yml` resolve | none | none | no AWS access |

Pull requests from forks never assume a role: `ci.yml` has no AWS step, and the PR plan job runs only when the PR comes from this repository.

## First release of a new environment

The first task definition revision, created by Terraform, points at `<ecr>:bootstrap`, a tag that does not exist, so the service has no healthy task. `_release.yml` handles that case as it stands. It copies that revision's settings, replaces only the image, and waits until a single deployment remains.

1. **Bootstrap and apply the account**

    As in [Deployment](/deployment), steps 1 to 3. Skip the manual image push and client publish there; the pipeline does both.

2. **Set the OpenAI key**

    `aws secretsmanager put-secret-value` as in [Deployment](/deployment). The smoke test makes a real suggestions call, so the release fails without the key.

3. **Apply infrastructure/github**

    So the Environment, its variables and its branch policy exist.

4. **Run deploy.yml**

    Push to `main`, or run `deploy.yml` by hand on `main`. There is no release record yet, so no rollback is possible until this release succeeds.
