Skip to content

Commit

Permalink
update npm.md
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelCurrin committed May 29, 2024
1 parent b7d206b commit a1108d0
Showing 1 changed file with 29 additions and 57 deletions.
86 changes: 29 additions & 57 deletions recipes/ci-cd/github-actions/workflows/node/npm.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,103 +20,74 @@ 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

<!-- TODO move to cheatsheets and link from here - similar to CLI usage guides this is actions usage -->

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):

- `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
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
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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 %}

0 comments on commit a1108d0

Please sign in to comment.