outputs block in GitHub Actions

Sharing Data Between Steps in the Same Job

In GitHub Actions, you can share environment variables between steps within the same job. This is useful when one step computes a value that subsequent steps need to use.

Example: Setting and Using Environment Variables

name: Share Env Variables Within Job

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Set Environment Variable
        run: echo "MY_VAR=Hello World" >> $GITHUB_ENV

      - name: Use Environment Variable
        run: |
          echo "The value of MY_VAR is $MY_VAR"

In this example:

  • The first step sets an environment variable MY_VAR using echo "MY_VAR=Hello World" >> $GITHUB_ENV.

  • The second step accesses MY_VAR using $MY_VAR.

Passing Data From One Job to Another

You can pass data from one job to another using outputs. This is particularly important in workflows where the output of one job needs to be used as an input for subsequent jobs.

Example: Setting and Using Job Outputs

name: Pass Data Between Jobs

on: [push]

jobs:
  job1:
    runs-on: ubuntu-latest
    outputs:
      job_output: ${{ steps.set_output.outputs.value }}
    steps:
      - id: set_output
        run: echo "value=Data from Job 1" >> $GITHUB_OUTPUT

  job2:
    needs: job1
    runs-on: ubuntu-latest
    steps:
      - run: echo "Received data from job1: ${{ needs.job1.outputs.job_output }}"

In this example:

  • job1 sets an output using echo "value=Data from Job 1" >> $GITHUB_OUTPUT.

  • job2, which needs job1, uses the output from job1 with needs.job1.outputs.job_output.

Advanced Use Case: Dynamic Matrix from Job Outputs

You can even use job outputs to dynamically create a matrix for another job.

Example: Dynamic Matrix Generation

name: Dynamic Matrix Workflow

on: [push]

jobs:
  setup:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set_matrix.outputs.matrix }}
    steps:
      - id: set_matrix
        run: echo 'matrix={"include": [{"os": "ubuntu-latest", "node": "14"}, {"os": "windows-latest", "node": "12"}]}' >> $GITHUB_OUTPUT

  build:
    needs: setup
    runs-on: ${{ matrix.os }}
    strategy:
      matrix: ${{fromJson(needs.setup.outputs.matrix)}}
    steps:
      - run: echo "Running on ${{ matrix.os }} with Node.js ${{ matrix.node }}"

In this advanced example:

  • The setup job creates a dynamic matrix for operating systems and Node.js versions.

  • The build job uses this matrix to run jobs on different OS/Node.js version combinations.

Conclusion

The outputs feature in GitHub Actions provides a powerful way to share data between steps and jobs, enabling complex workflows that require passing dynamic data or results. Whether it's sharing a simple environment variable within a job or creating a dynamic matrix for multiple jobs, GitHub Actions offers the flexibility needed for efficient CI/CD processes

Sharing Data Between Steps Using steps Outputs

In GitHub Actions, each step in a job can produce outputs that can be used by subsequent steps. This is achieved by writing data to the $GITHUB_OUTPUT file and then accessing that data using the steps context.

Example: Setting and Using Step Outputs

yamlCopy codename: Share Data Between Steps

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Generate Data
        id: data_step
        run: echo "data=Generated Data" >> $GITHUB_OUTPUT

      - name: Use Generated Data
        run: echo "The data from the previous step is ${{ steps.data_step.outputs.data }}"

In this example:

  • The first step, identified by id: data_step, generates an output using echo "data=Generated Data" >> $GITHUB_OUTPUT.

  • The second step accesses this output with steps.data_step.outputs.data.

When to Use steps Outputs vs Environment Variables

  1. steps Outputs:

    • Ideal for passing data that is only relevant to subsequent steps within the same job.

    • The scope is limited to the job, and the data is structured as step outputs.

    • Useful for cases where you need to maintain the output as part of a particular step's context.

  2. Environment Variables:

    • Suitable for sharing data that is used across many steps within the same job.

    • Once set, environment variables are globally available to all subsequent steps in that job.

    • Best for scenarios where a piece of data (like a configuration parameter or a build version) is repeatedly accessed throughout the job.

Conclusion

In GitHub Actions, both environment variables and steps outputs provide flexible ways to share data between steps within a job. While environment variables are more global, steps outputs offer a more structured way to pass data from a specific step to others. Depending on the workflow's complexity and requirements, you can choose the method that best suits your needs for efficient and effective CI/CD processes.