name: Cache on main
description: caches node_modules/.cache, but only saves from main
inputs:
  path:
    description: 'A list of files, directories, and wildcard patterns to cache and store'
    required: true
  key:
    description: 'An explicit key for restoring and saving the cache'
    required: true
  restore-keys:
    description: 'An ordered list of keys to use for restoring stale cache if no cache hit occured for key. Note `cache-hit` returns false in this case.'
    required: false

# Many build steps have their own caches to improve subsequent build times.
# Build tools are configured to cache to node_modules/.cache, so they are cached independently of node_modules.
# Caches are saved every run *on main* (by keying on github.run_id), and the most recent available cache is loaded.
# Caches are not saved on feature branches because they have limited utility, and extend the runtime of the workflow.
# See https://jongleberry.medium.com/speed-up-your-ci-and-dx-with-node-modules-cache-ac8df82b7bb0.

runs:
  using: composite
  steps:
    - uses: actions/cache/restore@v3
      with:
        path: ${{ inputs.path }}
        key: ${{ inputs.key }}
        restore-keys: ${{ inputs.restore-keys }}
    - if: github.ref_name == 'main'
      uses: actions/cache/save@v3
      with:
        path: ${{ inputs.path }}
        key: ${{ inputs.key }}