Maven / GitHub Actions Interview Questions
How do you cache dependencies in GitHub Actions using actions/cache?
actions/cache saves and restores a directory between workflow runs so that package managers like npm, Maven, or pip do not re-download the same dependencies on every run. A cache hit can reduce a 3-minute install step to a few seconds.
The action requires two inputs: path (the directory to cache) and key (a string that identifies the cache). If the key matches an existing cache, the directory is restored before your install step. If not, the action records a cache miss and saves the directory at the end of the job for future runs.
- name: Cache npm dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: npm-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
npm-${{ runner.os }}-
- name: Install dependencies
run: npm ci
The hashFiles('**/package-lock.json') expression produces a hash of your lock file. When the lock file changes (new dependency added), the hash changes, the old cache is missed, and a fresh install populates a new cache. restore-keys: provides fallback prefixes — if the exact key is not found, GitHub tries caches whose key starts with npm-ubuntu-latest-, giving a partial hit that is still faster than a cold install.
Popular language setups — actions/setup-node, actions/setup-java, actions/setup-python — have a built-in cache: input that wraps actions/cache automatically:
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm' # handles path + key automatically
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
