Skip to main content
Optimize your Node.js CI by extracting repeated caching and installation steps into a reusable composite action. This improves maintainability, reduces duplication, and speeds up workflow authorship.

Original Workflow Snippet

The following job explicitly checks out code, sets up Node.js, caches dependencies, installs packages, runs tests, and archives results:

Replace Cache & Install with Composite Action

Instead of repeating the cache and install steps in every job, call your composite action:
Reference the action directory (npm-action) without specifying action.yml; GitHub locates it automatically.

Defining the Composite Action

Create .github/custom-actions/npm-action/action.yml to define inputs and steps:

Supported Shells

The image shows a GitHub Docs page detailing workflow syntax for GitHub Actions, including supported platforms, shell parameters, descriptions, and command run details.

Passing Inputs to the Composite Action

When calling the composite action, supply the path-of-folder input:
You can also use expressions, variables, or secrets:

Applying to a Code Coverage Job

Reuse the same composite action in a code-coverage job to cache dependencies before running coverage reports:

Viewing Workflow Runs

After pushing your changes, navigate to the Actions tab in your repository to review each run, monitor status, and investigate logs:
The image shows a GitHub Actions interface displaying the "Solar System Workflow" with a list of workflow runs and their statuses. The sidebar includes options for managing caches, deployments, and runners.
To inspect the Unit Testing job logs and progress, click the Unit Testing entry in the run detail.
The image shows a GitHub Actions workflow interface with a workflow in progress, displaying job statuses and a visual representation of the workflow steps.

Composite Action Logs

Expand the composite action step to view cache restoration and installation details:
To verify the Code Coverage job used the composite action, check the job details in the run summary.
The image shows a GitHub Actions workflow interface with a list of jobs and their statuses, focusing on "Code Coverage" which has succeeded. The sidebar displays various jobs like unit testing and report uploads.

Next Steps

This composite action lives in your repository and is available to all your workflows. To publish it to the GitHub Marketplace, explore these resources:

Watch Video