Skip to content

A Github Action to save and restore files across job runs directly in the runner's file system

License

Notifications You must be signed in to change notification settings

MasterworksIO/action-local-cache

Repository files navigation

Local Cache Github Action

This github action allows you to save and restore files across job runs directly in the runner's file system.

It is intended to be used with runners with persisted storage. Don't use this action if you're using Github-hosted runners or self-hosted runners in ephemeral instances (like docker containers that are launched on demand when a workflow starts and are terminated when the job finishes).

Usage

# .github/workflows/my-workflow.yml
jobs:
  my_job:

    steps:
      - uses: actions/checkout@v2

      - name: Local cache for API dependencies
        id: api-cache
        uses: MasterworksIO/action-local-cache@2
        with:
          path: './api/node_modules/'
          key: 'api-dependencies-v1'

      - name: Install dependencies
        run: cd api/ && npm install
        if: steps.api-cache.outputs.cache-hit != 'true'

      - name: Do your stuff
        run: npm run build

The key param is optional and you can use it to invalidate cache.

Caching strategy

Define how you want your cache to be used. move is the default strategy:

Usage Description Observations
strategy: move Moves cache on job start, moves back on job end Uses the filesystem move syscalls, cache saving and restoration is instant. Does not work across disks/remote shares.
strategy: copy Recursiverly copies cache on job start, refreshes cache on job end Works across disks/remote shares.
strategy: copy-immutable Recursiverly copies cache on job start, does not refresh cache on job end Works across disks/remote shares. Faster to use if the contents will never change (e.g. installing node dependencies)

Refer to push.yaml for examples of how to use the above strategies.

How it works

The first time action-local-cache is used in a runner it will take the given path and create a folder structure inside the $RUNNER_TOOL_CACHE dir (usually _work/_tool inside the runner's workspace), prefixing the user/org name, repo name, and key.

For example, when running the usage example above inside a workflow for the MasterworksIO/product repo, this empty folder will be created:

~/runner/_work/_tool/MasterworksIO/product/api-dependencies-v1/

Since there's no api/node_modules/ dir inside, the restore step is skipped and the cache-hit output variable will be set to false; because of that, the next step which install the dependencies will run.

After the job is successfully completed, action-local-cache will take the api/node_modules dir generated by the install dependencies step and move/copy it to the previously created dir structure before the runner workspace is wiped.

On a next run under the same runner instance, the cache will be moved/copied back to the working directory and the cache-output variable will be set to true.

After all steps complete the folder will be moved/copied back to the cache dir and so on.

LICENSE

MIT, see LICENSE