Sub topic 12
Working with secrets
This chapter assumes you can write a composite action with inputs, outputs, and env-based input handling, as covered in the earlier chapters. It also assumes you know how repository and environment secrets are created in GitHub.
Composite actions cannot read secrets directly
Inside an action.yml, the secrets context is not available. A composite action has no way to say ${{ secrets.API_TOKEN }} and get a value. The action runs in the caller's job, but its definition lives in a different file, and GitHub only lets the calling workflow decide which secrets an action may see.
The fix is to declare an input and let the workflow pass the secret in:
# In the calling workflow
- uses: ./.github/actions/post-status
with:
api-token: ${{ secrets.API_TOKEN }}The secret travels as workflow secret -> with: -> input -> step env -> process. Each hop is a place where it could leak, so the rest of this chapter follows the path in order.
A complete example
This action sends a request to an API with a bearer token. The token is an input, and the step reads it only through an environment variable.
name: Post status
description: Call a status endpoint with a bearer token
inputs:
api-token:
description: Token used to authenticate with the API
required: true
api-url:
description: Base URL of the API
required: false
default: https://api.example.com
runs:
using: composite
steps:
- shell: bash
env:
API_TOKEN: ${{ inputs.api-token }}
API_URL: ${{ inputs.api-url }}
run: |
if [ -z "$API_TOKEN" ]; then
echo "::error::api-token is empty. Pass it with: api-token: \${{ secrets.NAME }}"
exit 1
fi
curl --fail --silent --show-error \
--header "Authorization: Bearer ${API_TOKEN}" \
"${API_URL}/status"Three details matter here.
- The value goes through
env, not into the script text. Writing${{ inputs.api-token }}insiderun:pastes the value into the script before the shell parses it. That is the injection risk covered earlier, and it also puts the secret into the generated script file. Withenv, the shell reads an ordinary variable. - The step checks for an empty value. The runner does not reliably enforce
required: truefor composite action inputs. Treatrequiredas documentation and validate in the script. - The error message does not print the value. It only says what is missing.
How masking works, and where it stops
When a workflow passes ${{ secrets.API_TOKEN }}, the runner knows the value is secret and replaces it with *** in the logs. This protects you from accidents like set -x or a command that echoes its arguments.
Masking is an exact string match on the log output. It does not follow the value through transformations. Each of these produces a new string that the runner does not recognize:
- A base64 encoding of the secret.
- A substring, such as the last four characters.
- A URL-encoded version.
- A value assembled from the secret, such as
user:token.
If your action derives a new sensitive value, register it for masking with the add-mask workflow command before anything prints it:
name: Build basic auth header
description: Derive a basic auth header from a username and token
inputs:
username:
description: Account name
required: true
api-token:
description: Token for the account
required: true
outputs:
header:
description: Authorization header value (masked in logs)
value: ${{ steps.derive.outputs.header }}
runs:
using: composite
steps:
- id: derive
shell: bash
env:
USERNAME: ${{ inputs.username }}
API_TOKEN: ${{ inputs.api-token }}
run: |
encoded=$(printf '%s:%s' "$USERNAME" "$API_TOKEN" | base64 | tr -d '\n')
echo "::add-mask::${encoded}"
echo "header=Basic ${encoded}" >> "$GITHUB_OUTPUT"The mask is added before the value is written anywhere. Note that the output is Basic <encoded>, and only <encoded> is registered. The runner will still replace the sensitive part wherever it appears.
Rules that keep secrets out of trouble
Never echo, even for debugging
Avoid echo "$API_TOKEN" and env or printenv in a step that has the secret in its environment. The exact value would be masked, but a variant would not be. When you debug, print whether the value is set, or its length, not the value:
echo "token length: ${#API_TOKEN}"Scope the secret to the step that needs it
Put env on the one step that uses the secret, not on every step. Environment variables set at step level exist only for that step's process. Steps that don't need the token never see it.
Do not return secrets as outputs
Outputs are written to a file and read by later steps, and they may appear in logs or be passed to other jobs. If a step must produce a credential, such as a short-lived token, mask it first and consider whether the caller needs it at all. Often the action can use it internally and keep it private.
Avoid writing secrets to disk
Tools like docker login, npm, and git can store credentials in files on the runner. On GitHub-hosted runners the machine is discarded after the job, but self-hosted runners may be reused. If you write credentials to a file, remove the file in a final step. Because composite actions have no post-step hook, a failure earlier can skip cleanup unless the cleanup step uses if: always():
- if: ${{ always() }}
shell: bash
run: rm -f "$HOME/.netrc"Pass the built-in token with a default
The automatic GITHUB_TOKEN is available as github.token, and a composite action can use it as a default so callers do not have to wire it:
inputs:
github-token:
description: Token for GitHub API calls
required: false
default: ${{ github.token }}Callers who need broader permissions can override it with a personal access token or a GitHub App token. The permissions of the default are controlled by the workflow's permissions: block, so the action should document what it needs.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Input is an empty string | The secret name is misspelled, or the secret does not exist in that repository or environment | Check the name and the environment the job runs in |
| Input is empty only on pull requests from forks | GitHub does not send most secrets to workflows triggered by fork pull requests | Skip the step when the input is empty, or use a trusted trigger with care |
| `Unrecognized named-value: 'secrets'` in `action.yml` | The action tried to read the `secrets` context | Add an input and pass the secret from the workflow |
| A derived value shows up in logs unmasked | Masking matches exact strings only | Call `::add-mask::` on the derived value before it is printed |
| The secret contains a newline and part of it is visible | Multi-line secrets are masked line by line, and some lines are too short or common | Store the secret as a single line, such as base64, and decode it in the step |
Testing without real secrets
When you test the action in its own repository, use a dummy value for most checks. A fake token is enough to test that the action validates empty values, builds headers, and handles errors. Reserve real secrets for a small number of integration tests, and run those only on trusted events such as pushes to the default branch.
Exercise
A composite action step contains run: curl -H "Authorization: Bearer ${{ inputs.api-token }}" "$API_URL". Name two problems with this, and show the corrected step.
Show answer
First, the input value is pasted into the script text before the shell runs, so a value containing quotes or shell syntax can change the command (script injection), and the secret is embedded in the generated script. Second, the step has no shell: key, which composite run steps require, and it does not check for an empty token. The corrected step is: - shell: bash, then env: with API_TOKEN: ${{ inputs.api-token }} and API_URL: ${{ inputs.api-url }}, then run: | with a check [ -n "$API_TOKEN" ] || { echo "::error::api-token is empty"; exit 1; } followed by curl --fail -H "Authorization: Bearer ${API_TOKEN}" "$API_URL".
Exercise
A step computes short=${API_TOKEN: -4} and writes echo "short=$short" to the log. Why might the last four characters appear in plain text, and how do you prevent it?
Show answer
The runner masks only the exact secret value, and the last four characters are a different string. Prevent it by registering the derived value first with echo "::add-mask::${short}", or by not printing it at all.