GitHub Actions: Expressions, Contexts, Variables, and Outputs

Expressions, contexts, env vars, configuration variables, step outputs, job outputs, and dynamic workflow logic in GitHub Actions.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Contexts and expressions

Read data from the workflow run and evaluate conditions dynamically.

Use repository or organization variables

Read configuration values from the `vars` context.

yamlANYvarsenv
yaml
env:
  APP_ENV: ${{ vars.APP_ENV }}

Use configuration variables for non-secret values shared across workflows.

Build a matrix from JSON

Convert JSON text into a matrix object.

yamlANYfromJsonmatrixneeds
yaml
strategy:
  matrix: ${{ fromJson(needs.prepare.outputs.matrix) }}

`fromJson()` is useful when one job computes dynamic matrix values for another.

Check if a label is present

Use `contains()` in workflow conditions.

yamlANYcontainsiflabels
yaml
if: contains(github.event.pull_request.labels.*.name, 'deploy-preview')

Functions like `contains`, `startsWith`, `endsWith`, and `format` are commonly used in conditions.

Env files, step outputs, and job outputs

Pass values safely between steps and jobs.

Set an environment variable for later steps

Append to `GITHUB_ENV` to persist a variable in the current job.

bashANYGITHUB_ENVenv-files
bash
echo "IMAGE_TAG=${GITHUB_SHA}" >> "$GITHUB_ENV"

This is the supported way to set environment variables for subsequent steps in the job.

Set a step output

Append to `GITHUB_OUTPUT` to expose a named output.

bashANYGITHUB_OUTPUToutputs
bash
echo "version=1.2.3" >> "$GITHUB_OUTPUT"

Use `GITHUB_OUTPUT` instead of the old deprecated `set-output` workflow command.

Read a previous step output

Use the `steps` context to consume a step output.

yamlANYstepsoutputs
yaml
- id: version
  run: echo "value=1.2.3" >> "$GITHUB_OUTPUT"

- run: echo "Version is ${{ steps.version.outputs.value }}"

Give the producing step an `id` so later steps can reference its outputs.

Promote a step output to a job output

Pass values between jobs through `needs`.

yamlANYjob-outputsneeds
yaml
jobs:
  prepare:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.meta.outputs.version }}
    steps:
      - id: meta
        run: echo "version=1.2.3" >> "$GITHUB_OUTPUT"

  deploy:
    needs: prepare
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deploying ${{ needs.prepare.outputs.version }}"

Job outputs are the standard way to pass structured values between jobs.

Write a multiline value to GITHUB_OUTPUT

Preserve newlines in an output variable.

bashANYmultilineGITHUB_OUTPUT
bash
{
  echo 'notes<<EOF'
  cat CHANGELOG.md
  echo 'EOF'
} >> "$GITHUB_OUTPUT"

Use the heredoc-like delimiter format for multiline values in environment files.

Recommended next

No recommendations yet.