-
-
Notifications
You must be signed in to change notification settings - Fork 4
205 lines (180 loc) · 7.29 KB
/
container_image_retention.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
---
name: 'Container image retention'
on: # yamllint disable-line rule:truthy
push:
branches:
- 'main'
paths:
- '.github/workflows/container_image_retention.yml'
schedule:
- cron: '30 3 * * *'
workflow_dispatch: {}
permissions:
contents: 'read'
concurrency:
group: 'ci-${{ github.workflow }}-${{ github.ref }}'
cancel-in-progress: false
env:
# full image name including the complete path of the registry
IMAGE_NAME: 'ghcr.io/${{ github.repository_owner }}/ansible-role-file_deployment-jekyll'
# number of versions to keep
KEEP_VERSIONS: 3
# regular expression for tags to filter/ignore
IGNORE_TAGS: '^(latest|buildcache)$|^sha([[:digit:]]+?)?-'
# additional tags to resolve and keep the digests from, comma-seperated
ADDITIONAL_KEEP_TAGS: 'latest,buildcache'
jobs:
check-user-permissions:
runs-on: 'ubuntu-24.04'
permissions:
contents: 'read'
outputs:
require-result: '${{ steps.check-access.outputs.require-result }}'
steps:
- name: 'Harden Runner'
uses: 'step-security/harden-runner@0080882f6c36860b6ba35c610c98ce87d4e2f26f' # v2.10.2
with:
egress-policy: 'block'
allowed-endpoints: >
api.github.com:443
github.com:443
- name: 'Get User Permissions'
id: 'check-access'
uses: 'actions-cool/check-user-permission@956b2e73cdfe3bcb819bb7225e490cb3b18fd76e' # v2.2.1
with:
require: 'write'
username: '${{ github.triggering_actor }}'
env:
GITHUB_TOKEN: '${{ secrets.token }}'
- name: 'Check User Permission'
if: "steps.check-access.outputs.require-result == 'false'"
shell: 'bash'
run: |
# fail if:
# - a variable is unbound
# - any command fails
# - a command in a pipe fails
# - a command in a sub-shell fails
set -Eeuo pipefail
# enable debug if runner runs in debug
[[ "${{ runner.debug }}" -ne 1 ]] || {
echo "INFO: Enabling bash trace";
set -x;
};
echo "${{ github.triggering_actor }} does not have permissions on this repo."
echo "Current permission level is ${{ steps.check-access.outputs.user-permission }}"
echo "Job originally triggered by ${{ github.actor }}"
prepare-vars:
name: 'Prepare variables for building the container image'
if: "needs.check-user-permissions.outputs.require-result == 'true'"
needs: 'check-user-permissions'
runs-on: 'ubuntu-24.04'
container:
# yamllint disable-line rule:line-length
image: 'registry.access.redhat.com/ubi9/podman:9.5-1731589681@sha256:920004cf33ec15e1005dc912af613409273323f6ab56a23a4227d038ceb4821d'
options: '--privileged'
outputs:
action-debug: '${{ steps.prepare-vars.outputs.action-debug }}'
short-image-name: '${{ steps.prepare-vars.outputs.short-image-name }}'
skip-digests: '${{ steps.prepare-vars.outputs.skip-digests }}'
skip-tags: '${{ steps.prepare-vars.outputs.skip-tags }}'
steps:
- name: 'Prepare variables'
id: 'prepare-vars'
shell: 'bash'
run: |
# fail if:
# - a variable is unbound
# - any command fails
# - a command in a pipe fails
# - a command in a sub-shell fails
set -Eeuo pipefail
echo "action-debug=container_retention_policy=info" >> "${GITHUB_OUTPUT}"
# enable debug if runner runs in debug
[[ "${{ runner.debug }}" -ne 1 ]] || {
echo "INFO: Enabling bash trace";
set -x;
echo "INFO: Defining 'action-debug' with: container_retention_policy=debug";
echo "action-debug=container_retention_policy=debug" >> "${GITHUB_OUTPUT}";
};
# install jq
dnf install -y jq
# podman arguments to search for tags
declare -ra podmanArguments=(
"search"
"--list-tags"
"--no-trunc"
"--limit"
"999999"
"--format"
"{{.Tag}}"
"${{ env.IMAGE_NAME }}"
)
# retrieve all tags for the image, sort it reverse and keep the last N versions
read -ra keepTags <<<"$(podman "${podmanArguments[@]}" | \
grep -vE "${{ env.IGNORE_TAGS }}" | \
sort -Vr | \
head -n "${{ env.KEEP_VERSIONS }}" | \
xargs \
)"
# add additional user tags to keep
IFS="," read -ra additionalKeepTags <<< "${{ env.ADDITIONAL_KEEP_TAGS }}"
declare -a allKeepTags=("${keepTags[@]}" "${additionalKeepTags[@]}")
# iterate over all tags and retrieve their digests
declare -a skipDigests=()
for keepTag in "${allKeepTags[@]}"; do
echo "INFO: Retrieving digests for tag ${keepTag}"
while read -r digest; do
echo "INFO: Found digest '${digest}'"
skipDigests+=("${digest}")
done < <(podman manifest inspect "${{ env.IMAGE_NAME }}:${keepTag}" | jq -r '.manifests[] | .digest')
# ^ extract the digests of a tag
done
# prepare tags to skip
declare -a skipTags=()
for tag in "${allKeepTags[@]}"; do
skipTags+=("!${tag}")
done
echo "INFO: Defining 'skip-tags' with value: ${skipTags[@]}"
echo "skip-tags=${skipTags[@]}" >> "${GITHUB_OUTPUT}"
# save the digests for a later step
echo "INFO: Defining 'skip-digests' with value: ${skipDigests[@]}"
echo "skip-digests=${skipDigests[@]}" >> "${GITHUB_OUTPUT}"
declare -r imageName="${{ env.IMAGE_NAME }}"
declare -r shortImageName="${imageName##*/}"
# save the image short name (without registry path)
echo "INFO: Defininig 'short-image-name' with value: ${shortImageName}"
echo "short-image-name=${shortImageName}" >> "${GITHUB_OUTPUT}"
container-image-retention:
name: 'Clean old container images'
if: "needs.check-user-permissions.outputs.require-result == 'true'"
needs:
- 'check-user-permissions'
- 'prepare-vars'
runs-on: 'ubuntu-24.04'
permissions:
packages: 'write'
steps:
- name: 'Harden Runner'
uses: 'step-security/harden-runner@0080882f6c36860b6ba35c610c98ce87d4e2f26f' # v2.10.2
with:
disable-sudo: true
egress-policy: 'block'
allowed-endpoints: >
api.github.com:443
github.com:443
- name: 'Delete container versions'
uses: 'snok/container-retention-policy@4f22ef80902ad409ed55a99dc5133cc1250a0d03' # v3.0.0
with:
account: 'user'
cut-off: '1m'
image-names: '${{ needs.prepare-vars.outputs.short-image-name }}'
image-tags: '${{ needs.prepare-vars.outputs.skip-tags }}'
keep-n-most-recent: '${{ env.KEEP_VERSIONS }}'
token: '${{ secrets.GITHUB_TOKEN }}'
tag-selection: 'both'
skip-shas: '${{ needs.prepare-vars.outputs.skip-digests }}'
timestamp-to-use: 'created_at'
dry-run: false
rust-log: '${{ needs.prepare-vars.outputs.action-debug }}'
...