Logo

MacStadium Blog

Sharing Variables Between Jobs in GitHub Actions

Level up your GitHub Actions workflows with this guide to controlling the order of execution of jobs in a workflow and passing variables from one job to the next within the workflow.

GitHub Actions workflows can be broken down into jobs, which require a runs-on value – that is, a label or series of labels that tell GitHub Actions where to execute the job. Jobs can be further broken down into steps that run in sequential order within a given job.

By contrast, jobs run in parallel by default. This means that they will each execute as soon as a suitable runner is found. If there are multiple runners with matching labels available, then the jobs in the workflow will each execute at the same time. This can be very beneficial in terms of reduced overall execution time for the workflow, but what if you need jobs to be executed in a given order? Moreover, what if you need to pass output from one job to the next, which requires that very output to run?

We’ll answer exactly those questions today as we look at needsoutputs, and variables to control the order of execution of multiple GitHub Actions jobs and to pass variables between those jobs.

Controlling Order of Execution of Jobs with needs

In order to control the order of execution of the various jobs within your workflow, you will need to set the needs value for the job to a list containing the ids of all jobs that must complete before it does.

jobs:
 job1:
   runs-on: macOS-latest
   run: echo "Hello"
 job2:
   needs: [job1]
   runs-on: macOS-latest
   run: echo "World"

In the above, we have ensured that “Hello” and “World” will always be written in the correct order because job2 needs job1 – that is, it cannot begin until job1 has been completed.

Setting Variables to a Given outputs Key

In order to store output from a job (in order to pass it to another job), you will need to define a list of steps to execute within the job. This list can be a single step, but it has to be defined as such in order to set its output to the outputs on the job itself.

jobs:
 spin_up:
   runs-on: [self-hosted, master]
   steps:
   - name: Spin Up VM
     id: spin_up
     uses: jeff-vincent/orka-actions-spin-up@master
     with:
       orkaIP: http://10.221.188.100
       orkaUser: ${{ secrets.ORKA_USER }}
       orkaPass: ${{ secrets.ORKA_PASS }}
       orkaBaseImage: gha_catalina_v2.img
       githubUser: ${{ secrets.GH_USER }}
       githubPat: ${{ secrets.GH_PAT }}
       githubRepoName: bintrail-swift-sdk
   outputs:
     uniqueVMActionsTag: ${{ steps.spin_up.outputs.uniqueVMActionsTag }}
     vmName: ${{ steps.spin_up.outputs.vmName }}

Notice in the above, we have set two outputs that will be available to any job that lists this job – spin-up – in its needs section. In order to do that, we have accessed outputs of the step by dot notation and set them to the outputs of the job.

Expressing outputs as Variables in Subsequent Jobs

Finally, after having gone to all that trouble to set the outputs, you’ll probably want to access them too. To do so, you’ll already have the needs value set in order to control the order of execution of these jobs. You can use this needs object to access the outputs you just set by again using dot notation with the following syntax:

native_job:
   needs: spin_up
   runs-on: [self-hosted, "${{ needs.spin_up.outputs.uniqueVMActionsTag }}"]
   steps:
   - name: Native Job
     id: native_job
     run: echo "I'm running on ${{ needs.spin_up.outputs.uniqueVMActionsTag }}"

In the above, we are accessing the variable uniqueVMActionsTag by referencing the outputs on the spin_up job (where it was defined) via the needs object.

TL;DR

It is possible to control the order of execution of jobs in a GitHub Actions workflow with the needs parameter. Moreover, as we demonstrated above, you can then use that needs object to access variables set as outputs by previous jobs in the workflow.

Logo

Orka, Orka Workspace and Orka Pulse are trademarks of MacStadium, Inc. Apple, Mac, Mac mini, Mac Pro, Mac Studio, and macOS are trademarks of Apple Inc. The names and logos of third-party products and companies shown on the website are the property of their respective owners and may also be trademarked.

©2023 MacStadium, Inc. is a U.S. corporation headquartered at 3525 Piedmont Road, NE, Building 7, Suite 700, Atlanta, GA 30305. MacStadium, Ltd. is registered in Ireland, company no. 562354.