Prompt
How do I handle secrets in GitHub Actions workflows?
Latest observation
The standard way to handle secrets in GitHub Actions is to store them in GitHub Secrets and reference them from your workflow, rather than hardcoding them in YAML or committing them to the repo.
1) Store secrets in GitHub
Go to:
- Repo secrets:
Settings → Secrets and variables → Actions → New repository secret - Environment secrets: same place, under Environments
- Organization secrets: for sharing across multiple repos
Examples:
API_KEYAWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYSLACK_WEBHOOK_URL
2) Use them in your workflow
Reference secrets with ${{ secrets.NAME }}:
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Use secret
run: |
echo "Secret length: ${#MY_SECRET}"
env:
MY_SECRET: ${{ secrets.API_KEY }}
3) Don’t print secrets
Avoid echo ${{ secrets.API_KEY }} or logging variables that may contain secrets. GitHub masks secrets in logs in many cases, but you should still treat them as sensitive.
4) Use environment protection for sensitive deploys
If a job deploys to production, use an environment with:
- required reviewers
- wait timers
- environment-specific secrets
Example:
jobs:
deploy:
environment: production
runs-on: ubuntu-latest
steps:
- run: ./deploy.sh
env:
PROD_TOKEN: ${{ secrets.PROD_TOKEN }}
5) Prefer short-lived credentials when possible
Instead of long-lived cloud keys, use OIDC (OpenID Connect) to get temporary cloud credentials:
- AWS: GitHub Actions OIDC + IAM role
- Azure: Workload identity federation
- GCP: Workload Identity Federation
This is more secure than storing static cloud secrets.
6) Keep secrets out of the repo
Don’t commit:
.envfiles- API keys in code
- secrets in workflow files
- credentials in artifacts or cache
Add sensitive files to .gitignore.
7) Be careful with reusable workflows and composite actions
Secrets don’t automatically flow everywhere. You may need to pass them explicitly:
uses: ./.github/workflows/deploy.yml
secrets: inherit
Or pass specific secrets:
secrets:
token: ${{ secrets.MY_TOKEN }}
8) Protect against accidental exposure
- Enable secret scanning in GitHub
- Use least-privilege permissions
- Rotate secrets regularly
- Revoke immediately if exposed
If you want, I can show you:
- a minimal example workflow using secrets, or
- how to set up OIDC for AWS/Azure/GCP.