GitHub Actions Default Environment Variables

The orchestration of continuous integration and continuous deployment (CI/CD) pipelines within GitHub Actions relies heavily on the ability of the runner to understand its own context. This contextual awareness is provided through a sophisticated system of default environment variables. These variables are automatically injected by GitHub into every step of a workflow, providing critical metadata about the trigger event, the repository state, the actor initiating the process, and the specific runner environment. For the developer, these variables transform a generic virtual machine into a specialized build agent that knows exactly which commit it is testing, which user triggered the build, and which branch is being merged.

The operational significance of these variables cannot be overstated. Without them, developers would be forced to manually parse complex JSON event payloads or use API calls to determine basic information, such as the current branch name or the repository owner. By providing these as environment variables, GitHub allows shell scripts, build tools, and third-party actions to interact seamlessly with the workflow state. This creates a dense web of automation where a script can dynamically decide to run a specific set of tests based on the GITHUB_EVENT_NAME or push a build artifact to a specific location based on the GITHUB_REF.

The Architectural Nature of Default Variables

GitHub Actions distinguishes between different types of variables to maintain security and operational stability. Default environment variables are those set by the GitHub platform itself. Because these are managed by the system, they exhibit specific behaviors regarding access and mutability.

A critical distinction exists between environment variables and context properties. While default environment variables are available to every shell command in a workflow step, they are not accessible through the env context because they are not defined within the workflow YAML. However, GitHub provides a corresponding context property for most of these variables. For example, while a shell script would use the environment variable GITHUB_REF, the workflow YAML can access the same data using the context property ${{ github.ref }}.

In terms of mutability, GitHub enforces strict rules to prevent the corruption of the workflow environment. Variables prefixed with GITHUB_* and RUNNER_* are immutable; they cannot be overwritten by the user. The only notable exception is the CI variable, which can currently be overwritten, though GitHub does not guarantee this capability will persist in future updates.

Comprehensive Analysis of Core Default Variables

The following table outlines the primary default environment variables available to any GitHub Actions workflow or shell script, regardless of the operating system.

Variable Description
CI Always set to true
GITHUB_WORKFLOW The name of the workflow
GITHUBRUNID A unique identifier for each run of a workflow
GITHUBRUNNUMBER A unique number for each run of a particular workflow in a repository
GITHUB_ACTION The name of the action currently running, or the id of a step
GITHUB_ACTIONS Always set to true when GitHub Actions is running the workflow
GITHUB_ACTOR The name of the person or app that initiated the workflow
GITHUB_REPOSITORY The owner and repository name (e.g., username/repository_name)
GITHUBEVENTNAME The name of the event that triggered the workflow
GITHUBEVENTPATH The path to the current event payload
GITHUB_WORKSPACE The default working directory for jobs
GITHUB_SHA The commit SHA that triggered the workflow
GITHUB_REF The branch or tag that triggered the workflow
GITHUBHEADREF The name of the head branch in a pull_request event
GITHUBBASEREF The name of the base branch in a pull_request event
GITHUBSERVERURL The URL of the GitHub server
GITHUBAPIURL The URL of the GitHub API
GITHUBGRAPHQLURL The URL of the GitHub GraphQL API

Deep Drill into Variable Functionality and Impact

The utility of these variables extends beyond simple identification. Each variable provides a specific lever for automation logic.

The CI and GITHUB_ACTIONS variables are both set to true. This is fundamentally important for developers who write tests that are intended to run both locally on a developer's machine and on a remote server. By checking for these variables, a script can determine if it is running in a CI environment and adjust its behavior—for example, by disabling interactive prompts or using a different logging format.

The GITHUB_ACTION variable provides granular tracking of the current execution step. If an action is running, it returns the repository owner and action name. If a script is running without a specific ID, it returns __run. If multiple scripts are executed, GitHub appends a sequence number, such as __run_2. This allows for precise debugging and logging within the workflow execution path.

The GITHUB_ACTION_PATH variable is exclusively supported in composite actions. It provides the absolute path to where the action is located (e.g., /home/runner/work/_actions/repo-owner/name-of-action-repo/v1). This is critical for composite actions that need to reference other files within their own repository, allowing them to change directories to the action's location rather than relying on the workspace root.

The GITHUB_ACTION_REPOSITORY variable identifies the owner and repository of the action being executed, such as actions/checkout. This provides a mechanism for auditing which third-party code is currently interacting with the runner's environment.

The GITHUB_REF variable serves as the primary indicator of the trigger source. For example, if a workflow is triggered by a push to a branch, GITHUB_REF will contain the branch reference. However, it is important to note that GITHUB_REF will be blank if the trigger is not based on branches or tags, such as in the case of a workflow_dispatch event.

The GITHUB_ACTOR variable captures the identity of the entity that caused the workflow to run. This is essential for notifications or for conditional logic where certain steps should only execute if a specific user or application (like a bot) initiated the process.

OS-Specific and Extended Environment Variables

While the 18 variables listed above are universal, the actual environment available to a developer is significantly larger depending on the runner image. For instance, a build running on ubuntu-latest provides over 60 additional environment variables beyond the standard GitHub set.

These extended variables are "standard issue" for the distribution but vary by operating system. A macos-latest runner may have a slightly different set of variables than ubuntu-latest. One primary example is the PATH variable, which is available and critical on windows-latest for locating executables within the system shell.

This variance means that while GITHUB_* variables are consistent, any reliance on system-level environment variables must be caveated by the specific runner image being used.

Implementation and Variable Management

Developers can interact with these variables through multiple methods depending on whether they are working within the YAML configuration or within a shell script.

Direct Shell Access

In a bash or shell context, these variables are accessed as standard environment variables. For example, to print the repository name, a developer would use:

echo $GITHUB_REPOSITORY

Contextual YAML Access

Within the workflow YAML, variables can be accessed using the GitHub context notation. This allows the developer to use the value of a variable to define the name of a step or to pass it as an input to another action:

echo ${{ github.repository }}

User-Defined Variables

Beyond the default variables provided by GitHub, developers can set their own variables. The simplest method is using the env: keyword within a step:

yaml - name: Example Step env: MY_CUSTOM_VAR: "Hello World" run: echo $MY_CUSTOM_VAR

In this structure, the name of the variable is placed on the left side of the colon, and the definition is placed on the right.

Advanced Workflow Manipulation: The default-env Action

Because default environment variables are not natively accessible through the env context, some developers utilize third-party tools to bridge this gap. The Actions-R-Us/default-env@v1 action is designed to populate the workflow context env map with the default environment variables.

Once this action is executed, every subsequent step in the workflow gains access to these variables through the env context. This is particularly useful for those who want to treat default variables with the same flexibility as user-defined variables.

Basic implementation:

yaml - uses: Actions-R-Us/default-env@v1

Furthermore, users can modify the values of the environment variables seen by this action by modifying its own env map. This effectively allows a developer to override the default values for the duration of the workflow:

yaml - uses: Actions-R-Us/default-env@v1 env: GITHUB_ACTOR: 'github' GITHUB_REF: 'master'

In this scenario, the specified values will replace the defaults for subsequent steps. It is important to note that this specific action is not certified by GitHub and is governed by third-party terms of service.

Practical Exercise for Variable Inspection

To understand the full scope of available variables in a specific environment, developers are encouraged to perform a manual inspection of their runner.

  1. Initialize a new branch for experimentation:
    git checkout -b "env-var"

  2. Move an exploration YAML file into the workflow directory:
    mv activity-1-sample-github-actions/exploring-var-and-secrets.yml .github/workflows/exploring-var-and-secrets.yml

  3. Commit and push the changes to trigger the workflow:
    git add .github/*
    git commit -m "exploring gha variables"
    git push --set-upstream origin env-var

  4. Create a pull request and navigate to the "Details" button next to the workflow run. This allows the developer to view the logs and see exactly which variables were printed to the console, providing a real-time map of the environment's state.

Conclusion: Strategic Analysis of Environment Variables

The system of default variables in GitHub Actions is designed to balance the need for rich metadata with the requirement for environment stability. By separating the GITHUB_* variables from the user-defined env context, GitHub ensures that the core identity of the workflow run remains untampered and consistent across all steps.

The impact of this design is a highly portable automation framework. Because variables like GITHUB_WORKSPACE and GITHUB_SHA are consistent across all runner types, developers can write actions that are agnostic to the underlying operating system. The ability to use these variables to avoid hardcoded file paths is a critical best practice; using GITHUB_WORKSPACE ensures that an action will function correctly regardless of whether it is running on a Linux, macOS, or Windows runner.

Ultimately, the mastery of these variables allows a developer to transition from simple linear scripts to complex, conditional pipelines. By leveraging GITHUB_EVENT_NAME to trigger specific logic and GITHUB_REF to target specific environments, the CI/CD pipeline becomes a dynamic entity capable of responding to the specific nuances of the software development lifecycle.

Sources

  1. GitHub Docs - Variables
  2. The Server Side - Environment Variables Full List
  3. GitHub Marketplace - default-environment
  4. Hutch Data Science - GitHub Automation for Scientists

Related Posts