From 1d03154a18499fe1511b67ca2964c6cce2177eb0 Mon Sep 17 00:00:00 2001 From: Erik Burton Date: Thu, 14 Nov 2024 14:11:32 -0800 Subject: [PATCH] feat: delete merge queue caches on pr merge (#15234) * feat: delete merge queue caches on pr merge * fix: use env, rename variables --- .github/workflows/delete-caches.yml | 51 ++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/.github/workflows/delete-caches.yml b/.github/workflows/delete-caches.yml index 870b6b6d086..aaba0dda623 100644 --- a/.github/workflows/delete-caches.yml +++ b/.github/workflows/delete-caches.yml @@ -16,27 +16,48 @@ jobs: # See also: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#delete-a-github-actions-cache-for-a-repository-using-a-cache-id actions: write contents: read + env: + REPO: ${{ github.repository }} + PR_NUMBER: ${{ github.event.pull_request.number }} steps: - name: Check out code uses: actions/checkout@v4.1.2 - - name: Cleanup Branch Caches - run: | - gh extension install actions/gh-actions-cache - - REPO=${{ github.repository }} - BRANCH=refs/pull/${{ github.event.pull_request.number }}/merge + - name: Setup gh-actions-cache extension + run: gh extension install actions/gh-actions-cache - echo "Fetching list of cache key" - cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH | cut -f 1 ) + - name: Retrieve Trunk SHA + id: get-sha + run: | + SHA=$(gh pr view -R $REPO $PR_NUMBER --json mergeCommit --jq .mergeCommit.oid) + echo "sha=$SHA" >> $GITHUB_OUTPUT - ## Setting this to not fail the workflow while deleting cache keys. + - name: Cleanup Caches + env: + TRUNK_SHA: ${{ steps.get-sha.outputs.sha }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | set +e - echo "Deleting caches..." - for cacheKey in $cacheKeysForPR - do - gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm + + PR_BRANCH=refs/pull/$PR_NUMBER/merge + echo "Fetching list of cache keys for the PR branch ($PR_BRANCH)" + PR_CACHE_KEYS=$(gh actions-cache list -R $REPO -B $PR_BRANCH | cut -f 1) + + echo "Deleting caches for PR branch ($PR_BRANCH)..." + for CACHE_KEY in $PR_CACHE_KEYS; do + gh actions-cache delete $CACHE_KEY -R $REPO -B $PR_BRANCH --confirm done + + if [[ -n "$TRUNK_SHA" ]]; then + echo "Found corresponding merge commit $TRUNK_SHA" + QUEUE_BRANCH="gh-readonly-queue/develop/pr-${PR_NUMBER}-${TRUNK_SHA}" + echo "Fetching list of cache keys for the merge queue branch ($QUEUE_BRANCH)" + QUEUE_CACHE_KEYS=$(gh actions-cache list -R $REPO -B $QUEUE_BRANCH | cut -f 1) + + echo "Deleting caches for merge queue branch ($QUEUE_BRANCH)..." + for CACHE_KEY in $QUEUE_CACHE_KEYS; do + gh actions-cache delete $CACHE_KEY -R $REPO -B $QUEUE_BRANCH --confirm + done + fi + echo "Done" - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file