Sub topic 4
Secrets and access per environment
This chapter assumes you have read the earlier chapters on building one immutable artifact and keeping configuration outside it. It also assumes you can read a GitHub Actions workflow and know what a Kubernetes namespace is.
Why secrets are not part of the build
A secret is a value that grants access, such as a database password, an API key or a deploy token. The pipeline builds one image and promotes it unchanged. That image is therefore pulled by every environment and sits in a registry that many people can read. A secret baked into it would be visible to all of them. It would also be identical in staging and production, which defeats the point of having separate environments.
The rule is that the artifact contains no secrets. Each environment supplies its own at deploy time or at runtime.
Two kinds of secrets
The pipeline needs two kinds of secrets, and they are easy to confuse.
- Application secrets are used by the running service, for example the database password. They live in the environment's own store, such as Kubernetes Secrets or a cloud secrets manager, and the deployment references them by name.
- Deployment credentials are used by the pipeline to reach an environment, for example a cluster token. They live in the CI system and are released only to jobs that deploy to that environment.
The two stores must be separate for each environment. Staging and production should have different values and different owners of those values.
Same name, different value
GitHub Actions supports environment-scoped secrets. You define a secret called KUBE_TOKEN once under the staging environment and once under the production environment, with different values. A job receives the value that belongs to the environment it declares.
jobs:
deploy-staging:
needs: build
runs-on: ubuntu-latest
environment: staging
steps:
- uses: actions/checkout@v4
- run: ./deploy.sh staging "${{ needs.build.outputs.image }}"
env:
KUBE_TOKEN: ${{ secrets.KUBE_TOKEN }}
deploy-production:
needs: [build, deploy-staging]
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- run: ./deploy.sh production "${{ needs.build.outputs.image }}"
env:
KUBE_TOKEN: ${{ secrets.KUBE_TOKEN }}The two jobs are identical except for the environment name. The deploy.sh script from the previous chapter reads KUBE_TOKEN from its environment and passes it to kubectl --token. The workflow file never changes between environments, and only the secret store differs.
The build job declares no environment, so it receives no deployment credentials. A compromised build step cannot reach the cluster.
How access is enforced
Three controls work together.
- Scoping. An environment secret is released only to a job that names that environment.
- Protection rules. You can require reviewers on
production. The job then waits, and its secrets are not exposed until someone approves. You can also limit which branches may deploy there, such asmainonly. - Least privilege on the target. The staging token should be valid only for the staging namespace. Production's token should be allowed only the actions a deploy needs, such as updating Deployments. A leaked staging token then cannot touch production.
Where your cloud supports it, prefer short-lived credentials over stored keys. With OpenID Connect (OIDC), the job proves its identity to the cloud provider and receives a temporary token. The cloud role can be tied to one environment. Nothing long-lived is stored in the CI system.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Passing a secret as a Docker build argument | The value stays in image layers and metadata | Inject it at runtime |
| Using one credential for every environment | A staging leak exposes production | Issue one credential per environment |
| Defining production secrets at repository level | Any workflow, including pull requests from branches, may read them | Use environment secrets with protection rules |
| Printing a secret to debug a deploy | The value lands in logs | Rely on log masking and never `echo` secrets |
| Staging pointing at the production database | Tests can modify real data | Give staging its own data store and connection string |
Log masking only hides exact matches. A secret that has been transformed, for example base64-encoded, may appear in the log unmasked.
Exercise
Your deploy-staging job succeeds, but deploy-production fails with an authentication error, even though both jobs run the same script. Name two likely causes related to this chapter.
Show answer
First, the KUBE_TOKEN secret may be missing or expired in the production environment, because each environment holds its own value. Second, the job may not declare environment: production, or a protection rule such as a branch restriction may be blocking the secret from being released. A third possibility is that the production token lacks permission in the production namespace.