Skip to content

Using Outputs from Reusable Workflow

olsido edited this page Aug 5, 2024 · 1 revision

Use Case

Using outputs from reusable workflows allows the parent workflow to use results generated by the called workflow. This is useful for workflows that need to perform operations sequentially, where the output of one step is required for the next step.

Implementation

The standard way to set an output is to transfer it to the variable $GITHUB_OUTPUT, like this:

echo "child-output=Hello from the child workflow" >> $GITHUB_OUTPUT

Then the parent workflow will display it like this (note the “child-output” at the end):

echo "Child workflow output - ${{ needs.child-workflow.outputs.child-output }}"

In this example, the child workflow generates an output that is used by the parent workflow. The parent workflow first performs some tasks, then calls the child workflow and finally uses the output from the child workflow in its subsequent steps. This chaining of workflows allows for complex CI/CD pipelines where each step builds on the results of the previous steps.

Parent Workflow (.github/workflows/using-outputs-from-reusable-workflow–parent-workflow.yml)

name: Using Outputs from Reusable Workflow - Parent Workflow  # Name of the parent workflow

on:
  workflow_dispatch:  # Trigger this workflow manually

jobs:
  before-child-workflow:  # Job to run before calling the child workflow
    runs-on: ubuntu-latest  # Use the latest Ubuntu runner
    steps:
      - name: Echo before calling Child  # Step name
        run: echo "This is the parent workflow, before calling the child workflow."  # Command to run

  child-workflow:  # Job to call the child workflow
    needs: before-child-workflow  # This job depends on the successful completion of 'before-child-workflow' job
    uses: ./.github/workflows/using-outputs-from-reusable-workflow--child-workflow.yml  # Call the child workflow

  after-child-workflow:  # Job to run after the child workflow has completed
    needs: child-workflow  # This job depends on the successful completion of 'child-workflow' job
    runs-on: ubuntu-latest  # Use the latest Ubuntu runner
    steps:
      - name: Echo after calling Child  # Step name
        run: echo "This is the parent workflow, after the child workflow has completed."  # Command to run
      - name: Use Child Workflow Output  # Step to use the output from the child workflow
        run: echo "Child workflow output - ${{ needs.child-workflow.outputs.child-output }}"  # Echo the output from the child workflow

View on Gist       View in this repo

Child Workflow (.github/workflows/using-outputs-from-reusable-workflow–child-workflow.yml)

name: Using Outputs from Reusable Workflow - Child Workflow  # Name of the child workflow

on:
  workflow_call:  # Allows this workflow to be called by another workflow
    outputs:  # Map the workflow outputs to job outputs
      child-output:  # Define an output named 'child-output'
        description: "The child output"  # Description of the output
        value: ${{ jobs.echo-from-child.outputs.output }}  # Value of the output mapped from the job output

jobs:
  echo-from-child:  # Job to run in the child workflow
    runs-on: ubuntu-latest  # Use the latest Ubuntu runner
    outputs:  # Map the job outputs to step outputs
      output: ${{ steps.generate-output.outputs.child-output }}  # Reference the output from the step
    steps:
      - name: Generate Output  # Step to generate the output
        id: generate-output  # Step ID
        run: echo "child-output=Hello from the child workflow" >> $GITHUB_OUTPUT  # Set the output

      - name: Echo from Child  # Step to echo a message from the child workflow
        run: echo "This is the child workflow."  # Command to run

View on Gist       View in this repo

Clone this wiki locally