-
Notifications
You must be signed in to change notification settings - Fork 0
96 lines (84 loc) · 2.89 KB
/
cache.yml
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
name: Clean cache
on:
pull_request:
types:
- closed
workflow_dispatch:
inputs:
PR_NUMBER:
description: ID number of the pull request assiocited with the cache
required: false
type: number
BRANCH_NAME:
description: Branch name assiocited with the cache
required: false
type: string
COMMIT_SHA:
description: Commit sha assiocited with the cache
required: false
type: string
permissions:
packages: write
jobs:
cleanup-cache:
name: Delete gituhb cache
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Clean cache for closed branch
run: |
gh extension install actions/gh-actions-cache
REPO=${{ github.repository }}
if [ -n "${{ inputs.BRANCH_NAME }}" ]; then
BRANCH="${{ inputs.BRANCH_NAME }}"
else
BRANCH="refs/pull/${{ github.event.pull_request.number || inputs.PR_NUMBER }}/merge"
fi
echo "Fetching list of cache key"
cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH -L 100 | cut -f 1 )
## Setting this to not fail the workflow while deleting cache keys.
set +e
echo "Deleting caches..."
for cacheKey in $cacheKeysForPR; do
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm
done
echo "Done"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
infos:
name: Generate infos
runs-on: ubuntu-latest
if: ${{ github.event.pull_request.number || github.event.number }}
outputs:
build-matrix: ${{ steps.infos.outputs.BUILD_MATRIX }}
steps:
- name: Checks-out repository
uses: actions/checkout@v4
- name: Get variables
id: infos
run: |
echo "BUILD_MATRIX=$(jq -c . < ./ci/matrix/docker.json)" >> $GITHUB_OUTPUT
cleanup-image:
name: Delete image from ghcr.io
runs-on: ubuntu-latest
needs:
- infos
strategy:
matrix:
images: ${{ fromJSON(needs.infos.outputs.build-matrix) }}
steps:
- name: Checks-out repository
uses: actions/checkout@v4
- name: Get repository owner and name
id: image-infos
run: |
echo "USER_NAME=$(echo '${{ github.repository }}' | cut -d "/" -f 1)" >> $GITHUB_OUTPUT
echo "REPO_NAME=$(echo '${{ github.repository }}' | cut -d "/" -f 2)" >> $GITHUB_OUTPUT
- name: Delete ${{ matrix.images.name }} image
run: |
curl -s https://raw.githubusercontent.com/this-is-tobi/tools/main/shell/delete-ghcr-image.sh | bash -s -- \
-o "${{ steps.image-infos.outputs.USER_NAME }}" \
-i "${{ steps.image-infos.outputs.REPO_NAME }}" \
-t "${{ inputs.COMMIT_SHA || format('pr-{0}', inputs.PR_NUMBER || inputs.COMMIT_SHA || github.event.pull_request.number || github.event.number) }}" \
-g "${{ secrets.GITHUB_TOKEN }}"