In this hands-on lab you will create a reusable workflow and a workflow that consumes it. You will learn to pass in parameters to the reusable workflow and use output parameters in the consuming workflow.
This hands on lab consists of the following steps:
- Create a new file
.github/workflows/reusable.yml
(paste the file name with the path in the box). - Set the name to
Reusable workflow
.
Solution
name: Reusable workflow
- Add a
workflow_call
trigger with an input parameterwho-to-greet
of the typestring
that is required. Set the default value toWorld
.
Solution
workflow_call:
inputs:
who-to-greet:
description: 'The person to greet'
type: string
required: true
default: World
- Add a job named
reusable-job
that runs onubuntu-latest
that echos "Hello " to the console.
Solution
jobs:
reusable-job:
runs-on: ubuntu-latest
steps:
- name: Greet someone
run: echo "Hello ${{ inputs.who-to-greet }}"
- Add an additional step with the id
time
that uses a workflow command to set an output parameter namedcurrent-time
to the current date and time (use$(date)
for that).
Solution
- name: Set time
id: time
run: echo "::set-output name=current-time::$(date)"
- Add an output called
current-time
to thereusable-job
.
Solution
outputs:
current-time: ${{ steps.time.outputs.current-time }}
- Add an output parameter called
current-time
toworkflow_call
and set it to the outputs of the workflow command.
Solution
outputs:
current-time:
description: 'The time when greeting.'
value: ${{ jobs.reusable-job.outputs.current-time }}
Complete Solution
name: Reusable workflow
on:
workflow_call:
inputs:
who-to-greet:
description: 'The person to greet'
type: string
required: true
default: World
outputs:
current-time:
description: 'The time when greeting.'
value: ${{ jobs.reusable-job.outputs.current-time }}
jobs:
reusable-job:
runs-on: ubuntu-latest
outputs:
current-time: ${{ steps.time.outputs.current-time }}
steps:
- name: Greet someone
run: echo "Hello ${{ inputs.who-to-greet }}"
- name: Set time
id: time
run: echo "::set-output name=current-time::$(date)"
- Create a new file
.github/workflows/reuse.yml
(paste the file name with the path in the box). - Set the name to
Reuse other workflow
and add a manual trigger.
Solution
name: Reuse other workflow
on: [workflow_dispatch]
- Add a job
call-workflow
that uses the reusable workflow and passes in your user name as an input parameter.
Solution
jobs:
call-workflow:
uses: ./.github/workflows/reusable.yml
with:
who-to-greet: '@wulfland'
- Add another job
use-output
that writes the output parametercurrent-time
to the console. (Hint: use the needs context to access the output)
Solution
use-output:
runs-on: ubuntu-latest
needs: [call-workflow]
steps:
- run: echo "Time was ${{ needs.call-workflow.outputs.current-time }}"
- Run the workflow and observe the output.
In this lab you have learned to create a reusable workflow and a workflow that consumes it. You also have learned to pass in parameters to the reusable workflow and to use output parameters in the consuming workflow.
You can continue with the README.