actions / cache

Cache dependencies and build outputs in GitHub Actions
MIT License
4.59k stars 1.23k forks source link

Possible to use cache with npx without a package.json? #768

Closed simonw closed 2 years ago

simonw commented 2 years ago

Some of my actions use npx to run commands, for example:

npx get-graphql-schema https://github-to-sqlite.dogsheep.net/graphql > schema.graphql

Is there a way to configure the Actions cache such that npx doesn't download packages when it runs every single time, without needing to add a package.json file to the repository?

simonw commented 2 years ago

I think I figured out a pattern to do this. The cache: npm option requires either a package.json or yarn.lock file... or you can give it a custom path.

I'm now doing this:

    - uses: actions/setup-node@v2
      with:
        node-version: '14'
        cache: 'npm'
        cache-dependency-path: '**/.github/workflows/*.yml'

Since the tools that might be run using npx are defined in my workflow YAML, I'm using the workflow YAML itself as the cache-dependency-path.

It looks like this works, so I'm happy to keep using this pattern.

simonw commented 2 years ago

Wrote this up here: https://til.simonwillison.net/github-actions/npm-cache-with-npx-no-package