How to pass custom variables in GitHub Actions ?

In GitHub Actions, you can share and pass custom variables between steps, jobs, or even workflows. Here’s how you can do it:


1. Sharing Variables Between Steps

You can define custom variables in one step and pass them to the next step using env or outputs.

  • Using env

Define the variable using env and reference it in subsequent steps:

The $GITHUB_ENV file is a special file for setting environment variables available to later steps.

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Set a custom variable
        run: echo "MY_VAR=Hello, World!" >> $GITHUB_ENV

      - name: Use the custom variable
        run: echo "$MY_VAR"

  • Using outputs

Define an output in one step and reference it in another.

Here:

id --> set-var gives the step an identifier.

::set-output --> sets an output value, accessible using steps.<step-id>.outputs.<output-name>.

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - id: set-var
        run: echo "::set-output name=my_var::Hello, World!"

      - name: Use the output
        run: echo "${{ steps.set-var.outputs.my_var }}"

2. Sharing Variables Between Jobs

Jobs run in isolated environments, so to share variables between them, you need to use artifacts or outputs from one job and pass them to another.

  • Using Outputs

You can pass outputs from one job to another via needs:

jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
      - name: Set an output
        id: set-output
        run: echo "::set-output name=my_var::Hello from job1"

  job2:
    needs: job1
    runs-on: ubuntu-latest
    steps:
      - name: Use the output from job1
        run: echo "${{ needs.job1.outputs.my_var }}"

  • Using Artifacts

Artifacts are files shared between jobs. Save variables to a file, upload it as an artifact, and download it in another job.

In Job 1: Save Variables

steps:
  - name: Save variable to file
    run: echo "MY_VAR=Hello, World!" > my_var.txt

  - name: Upload the file as an artifact
    uses: actions/upload-artifact@v3
    with:
      name: my-vars
      path: my_var.txt

In Job 2: Retrieve Variables

steps:
  - name: Download artifact
    uses: actions/download-artifact@v3
    with:
      name: my-vars

  - name: Read the variable
    run: |
      source my_var.txt
      echo "The variable is $MY_VAR"

3. Passing Variables to Reusable Workflows

When using reusable workflows, you can pass variables as inputs.

Reusable Workflow (e.g., .github/workflows/reusable.yml)

on:
  workflow_call:
    inputs:
      my_input:
        required: true
        type: string

jobs:
  example:
    runs-on: ubuntu-latest
    steps:
      - name: Print input
        run: echo "${{ inputs.my_input }}"

Calling Workflow

jobs:
  call-reusable:
    uses: user/repo/.github/workflows/reusable.yml@main
    with:
      my_input: "Hello from the caller"


Summary of Methods -

FragmentMethodTool
Between Stepsenv or outputs$GITHUB_ENV, ::set-output
Between Jobsneeds or artifactsOutputs or file artifacts
Between WorkflowsInputs to reusable workflowsworkflow_call inputs