From 7906e25b479e79d75671c1d259300695411f0624 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Thu, 29 Aug 2024 16:56:14 -0700 Subject: [PATCH] Revert "Simplify cache cleanup actions (#361)" (#362) This reverts commit e334889c1e99ae2c3878d74256787fb5ef163066. --- .github/workflows/ci.yml | 15 ++++++++ .github/workflows/cleanup-cache-postpr.yml | 40 ++++++++++++++++++++++ .github/workflows/cleanup-cache.yml | 7 ++-- .github/workflows/post-pr.yml | 39 ++++++--------------- 4 files changed, 68 insertions(+), 33 deletions(-) create mode 100644 .github/workflows/cleanup-cache-postpr.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a1325562..e3db7fba 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -47,3 +47,18 @@ jobs: contents: read security-events: write uses: ./.github/workflows/codeql.yml + + save_pr_number: + if: github.event_name != 'push' + runs-on: ubuntu-latest + steps: + - name: Save PR number + env: + PR_NUMBER: ${{ github.event.number }} + run: | + echo $PR_NUMBER > pr_number.txt + - uses: actions/upload-artifact@v4 + with: + name: pr_number + path: pr_number.txt + retention-days: 1 diff --git a/.github/workflows/cleanup-cache-postpr.yml b/.github/workflows/cleanup-cache-postpr.yml new file mode 100644 index 00000000..5dc32a68 --- /dev/null +++ b/.github/workflows/cleanup-cache-postpr.yml @@ -0,0 +1,40 @@ +name: CleanUpCachePostPR + +on: + workflow_run: + workflows: [PostPR] + types: + - completed + +jobs: + CleanUpCcacheCachePostPR: + name: Clean Up Ccahe Cache Post PR + runs-on: ubuntu-latest + permissions: + actions: write + contents: read + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v4 + - name: Clean up ccahe + run: | + gh extension install actions/gh-actions-cache + + REPO=${{ github.repository }} + + gh run download ${{ github.event.workflow_run.id }} -n pr_number + pr_number=`cat pr_number.txt` + BRANCH=refs/pull/${pr_number}/merge + + # Setting this to not fail the workflow while deleting cache keys. + set +e + + keys=$(gh actions-cache list -L 100 -R $REPO -B $BRANCH | cut -f 1) + # $keys might contain spaces. Thus we set IFS to \n. + IFS=$'\n' + for k in $keys + do + gh actions-cache delete "$k" -R $REPO -B $BRANCH --confirm + done + unset IFS diff --git a/.github/workflows/cleanup-cache.yml b/.github/workflows/cleanup-cache.yml index c0a4af9e..d1c49abc 100644 --- a/.github/workflows/cleanup-cache.yml +++ b/.github/workflows/cleanup-cache.yml @@ -17,7 +17,7 @@ jobs: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} steps: - uses: actions/checkout@v4 - - name: Clean up ccache + - name: Clean up ccahe run: | gh extension install actions/gh-actions-cache @@ -30,8 +30,8 @@ jobs: WORKFLOW_NAME="${{ github.event.workflow_run.name }}" if [[ $EVENT == "pull_request" ]]; then - pr_number=$(jq --raw-output .workflow_run.pull_requests[0].number < ${GITHUB_EVENT_PATH}) - echo "Clean up cache for PR ${pr_number}." + gh run download ${{ github.event.workflow_run.id }} -n pr_number + pr_number=`cat pr_number.txt` BRANCH=refs/pull/${pr_number}/merge else BRANCH=refs/heads/${{ github.event.workflow_run.head_branch }} @@ -54,7 +54,6 @@ jobs: IFS=$'\n' for j in $cached_jobs do - # Delete all entries except the last used one old_keys=$(gh actions-cache list -L 100 -R $REPO -B $BRANCH --key "${j}-git-" --sort last-used | cut -f 1 | tail -n +2) for k in $old_keys do diff --git a/.github/workflows/post-pr.yml b/.github/workflows/post-pr.yml index 4155b6c5..2768ef37 100644 --- a/.github/workflows/post-pr.yml +++ b/.github/workflows/post-pr.yml @@ -5,35 +5,16 @@ on: - closed jobs: - cleanup_cache: - name: Clean Up Ccache Cache Post PR + cleanup: runs-on: ubuntu-latest - permissions: - actions: write - contents: read - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} steps: - - uses: actions/checkout@v4 - - name: Clean up ccache + - name: Save PR number + env: + PR_NUMBER: ${{ github.event.number }} run: | - gh extension install actions/gh-actions-cache - - REPO=${{ github.repository }} - - pr_number=${{ github.event.number }} - echo "Clean up cache for PR ${pr_number}." - BRANCH=refs/pull/${pr_number}/merge - - # Setting this to not fail the workflow while deleting cache keys. - set +e - - # Delete all entries associated with this PR. - keys=$(gh actions-cache list -L 100 -R $REPO -B $BRANCH | cut -f 1) - # $keys might contain spaces. Thus we set IFS to \n. - IFS=$'\n' - for k in $keys - do - gh actions-cache delete "$k" -R $REPO -B $BRANCH --confirm - done - unset IFS + echo $PR_NUMBER > pr_number.txt + - uses: actions/upload-artifact@v4 + with: + name: pr_number + path: pr_number.txt + retention-days: 1