Use cache across multiple jobs in one workflow run, invalidate on subsequent runs #650
Unanswered
vincerubinetti
asked this question in
Show and tell
Replies: 1 comment
-
You could probably use actions/upload-artifact in the first job and actions/download-artifact in the rest. If you don't want to keep the artifact around long, you could also set |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Figured I would share this because I haven't seen an example on it.
I have a GitHub Actions workflow to run tests on my app. I split the tests up into separate jobs so I could get nice separate GitHub checks on pull requests to quickly see which tests passed ✔️ or failed ❌. But, all the tests work on the same version/commit of the app, and the same installation of packages (using
yarn install
).My app isn't huge; installing the dependencies only takes a few minutes. So it's not necessary for me to cache package installation between multiple workflow runs, but it is nice to not have to re-run
yarn install
for every individual job/test. As such, I'm using@actions/cache
.Here's how I've set up my workflow. I use
env.GITHUB_RUN_NUMBER
, "a unique number for each run of a particular workflow in a repository", as the cache key. This way, each time the workflow is run (aside from re-runs), a new cache key is used, and we're starting from scratch in the firstinstall-cache
job.Once the GitHub team adds a way to invalidate/clear cache, I'll use it to simply clear all of the cache in the
install-cache
job, then use a simple static string likecache-key
for the cache key. Also, had to prepend${{ runner.os }}-
to the key, because it seems like maybe the action doesn't like when the key is a number.I'm aware that people say it's "fragile" to cache
node_modules
directly. However, in this particular case, I'm caching over only a single workflow run, on a single version/commit of the app, on a single environment, over a short amount of time, over a few jobs that shouldn't have side effects in that folder, so I hope it's not a problem. It's also orders of magnitude faster. See #620Beta Was this translation helpful? Give feedback.
All reactions