Hi,
I have a project in which I want to run 3 jobs in parallel, but I want to install npm dependencies only once and cache the resulting node_modules
:
test-unit
- run unit teststest-integration
- run integration testsbuild-sam
- Build an AWS SAM (Serverless Application Model)
The installation of node modules and caching them is encapsulated in the install-deps-with-cache
command which I use in all the above 3 jobs.
My intuition is that if the cache key already exists (if the package.json
and package-lock.json
weren’t changed from a previous commit) then I guess the 3 parallel jobs will be able to use it.
However, what happens if the cache key doesn’t exist (either package.json
or package-lock.json
were changed)?
Will each of the 3 jobs that run in parallel install the npm dependencies, which is a bit wasteful?
Or is CircleCI somehow manage to reuse the cache even though the jobs run in parallel?
commands:
install-deps-with-cache:
description: Install dependencies by using the cache if exists
steps:
- restore_cache:
key: v1-{{ checksum "package.json" }}-{{ checksum "package-lock.json" }}
working_directory: ~/project
- run:
name: Install dependencies
command: |
if [ -d 'node_modules' ]
then
echo "restored node_modules from cache!"
else
npm ci
fi
working_directory: ~/project
- save_cache:
key: v1-{{ checksum "package.json" }}-{{ checksum "package-lock.json" }}
paths: node_modules
working_directory: ~/project
jobs:
test-unit:
docker:
- image: cimg/node:18.16.1
steps:
- checkout
- install-deps-with-cache
- run:
name: Test Unit
command: npm run test:unit
working_directory: ~/project
test-integration:
docker:
- image: cimg/node:18.16.1
steps:
- checkout
- install-deps-with-cache
- run:
name: Test Integration
command: npm run test:integration
working_directory: ~/project
build-sam:
docker:
- image: 563186419109.dkr.ecr.us-east-1.amazonaws.com/build-images:sam-node-18
steps:
- checkout
- install-deps-with-cache
- run:
name: Build template
command: sam build
working_directory: ~/project
workflows:
version: 2
package:
jobs:
- test-unit:
context: all
- test-integration:
context: all
- build-sam:
context: all
- deploy:
name: deploy-staging
context: all
deploy-env: staging
notify-slack: false
requires:
- build-sam