Skip to content

Commit d618a61

Browse files
authored
ci: reduce Actions cache usage and fix cleanup (#4123)
Reduce GitHub Actions cache pressure by keeping main as the authoritative Docker BuildKit cache producer and making cache cleanup reliable for pull requests and deleted branches. Cache cleanup: - Run closed pull request cleanup through pull_request_target so the workflow receives the actions: write permission required to remove caches created by fork-owned PRs. - Keep the privileged cleanup safe by avoiding checkout or execution of pull request code. - Replace the previous cache listing and extension loop with gh cache delete --all scoped to the exact pull request merge ref. - Stop suppressing cache deletion failures so permission and API errors are reported instead of producing misleading successful runs. - Add cleanup for caches associated with deleted branches, including temporary dependency-update branches. - Use --succeed-on-no-caches so already-clean refs remain valid cleanup runs. Docker cache behavior: - Make pull request Docker builds restore-only. - Allow PR builds to reuse the shared GitHub Actions BuildKit cache without exporting their own full mode=max cache. - Keep main as the only producer of the complete reusable BuildKit cache. - Make main cache-export backend or quota failures non-blocking so a successful Docker image build is not failed only because the cache could not be published. Measured impact: - Removed 178 legacy PR-scoped BuildKit caches. - Reclaimed 9.531 GiB of Actions cache storage. - Reduced repository cache usage from 10.088 GiB to 0.557 GiB, about 94.5% lower. - Preserved the default-branch BuildKit cache and existing mise, ccache, map, and other non-BuildKit caches. - Confirmed the PR Docker build completed without creating a cache under refs/pull/4123/merge. Validation: - Passed actionlint for clean-cache.yaml and reusable-build-docker.yml. - Passed git diff --check. - Passed GitHub Actions yamllint, fast checks, Lua checks, cppcheck, SonarCloud, Docker image build, and Docker quickstart smoke. - Completed the one-time legacy cache cleanup with no deletion failures. - Event-driven cleanup jobs were validated statically and will execute from the default branch after merge. This prevents closed PRs and deleted branches from accumulating stale Actions caches while avoiding a separate full Docker cache for every pull request.
1 parent 87c35d7 commit d618a61

2 files changed

Lines changed: 36 additions & 21 deletions

File tree

.github/workflows/clean-cache.yaml

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
---
2-
name: Cleanup caches by a branch
2+
name: Cleanup Actions caches
3+
34
on:
4-
pull_request:
5+
pull_request_target:
56
types:
67
- closed
8+
delete:
9+
10+
permissions:
11+
contents: read
712

813
jobs:
9-
cleanup:
14+
cleanup-pull-request:
15+
if: github.event_name == 'pull_request_target'
1016
runs-on: ubuntu-latest
17+
permissions:
18+
actions: write
19+
contents: read
1120
steps:
12-
- name: Cleanup
13-
run: |
14-
gh extension install actions/gh-actions-cache
15-
16-
echo "Fetching list of cache key"
17-
cacheKeysForPR=$(gh actions-cache list -R "$REPO" -B "$BRANCH" -L 100 | cut -f 1 )
21+
- name: Delete pull request caches
22+
env:
23+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
24+
CACHE_REF: refs/pull/${{ github.event.pull_request.number }}/merge
25+
run: >-
26+
gh cache delete --all
27+
--ref "$CACHE_REF"
28+
--repo "$GITHUB_REPOSITORY"
29+
--succeed-on-no-caches
1830
19-
## Setting this to not fail the workflow while deleting cache keys.
20-
set +e
21-
echo "Deleting caches..."
22-
for cacheKey in $cacheKeysForPR
23-
do
24-
gh actions-cache delete "$cacheKey" -R "$REPO" -B "$BRANCH" --confirm
25-
done
26-
echo "Done"
31+
cleanup-branch:
32+
if: github.event_name == 'delete' && github.event.ref_type == 'branch'
33+
runs-on: ubuntu-latest
34+
permissions:
35+
actions: write
36+
contents: read
37+
steps:
38+
- name: Delete branch caches
2739
env:
2840
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
29-
REPO: ${{ github.repository }}
30-
BRANCH: refs/pull/${{ github.event.pull_request.number }}/merge
41+
CACHE_REF: refs/heads/${{ github.event.ref }}
42+
run: >-
43+
gh cache delete --all
44+
--ref "$CACHE_REF"
45+
--repo "$GITHUB_REPOSITORY"
46+
--succeed-on-no-caches

.github/workflows/reusable-build-docker.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ jobs:
9292
ghcr.io/${{ steps.repo_lower.outputs.value }}:latest
9393
ghcr.io/${{ steps.repo_lower.outputs.value }}:${{ steps.gitversion.outputs.semVer }}
9494
cache-from: type=gha
95-
cache-to: type=gha,mode=max
95+
cache-to: type=gha,mode=max,ignore-error=true
9696

9797
- name: Build and export (PR)
9898
if: github.event_name != 'push' || github.ref != 'refs/heads/main'
@@ -111,7 +111,6 @@ jobs:
111111
tags: |
112112
ghcr.io/${{ steps.repo_lower.outputs.value }}:pr
113113
cache-from: type=gha
114-
cache-to: type=gha,mode=max
115114

116115
- name: Validate Docker image (PR)
117116
if: github.event_name != 'push' || github.ref != 'refs/heads/main'

0 commit comments

Comments
 (0)