Sub topic 13
Using secrets safely
This chapter assumes you can read a workflow file and know how env works at the workflow, job, and step level. Secrets are encrypted values stored outside your repository. A workflow can read them, but they never appear in the YAML.
Where secrets live
You create secrets in the repository settings under Secrets and variables > Actions. You can also create them at the organization level, or inside an environment such as production. An environment secret is available only to jobs that name that environment. This lets you attach approval rules to the most sensitive values.
A workflow reads a secret through the secrets context. Names are case-insensitive, and by convention they are written in uppercase.
${{ secrets.API_TOKEN }}Pass a secret to one step
Expose a secret only to the step that needs it. Set it in that step's env block, then read it as a normal shell variable.
name: Deploy
on:
push:
branches: [main]
permissions:
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Deploy
env:
API_TOKEN: ${{ secrets.API_TOKEN }}
run: ./scripts/deploy.shSetting the secret at the workflow level would hand it to every step in every job, including third-party actions. Step-level scope keeps the exposure small.
The value is also safer in env than inline. Writing ${{ secrets.API_TOKEN }} directly inside a run script pastes the value into the script text before the shell starts. Using $API_TOKEN avoids that, and it avoids quoting problems if the value contains special characters.
How masking works
The runner scans log output and replaces any exact match of a registered secret with ***. Masking is a safety net, not a guarantee. It fails when the value is transformed before printing, for example when it is base64-encoded, split across lines, or changed by a command. A derived value is not masked unless you register it.
- run: |
echo "::add-mask::$DERIVED"
env:
DERIVED: ${{ steps.meta.outputs.value }}Do not print secrets to debug a failure.
Common errors
- Empty secret on a pull request from a fork. Workflows triggered by forked pull requests do not receive repository secrets. The value is an empty string, so the step fails with a confusing message. This is intentional protection.
- Secret in a job
if. Thesecretscontext is not available inifconditions. Copy it to anenvvariable first, or test for it in a step. - Secret passed to a reusable workflow. Secrets are not inherited automatically. Declare them under
on.workflow_call.secrets, then pass them withsecrets:orsecrets: inheritin the caller. - Untrusted input in
run. Interpolating values such as a pull request title into a script allows command injection. Pass them throughenvinstead.
If a secret is ever printed or committed, rotate it right away. Deleting the log does not make the value safe again.
Exercise
Your deploy step needs API_TOKEN, but a colleague set env: API_TOKEN: ${{ secrets.API_TOKEN }} at the top of the workflow. Why is moving it to the step better, and why is $API_TOKEN in the script safer than inlining ${{ secrets.API_TOKEN }}?
Show answer
At workflow level, every step in every job receives the secret, including third-party actions that do not need it. At step level only that step gets it. Using $API_TOKEN makes the shell read an environment variable. Inlining pastes the raw value into the script text before it runs, which risks quoting bugs and exposure.