Sub topic 12
Environment variables and secrets in tests
This chapter assumes you can already write a workflow that checks out code, sets up Node.js and runs npm test. It covers how to pass configuration to your tests, and how to pass sensitive values without exposing them.
Variables versus secrets
Tests often need settings such as a database URL, a feature flag or an API key. GitHub Actions gives you two places to store them.
| Kind | Where to define it | Visible in logs | Use for |
|---|---|---|---|
| Variable | Repository settings, under Secrets and variables, Actions, Variables tab | Yes | Non-sensitive settings such as a region or log level |
| Secret | Same page, Secrets tab | Masked as `***` | Tokens, passwords, API keys |
Once you save a secret, you cannot read it back in the UI. You can only replace or delete it.
Setting environment variables
You can set env at three levels. The most specific level wins.
- Workflow level: applies to every job.
- Job level: applies to every step in that job.
- Step level: applies to one step only.
name: Tests
on:
push:
branches: [main]
pull_request:
env:
LOG_LEVEL: info
jobs:
test:
runs-on: ubuntu-latest
env:
NODE_ENV: test
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- name: Run tests
env:
LOG_LEVEL: debug
run: npm testIn this example the tests see LOG_LEVEL=debug, because the step-level value overrides the workflow-level one. Inside your test code, read these like any other environment variable, for example process.env.LOG_LEVEL.
Using secrets and variables
Reference them through contexts. Use secrets.NAME for secrets and vars.NAME for variables.
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- name: Run tests
env:
API_KEY: ${{ secrets.API_KEY }}
API_REGION: ${{ vars.API_REGION }}
run: npm testThe expression ${{ ... }} is replaced before the step runs. Scope the env block to the step that needs the secret. Then other steps, including third-party actions, never receive it.
How the flow works
Pitfalls to avoid
- Pull requests from forks. Workflows triggered by a fork's pull request do not receive your secrets, except for the read-only
GITHUB_TOKEN. Tests that need a real key will fail there. Design those tests to skip or use a fake service when the variable is empty. - Masking is not a guarantee. GitHub hides exact matches of a secret in logs. A transformed value, such as a Base64 version, may not be masked. Never print secrets, and avoid
set -xin steps that use them. - Dependabot runs. Pull requests from Dependabot read from a separate set of Dependabot secrets, not your Actions secrets.
- Quoting. Multi-line secrets, such as a private key, work in
env, but keep them out of command lines.
Safe defaults for test credentials
Use dedicated, low-privilege credentials for CI, such as a test account or a sandbox API key. If a leak happens, the damage is limited and you can rotate the key without affecting production.
Exercise
Your tests read PAYMENT_API_KEY. You stored it as a repository secret with that name. Write the step-level env entry that provides it to npm test, and explain why a pull request from a fork might still fail.
Show answer
Add env: with PAYMENT_API_KEY: ${{ secrets.PAYMENT_API_KEY }} on the step that runs npm test. A fork's pull request does not receive repository secrets, so the variable is empty and tests that need the real key fail unless they skip or use a fake.