GitHub Actions makes it easy to automate builds, tests, deployments, and other development tasks. A workflow can access API keys, cloud credentials, database passwords, package tokens, certificates, and other sensitive values through GitHub Secrets.
The problem starts when one of those values ends up in a workflow log.
A secret does not have to be committed to the repository to become a security problem. It can leak through a simple echo command, debug output, a failed deployment, a test framework, a third-party action, or even an environment dump.
Once a credential appears in a CI log, treating the log as harmless output is a mistake. Anyone who can access that log may be able to use the credential, and the same credential may also appear in copied logs, screenshots, artifacts, or external monitoring systems.
This article explains how secrets get exposed in GitHub Actions logs and how to design workflows so sensitive values stay out of normal build output.
Why Secrets End Up in GitHub Actions Logs
The most obvious example is printing a secret directly.
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Print token
run: echo "$API_TOKEN"
env:
API_TOKEN: ${{ secrets.API_TOKEN }}The workflow does not need to print the value for the deployment to work. The output only creates an unnecessary exposure.
A more realistic problem can happen indirectly.
For example, a deployment command may fail and include its arguments in the error message. A diagnostic command may print the entire environment. A test may dump configuration values when it fails.
The secret might therefore appear in the log even though nobody intentionally wrote an echo statement.
Common sources include:
echo and printf commands
shell tracing
environment dumps
failed commands
package manager debugging
test framework output
third-party actions
deployment tools
generated configuration files
uploaded artifacts
debug logging
application startup messages
stack traces
command-line arguments
The important rule is simple: a secret should never need to appear in a workflow log.
GitHub Secrets Are Not a License to Print Credentials
A secret can be stored safely in the repository or organization configuration and still be exposed during execution.
For example:
env:
DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}This is a normal way to make a secret available to a step.
The problem is what happens next.
run: |
echo "Starting database migration"
echo "Password: $DATABASE_PASSWORD"The first line is useful.
The second line serves no operational purpose and should not exist.
A good workflow treats secrets as input to a process, not as information that should be displayed.
Use Environment Variables for Secret Values
Passing secrets through environment variables is usually easier to control than embedding them directly into scripts.
For example:
steps:
- name: Deploy application
env:
API_TOKEN: ${{ secrets.API_TOKEN }}
run: ./deploy.shThe deployment script can then read the environment variable when required.
#!/usr/bin/env bash
set -e
./deployment-tool --token "$API_TOKEN"Even here, the deployment tool must be trusted not to print the token.
The workflow is responsible for keeping the secret out of its own output, but external tools and scripts also need to be checked.
Never Dump the Environment
One of the easiest ways to accidentally expose multiple secrets is to print the environment.
Avoid commands such as:
envor:
printenvor:
setwhen sensitive values are available as environment variables.
A debugging step like this may look harmless:
- name: Debug environment
run: envBut if the job contains several credentials, tokens, or passwords, the entire environment becomes part of the workflow log.
A better approach is to print only the values that are safe to inspect.
- name: Debug environment
run: |
echo "Node version:"
node --version
echo "Working directory:"
pwd
echo "Repository:"
echo "$GITHUB_REPOSITORY"When debugging CI failures, identify the specific information you need instead of dumping everything.
Be Careful With Shell Debugging
Shell tracing is another common source of accidental exposure.
For example:
set -x
curl -H "Authorization: Bearer $API_TOKEN" \
https://example.internal/api
set +xShell tracing can cause commands to be displayed while they execute. That can make debugging easier, but it can also expose sensitive command arguments.
Do not enable tracing around commands that contain secrets.
If debugging is required, isolate the sensitive operation:
set -x
echo "Starting deployment"
set +x
./deploy.shThe exact behavior depends on the shell and tooling being used, so sensitive commands should always be reviewed rather than assuming masking will make tracing safe.
Do Not Put Secrets Directly Into Workflow Files
This is obviously unsafe:
env:
API_TOKEN: "my-real-token"The value is now part of the repository configuration and can be exposed through source control, pull requests, code review, repository history, or forks.
Use GitHub Secrets instead:
env:
API_TOKEN: ${{ secrets.API_TOKEN }}The workflow file should describe how a credential is used, not contain the credential itself.
Secret Masking Is Not a Complete Security Boundary
GitHub Actions provides masking for secrets that appear in logs. This is an important protection, but it should not change how workflows are designed.
Developers sometimes assume this is safe:
- name: Debug token
run: echo "$API_TOKEN"
env:
API_TOKEN: ${{ secrets.API_TOKEN }}The reasoning is that GitHub will mask the value.
That is the wrong security model.
Masking should be considered a last line of defense, not permission to print credentials.
There are also cases where a transformed or derived value may not be recognized as the original secret.
For example, a workflow might transform a value:
ENCODED_TOKEN=$(printf '%s' "$API_TOKEN" | base64)
echo "$ENCODED_TOKEN"The encoded value is not necessarily treated the same way as the original secret.
The same concern applies to:
substrings
hashes
encoded values
transformed credentials
generated connection strings
values combined with other strings
values returned by external tools
The safest approach is to never intentionally write secrets to logs.
Use add-mask for Dynamically Generated Sensitive Values
Not every sensitive value comes from GitHub Secrets.
A workflow may generate a temporary token or receive a credential from another system.
In that situation, the value can be registered for log masking before it is used elsewhere.
TOKEN=$(./generate-token.sh)
echo "::add-mask::$TOKEN"
./deployment-tool --token "$TOKEN"The important part is the order.
The masking command must happen before the sensitive value could appear in log output.
Masking does not revoke the credential, encrypt it, or prevent the process from using it. It only helps prevent the value from being displayed in supported workflow logs.
If the credential has already been exposed somewhere else, it should still be rotated or revoked.
Avoid Passing Secrets as Command-Line Arguments When Possible
Some tools require credentials through command-line arguments.
For example:
./deploy-tool --token "$API_TOKEN"This may work, but command-line arguments can create additional exposure depending on the tool, runner, diagnostic output, or process inspection mechanisms involved.
When the tool supports a safer method, prefer:
environment variables
standard input
protected configuration files
credential files with restricted permissions
workload identity or OIDC-based authentication
For example, a tool that supports environment-based authentication can be called without putting the credential directly in the command:
export API_TOKEN="$API_TOKEN"
./deploy-toolThe exact authentication mechanism depends on the tool.
The general principle is to minimize the number of places where a secret exists.
Be Careful With Third-Party Actions
A secret passed to an action becomes available to that action.
For example:
- name: Deploy
uses: example/deploy-action@v1
with:
token: ${{ secrets.DEPLOY_TOKEN }}The workflow author may not be printing the secret, but the action receives it.
Before passing sensitive information to a third-party action, check:
whether the action actually requires the secret
what the action does with the value
whether the action writes inputs to logs
whether debugging output can expose the value
whether the action is actively maintained
whether the action is pinned to a trusted version
Secret exposure is one reason dependency and action trust matter in CI/CD.
Keep Secrets Out of Pull Request Workflows
Untrusted code should not receive production credentials.
This is especially important for pull requests originating from forks.
Consider a workflow that checks out pull request code and then passes a deployment credential to a script controlled by that code.
That creates a dangerous boundary.
The pull request code could potentially read the environment variable or otherwise misuse the credential.
A safer design separates validation from deployment.
For example:
Pull Request
|
+-- Build
|
+-- Test
|
+-- Static Analysis
|
v
No production secretsThen deployment happens through a trusted workflow or protected environment:
Main Branch
|
+-- Build
|
+-- Test
|
+-- Approval
|
+-- Deploy
|
+-- Production secretsThis design also makes it easier to understand exactly where sensitive credentials are available.
Be Careful With pull_request_target
The pull_request_target event deserves particular attention because it runs in the context of the base repository.
A common mistake is to use it simply because a normal pull_request workflow cannot access certain secrets.
The dangerous pattern is combining privileged workflow context with untrusted pull request code.
For example, a workflow that runs privileged operations and then checks out code from the pull request can create a serious security boundary problem.
Do not treat pull_request_target as a simple replacement for pull_request.
For workflows handling untrusted contributions, keep privileged operations separate from code controlled by the contributor.
Do Not Upload Secret Files as Artifacts
Secrets do not only leak through console output.
Consider a deployment process that creates:
.env
deployment-config.json
credentials.jsonand then uploads the whole working directory as an artifact.

Join the conversation! Your thoughts help the community grow.