Boost GitHub Action Performance and Save Money
Published at
Photo by Todd Diemer on Unsplash
Contents
Intro
While working on component library which I made few weeks ago, I noticed that the GitHub workflow actions I set up were taking nearly 6 minutes to complete.
Here’s a list of my GitHub actions:
- Unit test: (install dependencies, run unit tests)
- Size limit: (install dependencies, build the project, and check the size of build)
- Release: (install dependencies, build the project, create tags, and publish the package)
As you can see, “install dependencies” is common across all these actions and consumes significant time. I researched how to cache these dependencies and found a solution.
Solution
In your GitHub workflow action, before installing dependencies, you can add this step to cache them:
yarn:
- name: Cache Node Modules
uses: actions/cache@v4
with:
path: |
~/.yarn/cache
node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
npm:
- name: Cache Node Modules
uses: actions/cache@v4
with:
path: |
~/.npm
node_modules
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
Result
By implementing this cache system in our GitHub workflow action, I reduced the runtime of our GitHub Actions in this project from 6 minutes to just 2.30 seconds. Not only did this improve speed, but it also reduced our GitHub costs. We’re saving money by enhancing performance!