I want to make a GitHub Actions pipeline with 3 jobs:
- dependency installation with
npm install
- linting with
npm run lint
:"lint": "eslint api/ --ext .js,.jsx ."
- testing with
npm run test
:"test": "jest --ci --forceExit --detectOpenHandles --runInBand --verbose --logHeapUsage"
My problem is that, for both linting and testing jobs, it’s a prerequisite to install all my dependencies in order to run ESLint or Jest.
I would like to solve it with having the dependencies installed once by a job and then sharing my node_modules folder with the linting and testing jobs without needing them having to reinstall all my dependencies.
My package structure is the following:
.github folder
client folder
--package.json
server folder
--package.json
Currently my CI workflow file looks like this:
name: CI Pipeline
on:
push:
branches: [ main, develop ]
jobs:
dependency-install-server:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 16
uses: actions/setup-node@v3
with:
node-version: 16
- name: Install dependencies - server
working-directory: ./server
run: npm install
- name: Cache dependencies - server
uses: actions/cache@v3
with:
path: |
./server/node_modules
./client/node_modules
key: node_modules-${{ hashFiles('./server/package-lock.json') }}
restore-keys: |
./server/node_modules
./client/node_modules
dependency-install-client:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 16
uses: actions/setup-node@v3
with:
node-version: 16
- name: Install dependencies - client
working-directory: ./client
run: npm install
lint-server:
needs: [dependency-install-server]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 16
uses: actions/setup-node@v3
with:
node-version: 16
- name: Lint server code
working-directory: ./server
run: npm run lint
lint-client:
needs: [dependency-install-client]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 16
uses: actions/setup-node@v3
with:
node-version: 16
- name: Lint client code
working-directory: ./client
run: npm run lint
test-server:
needs: [dependency-install-server]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 16
uses: actions/setup-node@v3
with:
node-version: 16
- name: Run tests - server
working-directory: ./server
run: npm run test
test-client:
needs: [dependency-install-client]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 16
uses: actions/setup-node@v3
with:
node-version: 16
- name: Run tests - client
working-directory: ./client
run: npm run test
My first attempt was to use a cache but in the meantime I realized that it only caches within the actual job and I can’t share my node_modules folder between other jobs.
Is it possible to share my node_modules folder between jobs?
If not, what alternative solution would be the most optimal for my project? I was thinking of splitting my current jobs into separate workflow files and using caching on each of them when I install the dependencies.
1