Stop Duplicating Code! How GitHub Actions Reusable Workflows Can Save Your CI/CD Pipeline.

Stop Duplicating Code! How GitHub Actions Reusable Workflows Can Save Your CI/CD Pipeline.

1. What Are Reusable Workflows?

Reusable workflows are a feature of GitHub Actions that allow you to define a workflow in one place and reuse it across multiple repositories or even within the same repository. Instead of copying and pasting the same workflow logic everywhere, you can centralize it and call it wherever needed. This not only reduces duplication but also makes your workflows easier to maintain.

2. Why Use Reusable Workflows?

Here are three compelling reasons to start using reusable workflows:

a. Consistency

By centralizing your workflow logic, you ensure that all your projects follow the same standards. Whether it’s running tests, building artifacts, or deploying to production, reusable workflows guarantee consistency across your codebase.

b. Maintainability

Updating a workflow in one place is far easier than updating multiple copies of the same workflow. With reusable workflows, you can make changes once and have them propagate everywhere the workflow is used.

c. Flexibility with Parameters

Reusable workflows can accept parameters, allowing you to customize their behavior for different use cases. For example, you can pass different environment variables, runtime versions, or deployment targets without duplicating code.

3. A Real-World Example: Testing Across Multiple Node.js Versions

Let’s say you’re building a Node.js application, and you need to test it against multiple Node.js versions (e.g., 14.x and 16.x). Instead of writing separate workflows for each version, you can create one reusable workflow and call it twice with different parameters.

Step 1: Define the Reusable Workflow

Create a reusable workflow in your repository (e.g., .github/workflows/build-and-test.yml):


Article content
.github/workflows/build-and-test.yml

Step 2: Call the Reusable Workflow Twice

In your main workflow file (e.g., .github/workflows/ci.yml), call the reusable workflow twice with different Node.js versions:


Article content
.github/workflows/ci.yml

What Happens?

  • The reusable workflow is triggered twice: once for Node.js 14.x and once for 16.x.
  • Both runs will check out the code, set up the specified Node.js version, install dependencies, and run tests.

More examples ?

You can other real example in github documentation or in Chuteny-testing workflows.

4. Conclusion: Reusable Workflows Are a Game-Changer

GitHub Actions reusable workflows are a powerful tool for simplifying and scaling your CI/CD pipelines. By centralizing your workflow logic and using parameters, you can achieve consistency, maintainability, and flexibility across your projects. Whether you’re testing against multiple runtime versions, deploying to different environments, or running the same workflow for multiple platforms, reusable workflows can save you time and effort.

If you’re not already using reusable workflows, now is the time to start. Your future self (and your team) will thank you! 🚀


To view or add a comment, sign in

More articles by Karim Goubbaa

Others also viewed

Explore content categories