Leafwing-Studios / cargo-cache

Cache your Cargo build files
Apache License 2.0
8 stars 2 forks source link

Add `save-if` option for conditional cache saving #21

Closed TimJentzsch closed 1 month ago

TimJentzsch commented 1 month ago

Objective

Closes #20.

Sometimes you don't always want to save the new cache, but only restore from existing caches. This might be necessary if the repository is very active and always hits the cache size limit.

For example, you might only want to save the cache on the main branch and reuse it across all branches.

See also https://github.com/bevyengine/bevy/pull/13040.

Solution

Add a save-if option, allowing you to specify when the cache should be saved. Defaults to true.

Unfortunately, the cache action does not provide a save-if option, so we need to conditionally use cache/restore instead.

Internally, we add a new job step to determine the cache paths, key and fallbacks such that we can reuse them consistently. This turned out to be more difficult to get working than to just copy and paste everything, we will just have to be careful to keep the values in sync.

TimJentzsch commented 1 month ago

Tested and confirmed to be working (after a lot of bugfixing). A setup like this:

uses: Leafwing-Studios/cargo-cache@b46a3eb6f97357ee4bada528857bfff77d08b964
        with:
          save-if: ${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}

Makes it possible to save the cache only on the default branch.