Sub topic 3
Using your CI platform's secret store
This chapter assumes you can write a basic GitHub Actions workflow, know what an environment variable is, and understand what an API token is. The examples use GitHub Actions. GitLab CI, CircleCI, and other platforms have similar features under different names.
What a platform-native secret store does
A platform-native secret store is an encrypted key-value store that your CI system manages for you. You save a value once through the web interface or API. The platform encrypts it at rest and hands it to a job only when that job runs. After you save a value, you can't read it back through the interface. You can only replace or delete it.
The store gives you three things without extra infrastructure:
- Encryption at rest and write-only access through the UI.
- Automatic masking of the exact secret value in job logs.
- Withholding from workflows triggered by forks, in most default configurations.
Where secrets can be stored
GitHub Actions offers three levels. Choose the narrowest one that works.
| Level | Visible to | Typical use |
|---|---|---|
| Repository secret | Every workflow in one repository | A token used by all jobs in a project |
| Environment secret | Only jobs that target that environment | A production deploy key |
| Organization secret | Selected repositories in the organization | A shared registry token |
Environment secrets are the most useful for limiting exposure. An environment can also require a manual approval before any job gets access to its secrets. The next chapters cover scoping in more detail.
Reading a secret in a workflow
You read a secret through the secrets context. The safest pattern is to map it into an environment variable for one step only, then refer to the variable from the shell.
jobs:
call-api:
runs-on: ubuntu-latest
steps:
- name: Check API health
env:
API_TOKEN: ${{ secrets.API_TOKEN }}
run: curl -sS -H "Authorization: Bearer $API_TOKEN" https://api.example.com/healthThe env block is scoped to this step. Other steps in the job never see API_TOKEN.
Avoid writing ${{ secrets.API_TOKEN }} directly inside the run script. The platform substitutes the value into the script text before the shell starts. A value containing quotes or shell syntax can then break or alter the command. Passing it through env keeps the value as data.
How a secret travels through a run
Common errors
- Using a secret in a step condition. The
secretscontext is not available inif:expressions. Copy the secret intoenvfirst, or use a job-levelenvflag. - Wrong name or scope. A missing secret evaluates to an empty string and does not raise an error. If a job fails with an authentication error, print whether the variable is set (
[ -n "$API_TOKEN" ]) without printing its value. - Multi-line values. Private keys and JSON credentials can break when the value is pasted with different line endings. Store them exactly, and write them to a file with restricted permissions only when a tool requires a file.
- Structured secrets. Platform masking matches the exact stored string. If you save a JSON blob and the job uses one field from it, that field may not be masked. Store each sensitive field as its own secret.
- Treating the store as a vault. The store has limited audit detail and no automatic rotation. It suits a modest number of static values. Later chapters cover stronger options.
Exercise
A workflow step contains run: curl -H "Authorization: Bearer ${{ secrets.API_TOKEN }}" https://api.example.com. Name one reason to change it, and show the change.
Show answer
The platform pastes the secret into the script text before the shell runs, so a value with quotes or shell syntax can break or alter the command. Map it through env instead: add env: API_TOKEN: ${{ secrets.API_TOKEN }} to the step and use run: curl -H "Authorization: Bearer $API_TOKEN" https://api.example.com.
Exercise
You need a production deploy token that only the deploy job can use, and that requires a human approval before use. Which secret level do you choose and which setting do you add?
Show answer
Use an environment secret on a production environment, and configure required reviewers on that environment. The deploy job must declare environment: production to receive the secret.