Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub Action Cache with npm install -g (without package.json)

Can Github Action cache be used to speedup globally installed node tools?

I am using semantic-release on a ruby repository and I don't want to pollute that repository with package.json

My configuration for semantic-release resides in .releaserc

I can run this GitAction to update my SemVer.

name: SemVer
on:
  push:
    branches: [ main ]
jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '16'

      - name: Install Global Dependencies
        run: npm -g install semantic-release @semantic-release/changelog @semantic-release/commit-analyzer @semantic-release/exec @semantic-release/git @semantic-release/release-notes-generator

      - name: Run SemVer
        run: semantic-release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

It takes 22 seconds to install dependencies

enter image description here

I have tried integrating cache configuration that I found into my workflow, but this is is not working and I assume it is because there is no package-lock.json to build a hash against.

key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}

name: SemVer
on:
  push:
    branches: [ main ]
jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '16'

      - name: Cache node modules
        uses: actions/cache@v2
        id: cache-node-modules
        env:
          cache-name: cache-node-modules
        with:
          path: ~/.npm
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
            ${{ runner.os }}-

      - name: Install Global Dependencies
        if: steps.cache.outputs.cache-hit != 'true'
        run: npm -g install semantic-release @semantic-release/changelog @semantic-release/commit-analyzer @semantic-release/exec @semantic-release/git @semantic-release/release-notes-generator

      - name: Run SemVer
        run: semantic-release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

enter image description here

like image 388
David Cruwys Avatar asked May 27 '26 22:05

David Cruwys


1 Answers

You don't need to hash a package-lock.json if you don't have one, you can hash any other file or either none, eg:

with:
  path: ~/.npm
  key: ${{ runner.os }}-build-${{ env.cache-name }}

documentation is available here https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows

like image 87
DevTheJo Avatar answered May 30 '26 11:05

DevTheJo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!