Master GitHub Actions with hands-on labs and exercises. Learn how to automate workflows, run tests, deploy applications, and more using GitHub's powerful automation platform. This repository has everything you need to get started with continuous integration and continuous deployment.
In this lab, you will learn how to create and use reusable workflows in GitHub Actions. A reusable workflow enables modularity by defining common steps in one workflow that can be invoked by other workflows.
This lab covers:
Duration: 20-30 minutes
Navigate to Your Repository:
Create the Workflow File:
.github/workflows
directory, create a new file named reusable-workflow-echo.yml
.Define the Reusable Workflow:
Add the following YAML code:
name: Reusable Workflow Echo
on:
workflow_call:
inputs:
my-input:
required: true
type: string
jobs:
echo:
runs-on: ubuntu-latest
steps:
- name: Echo input
run: echo $
workflow_call
: Specifies that this is a reusable workflow.inputs
: The my-input
parameter is required and will accept a string.echo
: Runs on an Ubuntu environment and echoes the provided input.Commit the Workflow:
Save and commit the file:
git add .github/workflows/reusable-workflow-echo.yml
git commit -m "Add reusable workflow"
git push
Create the Workflow File:
.github/workflows
directory, create another file named reusable-workflow-echo-caller.yml
.Define the Caller Workflow:
Add the following YAML code:
name: Reusable Workflow Echo Caller
on:
workflow_dispatch:
push:
paths:
- '.github/workflows/reusable-workflow-echo-caller.yml'
jobs:
call:
uses: ./.github/workflows/reusable-workflow-echo.yml
with:
my-input: 'Hello, world!'
workflow_dispatch
: Allows manual execution of this workflow.push
: Automatically triggers the workflow if changes are made to the caller workflow file.uses
: Calls the reusable workflow reusable-workflow-echo.yml
.with
: Supplies the required input my-input
with the value 'Hello, world!'
.Commit the Caller Workflow:
Save and commit the file:
git add .github/workflows/reusable-workflow-echo-caller.yml
git commit -m "Add reusable workflow caller"
git push
Manually Trigger the Caller Workflow:
Reusable Workflow Echo Caller
workflow.View the Output:
Echo input
step under the call
job.Verify that the output is:
Hello, world!
Modify the Input Parameter:
Edit the reusable-workflow-echo-caller.yml
file:
with:
my-input: 'Reusable workflows are powerful!'
Commit and push the changes:
git add .github/workflows/reusable-workflow-echo-caller.yml
git commit -m "Update input parameter"
git push
Re-trigger the Workflow:
Reusable Workflow Echo Caller
workflow.In this lab, you have learned how to:
Reusable workflows help improve the modularity, maintainability, and scalability of your CI/CD pipelines.