Bug: cannot update cache in Cargo project
I have made a minimal example of bug.
I have setup a Cargo workspace and want to cache the ~/.cargo and target directories. Calling save explicitly at the end of the script does not overwrite the existing cache after the cache is stored on the first time.
Deleting the previous version of the cache fixes it
Run actions/cache/save@v4
/usr/bin/tar --posix -cf cache.tzst --exclude cache.tzst -P -C /home/runner/work/cargo-ci/cargo-ci --files-from manifest.txt --use-compress-program zstdmt
Failed to save: Unable to reserve cache with key Linux-restore-build, another job may be creating this cache. More details: Cache already exists. Scope: refs/heads/main, Key: Linux-restore-build, Version: cd36736238095b30d245e37cee3eada29643d39d68cc49be99ecf89155bdc73a
Warning: Cache save failed.
The core of the message seems to be another job may be creating this cache. More details: Cache already exists.
The YAML workflow file I am using is
name: Rust test workflow
on:
push:
branches:
- 'main'
jobs:
test-restore-build:
name: Test restore compile and store
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Update APT package index
run: sudo apt-get update
- name: Install and download required APT packages
run: sudo apt-get install -y rustup
- name: Restore Cargo cache
uses: actions/cache/restore@v4
with:
path: |
~/.cargo
target
key: ${{ runner.os }}-restore-build
- name: Write to target
run: cargo build
- name: Save Cargo cache
uses: actions/cache/save@v4
with:
path: |
~/.cargo
target
key: ${{ runner.os }}-restore-build
I'm wondering if this is actually a bug or intended behavior. I would also like the possibility to overwrite an existing cache with actions/cache/save.
I'm wondering if this is actually a bug or intended behavior. I would also like the possibility to overwrite an existing cache with
actions/cache/save.
It is not well documented but save will fail if any existing cache with the same key on the current branch exists. You have to delete it first manually with the gh cli tool. I created a nice script for listing caches and deleting them based on PR numbers. Let me know if you need it.
I see, that's what I thought. I'll try something with gh cache delete in the workflow when I detect that I want to overwrite the cache.
I see, that's what I thought. I'll try something with
gh cache deletein the workflow when I detect that I want to overwrite the cache.
It's best to extract the ID of the cache you want to delete and pass that to gh cache delete.
Yes indeed. I got a solution working for me. I'm simply doing:
ID="$(gh cache list --ref=${{ github.ref }} --key=cargo-home --json=id --jq='.[].id')"
[ -z "$ID" ] || gh cache delete "$ID"
Duplicate of https://github.com/actions/cache/issues/342