Skip to content

Commit

Permalink
Simplify cache cleanup actions (#361)
Browse files Browse the repository at this point in the history
* Simplify cache cleanup actions

For the CleanUpCache workflow run triggered by PR workflows, there is a
simple way of getting the PR number. The full event webhook payload is
available as a json file and it contains the number of the triggering PR. So
we don't need to upload the PR number as an artifact.

For the post-PR cleanup, we could also clean up the cache directly without
uploading the PR number and triggering a workflow run for cleaning.

* No need to upload PR number
  • Loading branch information
WeiqunZhang committed Aug 29, 2024
1 parent 4f197ca commit e334889
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 68 deletions.
15 changes: 0 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,3 @@ 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
40 changes: 0 additions & 40 deletions .github/workflows/cleanup-cache-postpr.yml

This file was deleted.

7 changes: 4 additions & 3 deletions .github/workflows/cleanup-cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v4
- name: Clean up ccahe
- name: Clean up ccache
run: |
gh extension install actions/gh-actions-cache
Expand All @@ -30,8 +30,8 @@ jobs:
WORKFLOW_NAME="${{ github.event.workflow_run.name }}"
if [[ $EVENT == "pull_request" ]]; then
gh run download ${{ github.event.workflow_run.id }} -n pr_number
pr_number=`cat pr_number.txt`
pr_number=$(jq --raw-output .workflow_run.pull_requests[0].number < ${GITHUB_EVENT_PATH})
echo "Clean up cache for PR ${pr_number}."
BRANCH=refs/pull/${pr_number}/merge
else
BRANCH=refs/heads/${{ github.event.workflow_run.head_branch }}
Expand All @@ -54,6 +54,7 @@ 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
Expand Down
39 changes: 29 additions & 10 deletions .github/workflows/post-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,35 @@ on:
- closed

jobs:
cleanup:
cleanup_cache:
name: Clean Up Ccache Cache Post PR
runs-on: ubuntu-latest
permissions:
actions: write
contents: read
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Save PR number
env:
PR_NUMBER: ${{ github.event.number }}
- uses: actions/checkout@v4
- name: Clean up ccache
run: |
echo $PR_NUMBER > pr_number.txt
- uses: actions/upload-artifact@v4
with:
name: pr_number
path: pr_number.txt
retention-days: 1
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

0 comments on commit e334889

Please sign in to comment.