Repository navigation
78 lines (67 loc) · 2.42 KB
/
Copy pathcleanup-cache.yml
File metadata and controls
78 lines (67 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
name: CleanUpCache
on:
workflow_run:
workflows:
- ERF CI
- Linux GCC
- Linux GCC NetCDF RRTMGP
- ERF CI (cuda)
- ERF CI (hip)
- ERF CI (sycl)
- MacOS
- Windows
types:
- completed
schedule:
- cron: '0 7 * * *'
workflow_dispatch:
# Keyed on branch only, not branch+sha, so back-to-back pushes coalesce instead of racing.
concurrency:
group: cleanup-${{ github.event.workflow_run.head_branch || github.ref }}
cancel-in-progress: true
jobs:
CleanUpCcacheCache:
name: Clean Up Ccache Cache
runs-on: ubuntu-22.04
timeout-minutes: 15
permissions:
actions: write
contents: read
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v7
- name: Clean up ccache
run: |
gh extension install actions/gh-actions-cache
REPO=${{ github.repository }}
# push or pull_request or schedule or ...
EVENT="${{ github.event.workflow_run.event }}"
HEAD_BRANCH="${{ github.event.workflow_run.head_branch }}"
# For debugging, cat ${GITHUB_EVENT_PATH} to see the payload.
if [[ $EVENT == "pull_request" ]]; then
pr_head_sha=${{ github.event.workflow_run.head_sha }}
pr_number=$(gh pr list --search $pr_head_sha --json number --jq '.[0].number')
echo "Clean up cache for PR ${pr_number}"
BRANCH=refs/pull/${pr_number}/merge
elif [[ -n $HEAD_BRANCH ]]; then
BRANCH=refs/heads/${HEAD_BRANCH}
else
# schedule / workflow_dispatch: no workflow_run context to inherit
BRANCH=refs/heads/development
fi
# Setting this to not fail the workflow while deleting cache keys.
set +e
keyprefix="ccache-"
cached_jobs=$(gh actions-cache list -L 100 -R $REPO -B $BRANCH --key "$keyprefix" | awk -F '-git-' '{print $1}' | sort | uniq)
IFS=$'\n'
for j in $cached_jobs
do
# Last used isn't always last created (ie we don't want latest cache overwritten by an old PR's cache)
old_keys=$(gh actions-cache list -L 100 -R $REPO -B $BRANCH --key "${j}-git-" --sort created-at --order desc | cut -f 1 | tail -n +2)
for k in $old_keys
do
gh actions-cache delete "$k" -R $REPO -B $BRANCH --confirm
done
done
unset IFS