Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Housekeeping] Cache Node Modules #1494

Merged
merged 15 commits into from
May 4, 2021

Conversation

MadsBuchmann
Copy link
Contributor

@MadsBuchmann MadsBuchmann commented Apr 23, 2021

This PR closes #1360

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Enhancement (to existing content)
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • cookbook application / infrastructure changes
  • Other... Please describe:

Which issue documents the current behaviour?

Issue Number: #1360

What is the new behavior?

Before installing dependencies it is attempted to fetch a cached version of node_modules using a cache-key. If there is a cache-hit, the cached node_modules are used and npm run postinstall is executed. In the case of a cache miss, a clean install is executed by running npm ci - the resulting node_modules is then cached.

The cache-key contains:

  • the os
  • the node version
  • a hash of package.json
  • a hash of package-lock.json

The OS and Node version such that we only retrieve caches created in the same environment, and the hash of package.json such that we only retrieve caches created with the same dependencies, post-install scripts etc.

Why use a mix of npm ci and npm run postinstall
It seems common consensus across the cool tech blogs is to not cache node_modules as npm ci does a clean install and therefore deletes this folder as part of the process.

Instead you should:

  1. Use a cached version of ~/.npm which is the cache NPM uses to speed up package installation.
  2. Run npm ci to create a re-creatable deploy from package-lock.json which will be sped up due to the cached ~/.npm

I started out by doing that and it did speed up the install step a tiny bit (like 20 seconds or something). But the majority of the time spent during the install step, actually came from our post-installation scripts which among other things runs ngcc. The result of ngcc is stored in node_modules.

So I opted for a sort of mixed strategy:

  • Check if there exists a cache that uses the same: 0S, node version, package.json and package-lock.json.
    • If there is not: Do a clean install (npm ci) which runs post-install scripts and cache the results. This is a strict install as no changes to package-lock.json should occur (npm ci fails if package.json and package-lock.json is out of sync).
    • If there is: Use the cached node_modules and just run npm run postinstall! We can do this safely, because we know the cache originates from a strict install, that used the same package.json and package-lock.json! We need to run postinstall as it runs some transpilation among other things that outputs to other folders than node_modules.

Pipeline when there is a cache-hit:
Screenshot 2021-04-28 at 11 18 35

Pipeline when there is a cache-miss:
Screenshot 2021-04-28 at 11 04 58

In general the install steps has gone from taking approx 2 minutes to 22 seconds, as long as there is a cache :)

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I've created two issues for additional improvement of our CI pipeline:

@github-actions github-actions bot temporarily deployed to pr-1360-cache-node-modules April 23, 2021 11:12 Inactive
@github-actions github-actions bot temporarily deployed to pr-1360-cache-node-modules April 28, 2021 09:36 Inactive
@github-actions github-actions bot temporarily deployed to pr-1360-cache-node-modules April 28, 2021 09:38 Inactive
@github-actions github-actions bot temporarily deployed to pr-1360-cache-node-modules April 28, 2021 09:40 Inactive
@MadsBuchmann MadsBuchmann changed the title Housekeeping/1360 cache node modules [Housekeeping] Cache Node Modules Apr 28, 2021
@github-actions github-actions bot temporarily deployed to pr-1360-cache-node-modules April 28, 2021 13:44 Inactive
@MadsBuchmann MadsBuchmann marked this pull request as ready for review April 28, 2021 14:09
.github/workflows/publish.yml Outdated Show resolved Hide resolved
@github-actions github-actions bot temporarily deployed to pr-1360-cache-node-modules May 3, 2021 11:15 Inactive
@github-actions github-actions bot temporarily deployed to pr-1360-cache-node-modules May 3, 2021 11:20 Inactive
@github-actions github-actions bot temporarily deployed to pr-1360-cache-node-modules May 3, 2021 11:47 Inactive
@MadsBuchmann MadsBuchmann requested a review from jakobe May 3, 2021 11:52
@MadsBuchmann MadsBuchmann enabled auto-merge (squash) May 3, 2021 13:15
@MadsBuchmann MadsBuchmann merged commit ac11c84 into master May 4, 2021
@MadsBuchmann MadsBuchmann deleted the housekeeping/1360-cache-node-modules branch May 4, 2021 07:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Housekeeping] Cache node_modules in GitHub Actions to speed up builds
2 participants