Sub topic 12
Working with secrets
This chapter assumes you can write an action.yml with inputs and composite steps, and that you know how env passes values to a run step. It shows how a composite action can use a secret without leaking it.
Composite actions cannot read the secrets context
A step inside a composite action cannot use ${{ secrets.NAME }}. The secrets context is only available in workflow files. The calling workflow has to pass the secret in as an input. The action then reads it through inputs.
This keeps the action honest. Its action.yml lists everything it needs, and the caller decides which secret to hand over.
Declare the secret as an input
Mark the input as required, and describe it as a credential. For a GitHub token, you can default to the token that the runner already provides.
name: Label pull request
description: Add a label to the current pull request
inputs:
token:
description: GitHub token with permission to edit pull requests
required: false
default: ${{ github.token }}
label:
description: Label to add
required: true
runs:
using: composite
steps:
- shell: bash
env:
GH_TOKEN: ${{ inputs.token }}
LABEL: ${{ inputs.label }}
PR_URL: ${{ github.event.pull_request.html_url }}
run: gh pr edit "$PR_URL" --add-label "$LABEL"The token reaches the script as the environment variable GH_TOKEN, which the gh CLI reads on its own. It is never written into the script text. This is the same env pattern you used to avoid script injection.
Pass the secret from the workflow
The caller reads the secret from its own secrets context and sends it through with.
name: Label
on:
pull_request:
jobs:
label:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/label-pr
with:
label: needs-review
token: ${{ secrets.LABEL_TOKEN }}If the caller leaves out token, the default github.token is used, and its rights come from the job's permissions block.
Masking and its limits
The runner replaces the exact text of a registered secret with *** in the logs. That protection reaches into composite steps, so a secret passed as an input stays masked there too.
Masking only matches the exact value. A transformed copy, such as a base64 string or one piece of a JSON secret, is not masked. Register derived values yourself with the add-mask workflow command, before anything prints them.
runs:
using: composite
steps:
- shell: bash
env:
RAW: ${{ inputs.credentials }}
run: |
ENCODED="$(printf '%s' "$RAW" | base64 -w0)"
echo "::add-mask::$ENCODED"
echo "Prepared credentials"Habits that keep secrets safe
- Pass secrets through
env, not by writing${{ inputs.token }}insiderun. - Never print a secret, and never run
set -xin a step that handles one. - Avoid putting secrets in action outputs. A job output that holds a secret may be dropped by the runner, and anything you write to a file can end up in an uploaded artifact.
- Give the token the least access that works, and set
permissionson the job. - Do not pass secrets to third-party actions that you have not pinned to a commit SHA.
Exercise
A composite action step contains run: curl -H "Authorization: Bearer ${{ secrets.API_KEY }}" .... Why does this fail, and how do you fix it?
Show answer
The secrets context is not available inside composite actions. Add an api-key input to action.yml, have the workflow pass api-key: ${{ secrets.API_KEY }}, and in the step set env: API_KEY: ${{ inputs.api-key }}, then use "Authorization: Bearer $API_KEY" in the script.