GitHub Actions Environment Variables
Environment variables are crucial for passing data between steps and jobs in GitHub Actions workflows. Let’s dive into how to use them effectively.
Default Environment Variables
GitHub Actions provides a set of default environment variables available in every workflow run. Some key ones include:
GITHUB_REPOSITORY
: The owner/repo nameGITHUB_SHA
: The commit SHA that triggered the workflowGITHUB_REF
: The branch or tag ref that triggered the workflowGITHUB_WORKSPACE
: The path to the GitHub workspace directory
For a full list, check out the official docs ↗.
Setting Environment Variables
Job-level Variables
Set variables for all steps in a job using the env
key:
Step-level Variables
Set variables for a specific step:
Adding custom environment variables from a step
To set variables that persist across steps, use the GITHUB_ENV
file:
Practical Example: Dynamic Configuration
Here’s a workflow that demonstrates various env var techniques:
This workflow:
- Sets an environment name based on the branch
- Passes the env name to a subsequent job
- Uses both job-level and step-level environment variables