Environment Variables & Secrets
A comprehensive developer cheatsheet covering GitHub Actions environment variables, secrets, and contexts, featuring essential YAML configurations and operational workflows for variable precedence, default variables, and secure credential management.
Environment Variables Overview
What are Environment Variables?
Environment variables in GitHub Actions are key-value pairs that can be used across jobs, steps, and workflows. They provide configuration and context for your automation.
Types of Environment Variables
| Type | Scope | Description |
|---|---|---|
| Workflow Variables | Entire workflow | Defined at top level |
| Job Variables | Single job | Defined within a job |
| Step Variables | Single step | Set/used within a step |
| Default Variables | All workflows | Automatically set by GitHub |
| Secrets | All workflows | Encrypted sensitive data |
Default Environment Variables
| Variable | Description | Example |
|---|---|---|
GITHUB_ACTOR | User who triggered the workflow | bob |
GITHUB_REPOSITORY | Repository name | myuser/myapp |
GITHUB_REF | Branch or tag ref | refs/heads/main |
GITHUB_SHA | Commit SHA | abc123def456... |
GITHUB_WORKFLOW | Workflow name | CI Pipeline |
GITHUB_RUN_ID | Unique run ID | 1234567890 |
GITHUB_RUN_NUMBER | Run number (sequential) | 42 |
GITHUB_ACTOR | Username of the actor | bob |
GITHUB_REF_NAME | Branch or tag name | main |
GITHUB_REF_TYPE | Ref type (branch, tag) | branch |
GITHUB_EVENT_NAME | Name of triggering event | push |
GITHUB_WORKSPACE | Workspace directory | /home/runner/work/myapp |
RUNNER_OS | Runner OS | Linux, Windows, macOS |
RUNNER_TEMP | Temporary directory | /home/runner/work/_temp |
Setting Environment Variables
1. Workflow-Level Variables
name: CI Pipeline
# Global environment variables (available to all jobs)
env:
NODE_VERSION: '18'
REGISTRY: 'docker.io'
APP_NAME: 'myapp'
REGION: 'us-east-1'
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "Node version: ${{ env.NODE_VERSION }}"2. Job-Level Variables
jobs:
build:
runs-on: ubuntu-latest
# Job-specific variables (override global)
env:
NODE_VERSION: '20' # Overrides global
ENVIRONMENT: 'production'
steps:
- run: echo "Node version: ${{ env.NODE_VERSION }}"
- run: echo "Environment: ${{ env.ENVIRONMENT }}"3. Step-Level Variables
steps:
- name: Set a step variable
run: |
echo "MY_VAR=Hello World" >> $GITHUB_ENV
echo "CURRENT_TIME=$(date)" >> $GITHUB_ENV
- name: Use the step variable
run: |
echo "Value: ${{ env.MY_VAR }}"
echo "Time: ${{ env.CURRENT_TIME }}"4. Dynamic Variables from Commands
steps:
- name: Set dynamic variable
id: vars
run: |
echo "VERSION=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT
echo "COMMIT=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
- name: Use dynamic variable
run: |
echo "Version: ${{ steps.vars.outputs.VERSION }}"
echo "Commit: ${{ steps.vars.outputs.COMMIT }}"5. Using $GITHUB_ENV for Step Variables
steps:
- name: Set multiple variables
run: |
echo "DEPLOY_ENV=staging" >> $GITHUB_ENV
echo "BUILD_NUMBER=${{ github.run_number }}" >> $GITHUB_ENV
echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- name: Use variables
run: |
echo "Deploying to ${{ env.DEPLOY_ENV }}"
echo "Build #${{ env.BUILD_NUMBER }}"Secrets Overview
What are Secrets?
Secrets are encrypted environment variables used to store sensitive information like API keys, passwords, and tokens. They are never exposed in logs.
Types of Secrets
| Type | Description | Use Case |
|---|---|---|
| Repository Secrets | Available in current repo | API keys, tokens |
| Organization Secrets | Available across org | Shared credentials |
| Environment Secrets | Available in specific environment | Environment-specific secrets |
| Dependabot Secrets | For Dependabot PRs | Dependabot access |
Adding Secrets
Repository:
Settings → Secrets and variables → Actions → New repository secret
Environment:
Settings → Environments → New environment → Add secret
Organization:
Organization Settings → Secrets and variables → Actions → New organization secret
Secret Naming Convention
- Use UPPERCASE_WITH_UNDERSCORES
- Be descriptive (e.g., AWS_ACCESS_KEY_ID, DOCKER_PASSWORD)
- Don't use spaces or special charactersUsing Secrets in Workflows
1. Basic Secret Usage
steps:
- name: Use a secret
run: |
echo "Deploying with API key: ${{ secrets.API_KEY }}"
env:
API_KEY: ${{ secrets.API_KEY }}** WARNING:** Secrets are masked in logs, but avoid printing them!
2. Using Secrets as Environment Variables
jobs:
deploy:
runs-on: ubuntu-latest
environment: production # Env-specific secrets
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
steps:
- run: aws s3 ls3. Using Secrets with Actions
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Deploy with kubectl
run: |
echo "${{ secrets.KUBE_CONFIG }}" > kubeconfig
kubectl apply -f deployment.yaml --kubeconfig=kubeconfig4. Secrets in Environment Files
steps:
- name: Create .env file from secrets
run: |
echo "DATABASE_URL=${{ secrets.DATABASE_URL }}" >> .env
echo "API_KEY=${{ secrets.API_KEY }}" >> .env
echo "REDIS_URL=${{ secrets.REDIS_URL }}" >> .env5. Contextual Secrets (GITHUB_TOKEN)
steps:
- name: Create PR comment
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Build completed successfully!'
})Contexts
Available Contexts
| Context | Description | Example |
|---|---|---|
github | GitHub event payload | github.ref |
env | Environment variables | env.NODE_VERSION |
secrets | Secrets | secrets.API_KEY |
inputs | Workflow inputs | inputs.environment |
runner | Runner info | runner.os |
strategy | Matrix strategy | strategy.matrix.node-version |
matrix | Matrix variables | matrix.node-version |
needs | Job dependencies | needs.build.outputs.version |
steps | Step outputs | steps.setup.outputs.version |
Common Context Usage
jobs:
build:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.vars.outputs.VERSION }}
steps:
- id: vars
run: echo "VERSION=1.0.0" >> $GITHUB_OUTPUT
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- run: echo "Version: ${{ needs.build.outputs.version }}"
- run: echo "Ref: ${{ github.ref }}"
- run: echo "Runner OS: ${{ runner.os }}"
- run: echo "Event: ${{ github.event_name }}"GitHub Context Examples
steps:
- name: Debug GitHub context
run: |
echo "Repository: ${{ github.repository }}"
echo "Actor: ${{ github.actor }}"
echo "Ref: ${{ github.ref }}"
echo "SHA: ${{ github.sha }}"
echo "Workflow: ${{ github.workflow }}"
echo "Run ID: ${{ github.run_id }}"
echo "Run Number: ${{ github.run_number }}"
echo "Event Name: ${{ github.event_name }}"
echo "Workspace: ${{ github.workspace }}"Variable Precedence
Precedence Order (Highest to Lowest)
| Level | Scope | Example |
|---|---|---|
1. Step env | Single step | env: { VAR: 'step' } |
2. Job env | Single job | env: { VAR: 'job' } |
3. Workflow env | Entire workflow | env: { VAR: 'workflow' } |
4. GITHUB_ENV | Step output | echo "VAR=value" >> $GITHUB_ENV |
| 5. Default vars | GitHub defaults | GITHUB_REF, RUNNER_OS |
Example: Override Precedence
name: Precedence Demo
env:
VAR: 'workflow-level'
jobs:
demo:
runs-on: ubuntu-latest
env:
VAR: 'job-level'
steps:
- name: Step variable
env:
VAR: 'step-level'
run: echo "Value: ${{ env.VAR }}" # Outputs: "step-level"
- name: No step variable
run: echo "Value: ${{ env.VAR }}" # Outputs: "job-level"
- name: Set via GITHUB_ENV
run: echo "VAR=GITHUB_ENV-level" >> $GITHUB_ENV
- name: After GITHUB_ENV
run: echo "Value: ${{ env.VAR }}" # Outputs: "GITHUB_ENV-level"Quick Reference — Most Used Commands
| Task | YAML Snippet |
|---|---|
| Set workflow env var | env: { NODE_VERSION: '18' } |
| Set job env var | env: { ENV: 'production' } |
| Set step env var | env: { VAR: 'value' } |
| Set via GITHUB_ENV | echo "VAR=value" >> $GITHUB_ENV |
| Set output | echo "value=123" >> $GITHUB_OUTPUT |
| Use env var | ${{ env.VAR }} |
| Use secret | ${{ secrets.API_KEY }} |
| Use context | ${{ github.ref }} |
| Use output from step | ${{ steps.step-id.outputs.value }} |
| Use output from job | ${{ needs.job.outputs.value }} |
Pro Tips
-
Never print secrets — they'll be masked but still exposed in logs.
-
Use
GITHUB_TOKEN— built-in secret for GitHub API access. -
Environment-specific secrets — use Environments for staging/prod.
-
Secret rotation — regularly rotate secrets for security.
-
Use
envbeforewith— for action-specific environment variables. -
Check variable precedence — when overriding.
-
Use
${{ github.event.inputs }}— for manual workflow inputs. -
Debug with
echo— but don't print secrets:yaml- run: echo "Debug: ${{ env.SAFE_VAR }}" -
Use
ifwith contexts — conditional execution:yamlif: github.ref == 'refs/heads/main' && github.actor == 'bob' -
Secrets in PRs — secrets aren't available to PRs from forks for security.