From a1108d0d7d8e6e97091f7b2aaf01846659a165c2 Mon Sep 17 00:00:00 2001 From: Michael Currin <18750745+MichaelCurrin@users.noreply.github.com> Date: Wed, 29 May 2024 14:14:04 +0200 Subject: [PATCH] update npm.md --- .../github-actions/workflows/node/npm.md | 86 +++++++------------ 1 file changed, 29 insertions(+), 57 deletions(-) diff --git a/recipes/ci-cd/github-actions/workflows/node/npm.md b/recipes/ci-cd/github-actions/workflows/node/npm.md index b7e6251b..db92807f 100644 --- a/recipes/ci-cd/github-actions/workflows/node/npm.md +++ b/recipes/ci-cd/github-actions/workflows/node/npm.md @@ -7,7 +7,9 @@ description: Patterns for using Node.js and NPM in a GH Actions workflow See the GH docs page on [Using Node.js with GitHub Actions](https://docs.github.com/en/actions/language-and-framework-guides/using-nodejs-with-github-actions). Some of the samples come from here. -To build a Node app (such as React or Vue) and serve it with GH Pages, see the [Deploy GH Pages]({% link recipes/ci-cd/github-actions/workflows/deploy-gh-pages/index.md %}) section of this cookbook site. +To build a Node app (such as React or Vue) and serve it with GH Pages, see the [Deploy GH Pages][] section of this cookbook site. + +See [npm ci installs][] for use of `npm ci` instead of `npm install`. ## Set up Node action @@ -18,77 +20,46 @@ See [Set up Node.js Environment](https://github.com/marketplace/actions/setup-no > Set up a Node.js environment by adding problem matchers and optionally downloading and adding it to the PATH -### Options - - - -Sample from GH Actions recommendation. Note that `v2` does not work but `v2.1.1` and `v2-beta` are available (August 2020). - -- `main.yml` - ```yaml - steps: - - name: Set up Node.js environment - uses: actions/setup-node@v2-beta - with: - # Set always-auth in npmrc - always-auth: # optional, default is false - - # Version Spec of the version to use. Examples: 12.x, 10.15.1, >=10.15.0 - node-version: # optional - - # Set this option if you want the action to check for the latest available version that satisfies the version spec - check-latest: # optional - - # Optional registry to set up for auth. Will set the registry in a project level .npmrc and .yarnrc file, and set up auth to read in from env.NODE_AUTH_TOKEN - registry-url: # optional - - # Optional scope for authenticating against scoped registries - scope: # optional - - # Used to pull node distributions from node-versions. Since there's a default, this is typically not supplied by the user. - token: # optional, default is ${{ github.token }} - - # Deprecated. Use node-version instead. Will not be supported after October 1, 2019 - version: # optional - ``` - ## Samples ### Basic -Use single Node.js version of say `16`, `16.3.0`, or `lts/erbium` (NVM syntax). +Use a single Node.js version of say `16`, `16.3.0`, or `lts/erbium` (NVM syntax). -I created this based on the other samples. +I created this based on the other samples and the [setup-node](https://github.com/actions/setup-node) action README. - `main.yml` ```yaml steps: - name: Checkout 🛎️ - uses: actions/checkout@v2 + uses: actions/checkout@master - name: Set up Node.js - uses: actions/setup-node@v2 + - uses: actions/setup-node@v4 with: - node-version: '16' + node-version: 20 + cache: 'npm' - name: Install dependencies - run: npm install - - - name: Build - run: npm run build + run: npm - - name: Test + - name: Test run: npm test env: CI: true + + - name: Build + run: npm run build ``` ### Cache dependencies You can reduce build time if dependencies are cached between builds. -There are two recommended approaches here. Both require `package-lock.json` to be in version control. +There are two recommended approaches here. Both require `package-lock.json` to be in version control. And both will cache tarballs in `~/.npm` - this is useful since `node_modules` get deleted by `npm ci` and so caching that would be useless. + +#### Setup-node action cache Built-in to `setup-node`, based on [doc](https://github.com/actions/setup-node#caching-packages-dependencies): @@ -96,25 +67,27 @@ Built-in to `setup-node`, based on [doc](https://github.com/actions/setup-node#c ```yaml steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@master - name: Set up Node.js - uses: actions/setup-node@v2 + uses: actions/setup-node@v4 with: - node-version: '16' + node-version: 20 cache: 'npm' - name: Install dependencies - run: npm install + run: npm ci ``` +#### Cache action + Using a separate `cache` action, based on GH docs: - `main.yml` ```yaml steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@master - name: Set up Node.js uses: actions/setup-node@v1 @@ -131,13 +104,9 @@ Using a separate `cache` action, based on GH docs: ${{ runner.OS }}- - name: Install dependencies - run: npm install + run: npm ci ``` -For the cache action, it looks like this depends on a lockfile existing. I don't like to commit that - perhaps `package.json` can be used instead. - -The docs actually use `npm ci` which will _delete_ `node_modules`, so I've set up `npm install` here instead. - ### Matrix @@ -159,7 +128,7 @@ This sample comes from the docs and is similar to the default covered in the exa steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@master - name: Set up Node.js ${{ matrix.node-version }} uses: actions/setup-node@v2 @@ -222,3 +191,6 @@ From the GitHub Action samples: ``` {% endraw %} + +[Deploy GH Pages]: {% link recipes/ci-cd/github-actions/workflows/deploy-gh-pages/index.md %} +[npm ci installs]: {% link recipes/ci-cd/github-actions/workflows/node/ci-installs.md %}