Lab: Speed Up Node.js CI with GitHub Actions Cache
Introduction
In this lab, you will learn how to use caching in GitHub Actions to speed up a Node.js application’s build by avoiding redundant dependency installation. You’ll compare two workflows: one without caching and one using the actions/cache
action.
You’ll use the existing express-hello-world
Node.js project located at /src/javascript/express-hello-world
Estimated Duration: 20–30 minutes
Instructions
Step 1: Open your repository
- Navigate to the repository containing the
express-hello-world
project. - Ensure the following file exists ` src/javascript/express-hello-world/package.json`
- If needed, test the app locally to verify it’s working:
cd src/javascript/express-hello-world
npm install
node server.js
Step 2: Create the workflow without cache
-
In the GitHub web UI, go to the Code tab.
-
Navigate to the
.github/workflows
directory. Create the folders if they don’t exist:- Click Add file > Create new file
- Name the file:
.github/workflows/build-no-cache.yml
-
Paste the following content:
name: Express Server without cache on: workflow_dispatch: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: '18' - name: Install dependencies run: | cd src/javascript/express-hello-world npm install - name: Show installed packages run: | cd src/javascript/express-hello-world npm list
-
Scroll down and click Commit new file.
Step 3: Run the workflow and check time taken
- Go to the Actions tab.
- Click on the Node.js CI without cache workflow.
- Click Run workflow > Run workflow (manually trigger it).
- After it completes, expand the Install dependencies step and note the duration.
Step 4: Create the workflow with cache
-
In the same
.github/workflows
folder, create another file namedbuild-with-cache.yml
-
Paste the following content:
name: Express Server with cache on: workflow_dispatch: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: '18' - name: Cache npm dependencies uses: actions/cache@v3 with: path: ~/.npm key: $-node-$ restore-keys: | $-node- - name: Install dependencies run: | cd src/javascript/express-hello-world npm install - name: Show installed packages run: | cd src/javascript/express-hello-world npm list
-
Commit the file.
Step 5: Run the workflow twice and compare
-
Navigate to the Actions tab.
-
Run the Node.js CI with cache workflow manually.
- First run: Installs dependencies and creates the cache.
- Second run: Restores the cache and speeds up installation.
-
Expand the Install dependencies step in both runs and compare durations.
Summary
In this lab, you created two GitHub Actions workflows for a Node.js app — one with caching and one without. You observed how dependency installation time was significantly reduced when using the actions/cache
step with .npm
as the cache path.
Caching is useful when:
- Dependencies are downloaded often
- Builds or installs are expensive
- You want faster workflows on repeated runs