Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Clear old CodeQL caches (#78108)

Fraserce14a4a5118f
diff --git a/.github/workflows/cache-cleanup-codeql.yml b/.github/workflows/cache-cleanup-codeql.yml
new file mode 100644
index 00000000000..b4971d5ec57
--- /dev/null
+++ b/.github/workflows/cache-cleanup-codeql.yml
@@ -0,0 +1,83 @@
+# CodeQL's overlay analysis saves a ~400MB "base database" cache on every master run, keyed by commit SHA.
+#
+# Only the newest entry is ever restored: CodeQL looks its cache up by key *prefix* and takes the most recent match,
+# so everything behind it is dead weight. This job keeps the newest few and deletes the rest.
+name: CodeQL Cache Cleanup
+
+on:
+ schedule:
+ - cron: "0 4 * * *" # daily at 04:00 UTC
+ workflow_dispatch:
+ inputs:
+ keep:
+ description: "How many of the newest CodeQL caches to keep"
+ required: false
+ default: "2"
+ dry-run:
+ description: "List what would be deleted without deleting it"
+ type: boolean
+ required: false
+ default: false
+
+concurrency:
+ group: cache-cleanup-codeql
+ cancel-in-progress: false
+
+permissions:
+ actions: write # required to delete cache entries
+ contents: read
+
+jobs:
+ cleanup:
+ # don't run the schedule on forks
+ if: ${{ github.event_name != 'schedule' || github.repository == 'metabase/metabase' }}
+ runs-on: ${{ vars.DEFAULT_RUNNER_KEY }}
+ timeout-minutes: 10
+ env:
+ GH_TOKEN: ${{ github.token }}
+ KEEP: ${{ github.event.inputs.keep || '2' }}
+ DRY_RUN: ${{ github.event.inputs.dry-run || 'false' }}
+ PREFIX: codeql-overlay-base-database-
+ steps:
+ - name: Delete superseded CodeQL caches
+ shell: bash
+ run: |
+ set -euo pipefail
+
+ # created_at is ISO-8601, so a reverse lexicographic sort is newest-first.
+ gh api --paginate "/repos/${GITHUB_REPOSITORY}/actions/caches?per_page=100" \
+ --jq ".actions_caches[] | select(.key | startswith(\"${PREFIX}\")) | \"\(.created_at)\t\(.id)\t\(.size_in_bytes)\t\(.key)\"" \
+ | sort -r > all.tsv || true
+
+ found=$(wc -l < all.tsv | tr -d ' ')
+ echo "Found $found cache(s) matching '${PREFIX}'; keeping the newest $KEEP."
+ if [ "$found" -eq 0 ]; then
+ echo "Nothing to do." >> "$GITHUB_STEP_SUMMARY"
+ exit 0
+ fi
+
+ tail -n +$((KEEP + 1)) all.tsv > doomed.tsv || true
+ doomed=$(wc -l < doomed.tsv | tr -d ' ')
+
+ freed=0
+ while IFS=$'\t' read -r created id size key; do
+ [ -z "${id:-}" ] && continue
+ echo " deleting $key ($((size / 1000000))MB, created $created)"
+ if [ "$DRY_RUN" = "true" ]; then
+ echo " (dry run — not deleted)"
+ else
+ # A cache can vanish between listing and deleting (concurrent eviction); don't fail the job over it.
+ gh api --method DELETE "/repos/${GITHUB_REPOSITORY}/actions/caches/${id}" >/dev/null \
+ || echo " (already gone, skipping)"
+ fi
+ freed=$((freed + size))
+ done < doomed.tsv
+
+ {
+ echo "### CodeQL cache cleanup"
+ echo ""
+ echo "- matched: **$found**"
+ echo "- kept (newest): **$KEEP**"
+ echo "- deleted: **$doomed**"
+ echo "- reclaimed: **$((freed / 1000000)) MB**${DRY_RUN:+ (dry run: $DRY_RUN)}"
+ } >> "$GITHUB_STEP_SUMMARY"