Bootstrap FreeKB - GitHub Actions - Passing output between jobs
GitHub Actions - Passing output between jobs

Updated:   |  GitHub Actions articles

GitHub Actions can be used to do something whenever something happens in one of your GitHub repositories. If you are not familiar with GitHub Actions, check out my article Getting Started with GitHub Actions.

This also assumes you are familiar with Output. If not, check out my article GitHub Actions - Getting Started with Output.

You can

Here is an example of how you can create a variable named foo that contains a value of Hello World in job1 and use the foo variable in job2.

name: my workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
jobs:  
  job1:
    runs-on: ubuntu-latest
    outputs:
      foo: ${{ steps.greeting.outputs.foo }}     
    steps:
      - name: define the foo variable
        id: greeting
        run: echo "foo=Hello World" >> $GITHUB_OUTPUT

      - name: display the value of the foo variable in job1
        run: echo ${{ steps.greeting.outputs.foo }}
  job2:
    runs-on: ubuntu-latest
    needs: job1
    steps:
      - name: display the value of the foo variable in job2
        run: echo ${{ needs.job1.outputs.foo }}  

 

For more complex scenarios, you may want to create JSON in job1 and then use the JSON in job2.

name: my workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
jobs:  
  job1:
    runs-on: ubuntu-latest
    outputs:
      greetings: ${{ steps.greeting.outputs.greetings }}     
    steps:
      - name: define greetings
        id: greetings
        run: |
          vars='{"include":[]}'
          echo "greeting=$(echo $vars | jq --compact-output ".include += [{\"name\":\"John Doe\", \"occupation\":\"engineer\"}]")" >> $GITHUB_OUTPUT
          echo "greeting=$(echo $vars | jq --compact-output ".include += [{\"name\":\"Jane Doe\", \"occupation\":\"customer service\"}]")" >> $GITHUB_OUTPUT

      - name: display the greetings in job1
        run: echo ${{ steps.greeting.outputs.foo }}
  job2:
    runs-on: ubuntu-latest
    needs: job1
    strategy:
      matrix: ${{ fromJson(needs.job1.outputs.greetings) }}    
    steps:
      - name: display each persons name and occupation
        run: echo "${{ toJSON(matrix.name) }} ${{ toJSON(matrix.occupation) }}"  

 

Let's say you have a main workflow that uses a reusable workflow and the reusable workflow creates an output. Here is an example of how you can use an output created in a reusable working in another job or in another reusable workflow.

For example, let's say you have a main workflow that first uses the reusable workflow named foo.yml and the foo.yml workflow creates an output named hello that contains value World. The main workflow then has a subsequent job that needs the hello output and a subsequent reusable workflow that also needs the hello output. The main workflow could look something like this.

name: main workflow
run-name: ${{ github.workflow }} by ${{ github.actor }}
on:
  workflow_dispatch:  
jobs:  
  job1:
    uses: acme/my-repo/.github/workflows/foo.yml@main
    secrets: inherit

  job2:
    runs-on: [ubuntu-latest]
    needs: foo
    steps:
      - run: echo ${{ needs.foo.outputs.hello }}
	  
  job3:
    uses: acme/my-repo/.github/workflows/bar.yml@main
    secrets: inherit
    needs: foo
    with:
      hello: ${{ needs.foo.outputs.hello }}   

 

The reusable workflow that creates the hello output could have something like this.

name: foo reusable workflow
run-name: ${{ github.workflow }} by ${{ github.actor }}
on:
  workflow_call:
    outputs:
      version:
        hello: ${{ jobs.my-job.outputs.hello }}       
jobs:
  my-job:
    runs-on: [ubuntu-latest]
    outputs:
      hello: ${{ steps.my.outputs.hello }}
    steps:
      - name: define steps.my.outputs.hello
        id: my
        run: echo "hello=World" >> $GITHUB_OUTPUT 

 

You can then use the output in your main workflow.

  job2:
    runs-on: [ubuntu-latest]
    needs: foo
    steps:
      - run: echo ${{ needs.foo.outputs.hello }}

 

And here is an example of how to use the hello output in a different reusable workflow.

name: bar reusable workflow
run-name: ${{ github.workflow }} by ${{ github.actor }}
on:
  workflow_call:
    inputs:
      hello:
        type: string
        required: true          
jobs:
  my-job:
    runs-on: [ubuntu-latest]
    steps:
      - run: echo ${{ inputs.hello }}

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter d5514a in the box below so that we can be sure you are a human.