From 5472ee9c9742bd334862dd2c08b217bdeb1ee9cd Mon Sep 17 00:00:00 2001 From: Weiqun Zhang Date: Thu, 29 Aug 2024 14:52:26 -0700 Subject: [PATCH] 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. --- .github/workflows/ci.yml | 15 -------- .github/workflows/cleanup-cache-postpr.yml | 40 ---------------------- .github/workflows/cleanup-cache.yml | 5 +-- .github/workflows/cuda-ci.yml | 15 -------- .github/workflows/gcc.yml | 15 -------- .github/workflows/hip.yml | 15 -------- .github/workflows/macos.yml | 15 -------- .github/workflows/post-pr.yml | 39 +++++++++++++++------ .github/workflows/sycl.yml | 15 -------- 9 files changed, 32 insertions(+), 142 deletions(-) delete mode 100644 .github/workflows/cleanup-cache-postpr.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9859100..dc0e73a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -107,18 +107,3 @@ jobs: # name: build-and-test # path: | # ${{runner.workspace}}/REMORA/regressioncov.html - - save_pr_number: - if: github.event_name == 'pull_request' - 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 deleted file mode 100644 index 9a2ffb0..0000000 --- a/.github/workflows/cleanup-cache-postpr.yml +++ /dev/null @@ -1,40 +0,0 @@ -name: CleanUpCachePostPR - -on: - workflow_run: - workflows: [PostPR] - types: - - completed - -jobs: - CleanUpCcacheCachePostPR: - name: Clean Up Ccache 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 ccache - 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 3cf2c54..5f66a5b 100644 --- a/.github/workflows/cleanup-cache.yml +++ b/.github/workflows/cleanup-cache.yml @@ -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 }} @@ -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 diff --git a/.github/workflows/cuda-ci.yml b/.github/workflows/cuda-ci.yml index 9fc77e1..30dba09 100644 --- a/.github/workflows/cuda-ci.yml +++ b/.github/workflows/cuda-ci.yml @@ -60,18 +60,3 @@ jobs: ccache -s du -hs ~/.cache/ccache - - save_pr_number: - if: github.event_name == 'pull_request' - 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/gcc.yml b/.github/workflows/gcc.yml index 89a7d17..1ba05f8 100644 --- a/.github/workflows/gcc.yml +++ b/.github/workflows/gcc.yml @@ -60,18 +60,3 @@ jobs: run: | ctest -L regression -VV working-directory: ${{runner.workspace}}/REMORA/build - - save_pr_number: - if: github.event_name == 'pull_request' - 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/hip.yml b/.github/workflows/hip.yml index a9a06fd..cb651f4 100644 --- a/.github/workflows/hip.yml +++ b/.github/workflows/hip.yml @@ -132,18 +132,3 @@ jobs: name: build-and-test path: | ${{runner.workspace}}/REMORA/regressioncov.xml - - save_pr_number: - if: github.event_name == 'pull_request' - 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/macos.yml b/.github/workflows/macos.yml index fbd65c5..b9dd73e 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -59,18 +59,3 @@ jobs: run: | ctest -L regression -VV working-directory: ${{runner.workspace}}/REMORA/build - - save_pr_number: - if: github.event_name == 'pull_request' - 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/post-pr.yml b/.github/workflows/post-pr.yml index 2768ef3..4155b6c 100644 --- a/.github/workflows/post-pr.yml +++ b/.github/workflows/post-pr.yml @@ -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 diff --git a/.github/workflows/sycl.yml b/.github/workflows/sycl.yml index 9046421..bfd7444 100644 --- a/.github/workflows/sycl.yml +++ b/.github/workflows/sycl.yml @@ -62,18 +62,3 @@ jobs: ccache -s du -hs ~/.cache/ccache - - save_pr_number: - if: github.event_name == 'pull_request' - 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