GitHub Actions Secrets & Environment Variables
Securely manage credentials, repository secrets, environment variables, automatic GITHUB_TOKEN, and prevent credential exposure in CI/CD.
GitHub Actions Secrets & Environment Variables
Managing sensitive tokens, API credentials, and environment-specific configurations securely is essential for production CI/CD pipelines.
1. Secrets vs Environment Variables
| Feature | Environment Variables (env) | Encrypted Secrets (secrets) |
|---|---|---|
| Visibility | Plain text in workflow files or UI settings | Encrypted at rest, never visible in UI once saved |
| Log Masking | Displayed in raw terminal build logs | Automatically redacted as *** in build logs |
| Best Used For | Non-sensitive config: NODE_ENV, PORT, REGION | API keys, SSH keys, registry tokens, database passwords |
2. Defining & Accessing Encrypted Secrets
Secrets are configured in Repository Settings -> Secrets and variables -> Actions.
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Deploy to Cloud Provider
# ALWAYS pass secrets as environment variables to the step
env:
API_KEY: ${{ secrets.PROD_API_KEY }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}
run: |
./scripts/deploy.sh[!CAUTION] Never interpolate secrets directly into shell command strings:
β Vulnerable to injection & process table leaks:
run: curl -H "Authorization: Bearer ${{ secrets.TOKEN }}" https://api.example.comβ Safe pattern (passed via step env):
env: AUTH_TOKEN: ${{ secrets.TOKEN }} run: curl -H "Authorization: Bearer $AUTH_TOKEN" https://api.example.com
3. Automatic GITHUB_TOKEN
GitHub automatically provisions a short-lived installation token for every workflow run: ${{ secrets.GITHUB_TOKEN }}.
jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
- name: Create Release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh release create v1.0.0 --generate-notes4. Environment Variable Scopes
Environment variables can be declared at three different hierarchy levels:
# 1. Workflow Scope: Available to all jobs & all steps
env:
GLOBAL_REGION: us-east-1
jobs:
build:
runs-on: ubuntu-latest
# 2. Job Scope: Available to all steps in this job
env:
BUILD_TARGET: serverless
steps:
# 3. Step Scope: Available only within this single step
- name: Compile
env:
NODE_ENV: production
run: |
echo "Region: $GLOBAL_REGION"
echo "Target: $BUILD_TARGET"
echo "Node Env: $NODE_ENV"5. Built-in Runner Environment Variables
GitHub provides default environment variables on every runner:
CI: Always set totrue.GITHUB_WORKSPACE: The default working directory for steps (/home/runner/work/repo/repo).GITHUB_SHA: The commit SHA that triggered the workflow run.GITHUB_REF: The git ref that triggered the run (e.g.refs/heads/mainorrefs/pull/42/merge).GITHUB_ACTOR: The username of the user who initiated the workflow.RUNNER_TEMP: The temporary directory on the runner for scratch files.
GitHub Actions Steps & Actions
Master steps, shell command execution, marketplace actions, step outputs, GITHUB_ENV, GITHUB_OUTPUT, GITHUB_STEP_SUMMARY, and reusable composite actions.
GitHub Actions Caching & Artifacts
Accelerate pipeline speeds with dependency and build caching, and pass compiled binaries and test reports between jobs with artifacts.