Lab: Introduction to Reusable Workflows
Objective
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:
- Creating a reusable workflow.
- Defining a caller workflow to invoke the reusable workflow.
Duration: 20-30 minutes
Prerequisites
- GitHub Repository: Ensure you have access to a repository where you can create workflows.
- Basic Knowledge of YAML: Familiarity with GitHub Actions workflow syntax is beneficial.
Instructions
Step 1: Create a Reusable Workflow
-
Navigate to Your Repository:
- Open your GitHub repository in Visual Studio Code or your preferred IDE.
-
Create the Workflow File:
- In the
.github/workflows
directory, create a new file namedreusable-workflow-echo.yml
.
- In the
-
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 $
Explanation
workflow_call
: Specifies that this is a reusable workflow.inputs
: Themy-input
parameter is required and will accept a string.- Job
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
-
Step 2: Create a Caller Workflow
-
Create the Workflow File:
- In the
.github/workflows
directory, create another file namedreusable-workflow-echo-caller.yml
.
- In the
-
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!'
Explanation
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 workflowreusable-workflow-echo.yml
.with
: Supplies the required inputmy-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
-
Step 3: Trigger and Verify the Workflows
-
Manually Trigger the Caller Workflow:
- Navigate to the Actions tab in your GitHub repository.
- Select the
Reusable Workflow Echo Caller
workflow. - Click Run workflow to manually trigger it.
-
View the Output:
- Open the workflow run details.
- Look for the
Echo input
step under thecall
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:
- Navigate to the Actions tab and rerun the
Reusable Workflow Echo Caller
workflow. - Verify the new input value is echoed in the logs.
- Navigate to the Actions tab and rerun the
Summary
In this lab, you have learned how to:
- Create a reusable workflow with input parameters.
- Call the reusable workflow using a caller workflow.
- Pass dynamic parameters from the caller to the reusable workflow.
Reusable workflows help improve the modularity, maintainability, and scalability of your CI/CD pipelines.