| Building and testing Node.js | ||||||||
| Learn how to create a continuous integration (CI) workflow to build and test your Node.js project. | ||||||||
|
||||||||
|
||||||||
| Node.js | ||||||||
|
||||||||
| tutorials |
{% data reusables.actions.enterprise-github-hosted-runners %}
This guide shows you how to create a continuous integration (CI) workflow that builds and tests Node.js code. If your CI tests pass, you may want to deploy your code or publish a package.
We recommend that you have a basic understanding of Node.js, YAML, workflow configuration options, and how to create a workflow file. For more information, see:
{% data reusables.actions.enterprise-setup-prereq %}
{% data reusables.actions.workflow-templates-get-started %}
{% data variables.product.prodname_dotcom %} provides a workflow template for Node.js that should work for most Node.js projects. The subsequent sections of this guide give examples of how you can customize this workflow template.
{% data reusables.repositories.navigate-to-repo %} {% data reusables.repositories.actions-tab %} {% data reusables.actions.new-starter-workflow %}
{%- ifversion ghes %}
If you don't find the "Node.js" workflow template, copy the following workflow code to a new file called node.js.yml in the .github/workflows directory of your repository.
{%- endif %}
{% ifversion fpt or ghec %} The node.js.yml workflow file is added to the .github/workflows directory of your repository. {% endif %}
The easiest way to specify a Node.js version is by using the setup-node action provided by {% data variables.product.prodname_dotcom %}. For more information see, setup-node.
The setup-node action takes a Node.js version as an input and configures that version on the runner. The setup-node action finds a specific version of Node.js from the tools cache on each runner and adds the necessary binaries to PATH, which persists for the rest of the job. Using the setup-node action is the recommended way of using Node.js with {% data variables.product.prodname_actions %} because it ensures consistent behavior across different runners and different versions of Node.js. If you are using a self-hosted runner, you must install Node.js and add it to PATH.
The workflow template includes a matrix strategy that builds and tests your code with the Node.js versions listed in node-version. The 'x' in the version number is a wildcard character that matches the latest minor and patch release available for a version. Each version of Node.js specified in the node-version array creates a job that runs the same steps.
Each job can access the value defined in the matrix node-version array using the matrix context. The setup-node action uses the context as the node-version input. The setup-node action configures each job with a different Node.js version before building and testing code. For more information about matrix strategies and contexts, see AUTOTITLE and AUTOTITLE.
Alternatively, you can build and test with exact Node.js versions.
Or, you can build and test using a single version of Node.js too.
If you don't specify a Node.js version, {% data variables.product.prodname_dotcom %} uses the environment's default Node.js version. For more information, see AUTOTITLE.
{% data variables.product.prodname_dotcom %}-hosted runners have npm and Yarn dependency managers installed. You can use npm and Yarn to install dependencies in your workflow before building and testing your code. The Windows and Linux {% data variables.product.prodname_dotcom %}-hosted runners also have Grunt, Gulp, and Bower installed.
You can also cache dependencies to speed up your workflow. For more information, see AUTOTITLE.
This example installs the versions in the package-lock.json or npm-shrinkwrap.json file and prevents updates to the lock file. Using npm ci is generally faster than running npm install. For more information, see npm ci and Introducing npm ci for faster, more reliable builds.
Using npm install installs the dependencies defined in the package.json file. For more information, see npm install.
This example installs the dependencies defined in the yarn.lock file and prevents updates to the yarn.lock file. For more information, see yarn install.
Alternatively, you can install the dependencies defined in the package.json file.
{% data reusables.actions.setup-node-intro %}
To authenticate to your private registry, you'll need to store your npm authentication token as a secret. For example, create a repository secret called NPM_TOKEN. For more information, see AUTOTITLE.
In the example below, the secret NPM_TOKEN stores the npm authentication token. The setup-node action configures the .npmrc file to read the npm authentication token from the NODE_AUTH_TOKEN environment variable. When using the setup-node action to create an .npmrc file, you must set the NODE_AUTH_TOKEN environment variable with the secret that contains your npm authentication token.
Before installing dependencies, use the setup-node action to create the .npmrc file. The action has two input parameters. The node-version parameter sets the Node.js version, and the registry-url parameter sets the default registry. If your package registry uses scopes, you must use the scope parameter. For more information, see npm-scope.
The example above creates an .npmrc file with the following contents:
You can cache and restore the dependencies using the setup-node action.
The following example caches dependencies for npm.
The following example caches dependencies for Yarn.
The following example caches dependencies for pnpm (v6.10+).
If you have a custom requirement or need finer controls for caching, you can use the cache action. For more information, see AUTOTITLE.
You can use the same commands that you use locally to build and test your code. For example, if you run npm run build to run build steps defined in your package.json file and npm test to run your test suite, you would add those commands in your workflow file.
You can save artifacts from your build and test steps to view after a job completes. For example, you may need to save log files, core dumps, test results, or screenshots. For more information, see AUTOTITLE.
You can configure your workflow to publish your Node.js package to a package registry after your CI tests pass. For more information about publishing to npm and {% data variables.product.prodname_registry %}, see AUTOTITLE.