Skip to content

Latest commit

 

History

History
187 lines (135 loc) · 4.5 KB

04_Reusable-workflows.md

File metadata and controls

187 lines (135 loc) · 4.5 KB

🔨 Hands-on: Reusable workflows

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:

Creating a reusable workflow

  1. Create a new file .github/workflows/reusable.yml (paste the file name with the path in the box).
  2. Set the name to Reusable workflow.
Solution
name: Reusable workflow
  1. Add a workflow_call trigger with an input parameter who-to-greet of the type string that is required. Set the default value to World.
Solution
on:
  workflow_call:
    inputs:
      who-to-greet:
        description: 'The person to greet'
        type: string
        required: true
        default: World
  1. Add a job named reusable-job that runs on ubuntu-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 }}"

Adding an output parameter

  1. Add an additional step with the id time that uses a workflow command to set an output parameter named current-time to the current date and time (use $(date) for that).
Solution
      - name: Set time
        id: time
        run: echo "time=$(date)" >> $GITHUB_OUTPUT
  1. Add an output called current-time to the reusable-job.
Solution
   outputs:
      current-time: ${{ steps.time.outputs.time }}
  1. Add an output parameter called current-time to workflow_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.time }}
    steps:
      - name: Greet someone
        run: echo "Hello ${{ inputs.who-to-greet }}"
      - name: Set time
        id: time
        run: echo "time=$(date)" >> $GITHUB_OUTPUT

Consuming the reusable workflow

  1. Create a new file .github/workflows/reuse.yml (paste the file name with the path in the box).
  2. Set the name to Reuse other workflow and add a manual trigger.
Solution
name: Reuse other workflow

on: [workflow_dispatch]
  1. 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: '@octocat'
  1. Add another job use-output that writes the output parameter current-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 }}"
  1. Run the workflow and observe the output.

Summary

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.