Jobs by default run in parallel. This allows for the execution of multiple tasks as the same time. Additionally, jobs can be chained in series using the needs
property.
This exercise will walk you through creating a chained set of jobs the depend on each other.
First we will create two jobs independent of each other (running parallel).
- From the default branch of your repository, create a new branch of code called
feature/dependent
- Create a new file named
.github/workflows/dependent.yaml
- Copy the contents below to the newly created file:
name: Dependent Jobs
on:
push:
branches: feature/dependent
jobs:
first-job:
name: First Job
runs-on: ubuntu-latest
steps:
- name: Dump Job Information
env:
CONTEXT_ITEM: ${{ toJson(job) }}
run: echo "${CONTEXT_ITEM}"
second-job:
name: Second Job
runs-on: ubuntu-latest
steps:
- name: Dump Job Information
env:
CONTEXT_ITEM: ${{ toJson(job) }}
run: echo "${CONTEXT_ITEM}"
- Add & commit your changes, then publish your branch.
- Go to your repository, and view the Actions tab to see the execution against your published branch.
The result will be an execution that has two jobs which executed at the same time. The execution graph shows that jobs separated.
Second we will adjust our workflow to have two jobs in series, and a third in parallel.
- Replace the contents of the workflow file from the previous step:
name: Dependent Jobs
on:
push:
branches: feature/dependent
jobs:
first-job:
name: First Job (parallel)
runs-on: ubuntu-latest
steps:
- name: Dump Job Information
env:
CONTEXT_ITEM: ${{ toJson(job) }}
run: echo "${CONTEXT_ITEM}"
second-job:
name: Second Job (series)
runs-on: ubuntu-latest
needs: first-job
steps:
- name: Dump Job Information
env:
CONTEXT_ITEM: ${{ toJson(job) }}
run: echo "${CONTEXT_ITEM}"
third-job:
name: Third Job (parallel)
runs-on: ubuntu-latest
steps:
- name: Dump Job Information
env:
CONTEXT_ITEM: ${{ toJson(job) }}
run: echo "${CONTEXT_ITEM}"
- Add & commit your changes, then push your branch.
- Go to your repository, and view the Actions tab to see the execution.
The result will be second-job
waiting for first-job
to complete, while third-job
runs in parallel.
- Delete the published branch created in Step 1
- Switch back to the default branch locally.