Make a workflow reusable, call it in another workflow, and use a matrix strategy to run multiple versions.
Welcome to "Reusable Workflows and Matrix Strategies"! 👋
You can do a lot with GitHub Actions! You can automate repetitive tasks, build continuous integration and continuous deployment pipelines, and customize essentially any part of your software development workflow. It doesn't matter if you're just learning about workflows and GitHub Actions for the first time or you're well exerpienced with the process, you'll quickly find yourself repeating automation jobs and steps within the same workflow, and even using the dreaded copy and paste method for workflows across multiple repositories.
Is there a solution to reduce these repetitive tasks? Yes, I'm glad you asked 😉 Enter reusable workflows, a simple and powerful way to avoid copying and pasting workflows across your repositories.
What are the benefits of using reusable workflows?: Reusable workflows are … reusable. Reusable workflows let you DRY (don’t repeat yourself) your Actions configurations, so you don’t need to copy and paste your workflows from one repository to another.
- Case in point: if you have three different Node applications and you’re building them all the same way, you can use one reusable workflow instead of copying and pasting your workflows again and again.
I have a workflow, how do I make it reusable?: A reusable workflow is just like any GitHub Actions workflow with one key difference: it includes a workflow_call
event trigger, similar to event triggers like push
, issues
, and workflow_dispatch
. This means that all you need to do to make a workflow reusable is to use the workflow call trigger.
Let's get started with our first step to see how this would work!
-
Open a new browser tab, and navigate to this same repository. Then, work on the steps in your second tab while you read the instructions in this tab.
-
Navigate to the Code tab.
-
From the main branch dropdown, click on the reusable-workflow branch.
-
Navigate to the
.github/workflows/
folder, then select the reusable-workflow.yml file. -
Replace the
workflow_dispatch
event trigger with theworkflow_call
event trigger. It should look like the following:name: Reusable Workflow on: workflow_call: inputs: node: required: true type: string
-
To commit your changes, click Start commit, and then Commit changes.
-
(optional) Create a pull request to view all the changes you'll make throughout this course. Click the Pull Requests tab, click New pull request, set
base: main
andcompare: reusable-workflow
. -
Wait about 20 seconds for actions to run, then refresh this page (the one you're following instructions from) and an action will automatically close this step and open the next one.
Get help: Post in our discussion board • Review the GitHub status page
© 2023 GitHub • Code of Conduct • MIT License