Skip to main content
Integrating a composite action into your GitHub Actions workflow helps you DRY up repetitive steps—such as caching dependencies and installing packages—so your CI pipeline stays clean and maintainable.

Original Workflow

The workflow below sets up Node.js, caches dependencies, installs packages, runs tests, and uploads test results:

Refactoring with a Composite Action

Instead of repeating cache and install steps, reference a custom composite action stored under .github/custom-actions/npm-action:
You can omit the action.yml filename and simply point to the directory containing your composite action.

Comparison of Workflows

Defining the Composite Action

Below is the action.yml for our reusable NPM composite action. It accepts a required path-of-folder input and runs cache & install steps:
Every run step in a composite action must specify a shell (e.g., bash, pwsh, sh, cmd) depending on the runner OS.
The image shows a GitHub Docs page detailing the workflow syntax for GitHub Actions, including supported platforms, shell parameters, and command run details. The left sidebar lists various topics related to GitHub Actions, such as using workflows and workflow syntax.

Running the Refactored Workflow

After committing your composite action and workflow changes, view the GitHub Actions dashboard. You’ll see the unified “Cache & Install NPM Packages” step replace the individual cache and install tasks across all jobs:
The image shows a GitHub Actions workflow interface with a "solar-system.yml" file in progress. It displays a series of jobs including unit testing and code coverage, with a visual representation of the workflow steps.
The image shows a GitHub Actions workflow interface with a focus on a unit testing job for an Ubuntu environment. It details steps like setting up a job, initializing containers, and a composite action for caching and installing NPM packages.
The image shows a GitHub Actions workflow interface with a series of jobs and their statuses, including unit testing, code coverage, and deployment steps.
By consolidating common operations into a composite action, you keep workflows consistent and scalable. To share your action with the community, consider publishing it on the GitHub Marketplace.

References

Watch Video