Sub topic 4
Passing Secrets to Jobs: Environment Variables, Files, and Command Arguments
This chapter assumes you can read a GitHub Actions workflow, use shell variables, and have already stored a secret in your platform's secret store. Here you choose how the secret reaches the process that needs it.
Three delivery channels
A secret has to cross from the platform into a running program. Each channel exposes it in a different place.
| Channel | Where the value can be seen | Typical use |
|---|---|---|
| Environment variable | The process and its child processes | API tokens, passwords |
| File | The file system, until you delete the file | Private keys, kubeconfig, JSON credentials |
| Command argument | The process list and shell history | Avoid when possible |
Environment variables
Map the secret into an environment variable on the one step that needs it:
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/healthDo not write ${{ secrets.API_TOKEN }} inside the run script. The runner substitutes that expression into the script text before the shell starts. A value that contains a quote or a $(...) sequence can then change what the script does. With env, the shell reads the value as data from a variable.
Set env on the step, not on the job or workflow. Every step in the scope, including third-party actions, can read variables set at a wider level.
Files
Some tools only accept a file path. Write the file with restrictive permissions, point the tool at it, and remove it afterward:
steps:
- name: Query the cluster
env:
KUBECONFIG_DATA: ${{ secrets.KUBECONFIG_DATA }}
run: |
umask 077
export KUBECONFIG="$RUNNER_TEMP/kubeconfig"
trap 'rm -f "$KUBECONFIG"' EXIT
printf '%s' "$KUBECONFIG_DATA" > "$KUBECONFIG"
kubectl get nodesumask 077 makes the file readable only by its owner. RUNNER_TEMP is a directory outside the checked-out workspace, so a later upload-artifact step is less likely to pick the file up by accident. The trap removes the file even if kubectl fails. Hosted runners are destroyed after the job. Self-hosted runners may not be, so cleanup matters there.
Command arguments
Arguments are the weakest channel. Any user on the machine can read them with ps or /proc/<pid>/cmdline. They also appear in shell traces such as set -x, and some tools echo them in error messages.
Many tools can read the secret from standard input instead. The printf shell builtin does not create a new process, so the value never appears in an argument list:
printf '%s' "$REGISTRY_PASSWORD" | docker login ghcr.io -u "$REGISTRY_USER" --password-stdin
printf 'header = "Authorization: Bearer %s"\n' "$API_TOKEN" | curl -sS --config - https://api.example.com/healthCommon errors
- Dumping the environment.
env,printenv, andset -xprint every variable. Masking may hide the exact stored value, but not a transformed copy. - Writing the secret into the workspace. Files in the checkout can end up in artifacts or caches.
- Passing secrets between jobs through outputs. Job outputs are not a secret channel. Fetch the secret again in the job that needs it.
- Reusing one broad variable. Give each step only the secret it uses.
Exercise
A deploy script runs deploy --api-key ${{ secrets.DEPLOY_KEY }}. Name two problems with this line and give a safer form, assuming the tool also reads a DEPLOY_API_KEY environment variable.
Show answer
The expression is substituted into the script text before the shell runs, so a value containing shell syntax can alter the command. The value also becomes a command argument, so it shows up in the process list and in set -x traces. Safer: set env: DEPLOY_API_KEY: ${{ secrets.DEPLOY_KEY }} on that step and run deploy without the argument.