Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/c-hive/gha-npm-cache

1-liner npm install cache for GitHub Actions
https://github.com/c-hive/gha-npm-cache

Last synced: 2 months ago
JSON representation

1-liner npm install cache for GitHub Actions

Awesome Lists containing this project

README

        

## Deprecated

This functionality is now part of [`actions/setup-node`](https://github.com/actions/setup-node):

```yml
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
cache: 'npm'
```

---

# gha-npm-cache

#### 1-liner npm install cache for GitHub Actions

Status and support

- ✔ stable
- ✖ not supported
- ✖ no ongoing development

[![CI](/../../workflows/CI/badge.svg?branch=master)](/../../actions)

GitHub Action caches improve build times and reduce network dependencies. However, writing the correct cache logic is [tricky](https://github.com/actions/cache/blob/9ab95382c899bf0953a0c6c1374373fc40456ffe/examples.md#node---npm). You need to understand how the [cache action](https://github.com/actions/cache) (keys and restore keys) work. Did you know you're not supposed to cache the `node_modules` folder? The setup is different per OS and takes a lot of space in your workflows. Not anymore!

`gha-npm-cache` is a simple 1-liner that covers all use-cases, correctly:
- Caches the NPM cache directory instead of `node-modules` [as recommended](https://github.com/actions/cache/blob/9ab95382c899bf0953a0c6c1374373fc40456ffe/examples.md#node---npm)
- Works on Ubuntu, MacOS and Windows
- Restore keys take the OS into account [as recommended](https://github.com/actions/cache/blob/9ab95382c899bf0953a0c6c1374373fc40456ffe/examples.md#node---npm)
- Builds on the [native cache functionality of GitHub Actions](https://github.com/actions/toolkit/tree/master/packages/cache), same as [v2 of the generic cache action](https://github.com/actions/cache/issues/55#issuecomment-629433225)

## Usage

Add this step before `npm install`:
```yml
- uses: c-hive/gha-npm-cache@v1
```

For example:

`.github/workflows/ci.yml`
```yml
name: CI

on: [push]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1

- uses: c-hive/gha-npm-cache@v1
# with:
# directory: server # Optional, specify the folder of package-lock.json if not in the root directory

- name: Install JS dependencies
run: npm install
- name: Test
run: npm run test
```

### Solution comparison

[Native](https://github.com/actions/cache/blob/9ab95382c899bf0953a0c6c1374373fc40456ffe/examples.md#node---npm)
```yml
- name: Get npm cache directory
id: npm-cache
run: |
echo "::set-output name=dir::$(npm config get cache)"
- uses: actions/cache@v1
with:
path: ${{ steps.npm-cache.outputs.dir }}
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
```

`gha-npm-cache`
```yml
- uses: c-hive/gha-npm-cache@v1
```

## Similar actions

- [gha-yarn-cache](https://github.com/c-hive/gha-yarn-cache)

## Conventions

This project follows [C-Hive guides](https://github.com/c-hive/guides) for code style, way of working and other development concerns.

## License

The project is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).