Skip to content

Chapter 4 of 7

Environment Variables, Outputs, and Context References

Environment variables can be set at three scopes. A step-level env: block applies only to that step, a job-level env: applies to all steps within the job, and a workflow-level env: placed under on: and above jobs: applies to every step in every job. The default working directory is the repository root, also known as GITHUB_WORKSPACE, which can be overridden with working-directory: at the job or step level.

Steps and jobs exchange data through outputs. A step declares its outputs in its id block, and downstream steps read them with ${{ steps.step_id.outputs.output_name }}. Jobs similarly declare outputs under jobs..outputs and expose them to dependent jobs through ${{ needs.job_id.outputs.output_name }}. This expression syntax is the same one used throughout GitHub Actions to read context variables, which are objects exposed by the runner. github.sha returns the commit SHA that triggered the run, github.ref returns the branch or tag ref such as refs/heads/main, github.actor returns the user or app that initiated the run, and github.event.pull_request.number returns the pull request number for pull_request events. Run-specific identifiers include github.run_id, a unique number for the current run, and github.run_number, which increments per run within the repository.

The shell used by run: defaults to bash on Linux runners and can be changed per step with shell: pwsh, shell: sh, shell: python, and so on, or globally for a job with defaults.run.shell. Multiline scripts are written with a pipe block under run: or a heredoc inside the script. Conditional steps can read these context variables directly, such as if: github.event_name == 'pull_request' to run only on pull requests, or if: "! contains(github.event.head_commit.message, '[skip ci]')" to skip workflows based on commit message patterns.

All chapters
  1. 1GitHub Actions Foundations
  2. 2Triggering Workflows
  3. 3Jobs, Matrices, Concurrency, and Conditions
  4. 4Environment Variables, Outputs, and Context References
  5. 5Permissions, Tokens, and Security
  6. 6Artifacts, Caching, and Service Containers
  7. 7Reusable Workflows, Environments, and Deployments

Drill it

Reading is not remembering. These come from the Github Actions Ci Cd Recipes deck:

Q

What is GitHub Actions?

A CI/CD and workflow automation platform built into GitHub that runs jobs in response to repository events using YAML-defined workflows stored in .github/workfl...

Q

What file extension do GitHub Actions workflow files use?

.yaml or .yml, stored under .github/workflows/ in the repository.

Q

What is a workflow in GitHub Actions?

An automated, configurable process defined by a YAML file that runs one or more jobs and is triggered by events.

Q

What is a job in GitHub Actions?

A set of steps in a workflow that execute on the same runner; jobs run in parallel by default unless dependencies are declared with needs:.