|
| 1 | +name: Save rolling ccache |
| 2 | +description: Delete the previous exact ccache key and save the refreshed cache under the same key. |
| 3 | +inputs: |
| 4 | + key: |
| 5 | + description: Exact GitHub cache key to replace. |
| 6 | + required: true |
| 7 | + ref: |
| 8 | + description: Git ref that owns the cache, for example refs/heads/main. |
| 9 | + required: true |
| 10 | + path: |
| 11 | + description: ccache directory to save. |
| 12 | + required: false |
| 13 | + default: .ccache |
| 14 | +runs: |
| 15 | + using: composite |
| 16 | + steps: |
| 17 | + - name: Delete previous cache |
| 18 | + uses: actions/github-script@v7 |
| 19 | + env: |
| 20 | + CACHE_KEY: ${{ inputs.key }} |
| 21 | + CACHE_REF: ${{ inputs.ref }} |
| 22 | + with: |
| 23 | + script: | |
| 24 | + const key = process.env.CACHE_KEY; |
| 25 | + const ref = process.env.CACHE_REF; |
| 26 | + const { owner, repo } = context.repo; |
| 27 | + let deleted = 0; |
| 28 | +
|
| 29 | + for (let page = 1; ; page += 1) { |
| 30 | + const { data } = await github.rest.actions.getActionsCacheList({ |
| 31 | + owner, |
| 32 | + repo, |
| 33 | + ref, |
| 34 | + key, |
| 35 | + per_page: 100, |
| 36 | + page, |
| 37 | + }); |
| 38 | + const caches = data.actions_caches ?? []; |
| 39 | + for (const cache of caches) { |
| 40 | + if (cache.key !== key) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + await github.rest.actions.deleteActionsCacheById({ |
| 44 | + owner, |
| 45 | + repo, |
| 46 | + cache_id: cache.id, |
| 47 | + }); |
| 48 | + deleted += 1; |
| 49 | + } |
| 50 | + if (caches.length < 100) { |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | +
|
| 55 | + core.info(deleted === 0 |
| 56 | + ? `No previous cache found for ${key} on ${ref}.` |
| 57 | + : `Deleted ${deleted} previous cache entr${deleted === 1 ? "y" : "ies"} for ${key} on ${ref}.`); |
| 58 | +
|
| 59 | + - name: Save refreshed cache |
| 60 | + uses: actions/cache/save@v4 |
| 61 | + with: |
| 62 | + path: ${{ inputs.path }} |
| 63 | + key: ${{ inputs.key }} |
0 commit comments