Skip to content
AstralSurge Learnings
Follow LinkedIn X RSS

How to Pass Variables Between Jobs in GitHub Actions

In GitHub Actions workflows, individual jobs execute in completely isolated runner virtual machines. Environment variables defined in one job are not available in downstream jobs. To pass data between jobs, you must export values using step outputs and declare them in job-level outputs.

Deprecation Warning: Avoid ::set-output

GitHub has officially deprecated and disabled the legacy ::set-output workflow command. Workflows attempting to execute echo "::set-output name=..." fail on modern GitHub runners. Always append output keys to the $GITHUB_OUTPUT environment file.

Correct Multi-Job Workflow Syntax

Passing variables between jobs requires a two-tiered mapping:

  1. Write the value to $GITHUB_OUTPUT inside a step with an id.
  2. Expose that step output in the job's top-level outputs block.
  3. Declare needs: <producer-job> in the downstream job and access the value via needs.<job>.outputs.<key>.
name: Cross-Job Variable Transfer

on: [push]

jobs:
  build-and-generate:
    runs-on: ubuntu-latest
    outputs:
      artifact-version: ${{ steps.version-step.outputs.version }}
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Compute Build Version
        id: version-step
        run: |
          BUILD_TAG="v2.4.0-${{ github.run_number }}"
          echo "Computed build tag: $BUILD_TAG"
          # Modern GitHub Actions output assignment
          echo "version=$BUILD_TAG" >> "$GITHUB_OUTPUT"

  deploy-service:
    runs-on: ubuntu-latest
    needs: build-and-generate
    steps:
      - name: Consume Version from Upstream Job
        run: |
          echo "Deploying target version: ${{ needs.build-and-generate.outputs.artifact-version }}"

0 Comments