forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 1
184 lines (157 loc) · 6.29 KB
/
gh_registry.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
on:
workflow_dispatch:
schedule:
- cron: 0 3 * * *
name: Update GHCR
jobs:
ghcr_config:
name: Read GHCR config
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
external_images: ${{ steps.ghcr_config.outputs.external_images }}
internal_images: ${{ steps.ghcr_config.outputs.internal_images }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Get images list
id: ghcr_config
run: |
repo_owner=${{ github.repository_owner }}
config=`cat .github/workflows/config/ghcr_config.json | jq ".external_images"`
images=`echo "$config" | jq '.[].source'`
external_images="{\"images\":[],\"tags\":[]}"
for image in $images; do
image_data=`echo "$config" | jq ".[] | select(.source==$image)"`
source=`echo $image_data | jq -r '.source'`
platforms=`echo $image_data | jq -r '.platforms'`
original_name=`echo $source | sed -e 's@.*\.io/\(\)@\1@'`
package_name=${original_name#${repo_owner,,}}
tags=`echo "$image_data" | jq -r '.tags[]'`
external_images=`echo $external_images | jq ".images |= . + [\"$source\"]"`
info={\"$source\":{\"platforms\":\"$platforms\",\"package_name\":\"$package_name\"}}
external_images=`echo $external_images | jq ". |= . + $info"`
for tag in $tags; do
external_images=`echo $external_images | jq ".tags |= . + [\"$source:$tag\"]"`
done
done
config=`cat .github/workflows/config/ghcr_config.json | jq ".internal_images"`
images=`echo "$config" | jq '.[].name'`
internal_images="{\"package_names\":[]}"
for image in $images; do
image_data=`echo "$config" | jq ".[] | select(.name==$image)"`
package_name=`echo $image_data | jq -r '.name'`
nr_tags_to_keep=`echo $image_data | jq -r '.tags_to_keep'`
internal_images=`echo $internal_images | jq ".package_names |= . + [\"$package_name\"]"`
info={\"$package_name\":{\"nr_tags_to_keep\":\"$nr_tags_to_keep\"}}
internal_images=`echo $internal_images | jq ". |= . + $info"`
done
echo "internal_images=$(echo $internal_images)" >> $GITHUB_OUTPUT
echo "external_images=$(echo $external_images)" >> $GITHUB_OUTPUT
delete_images:
name: Delete old images on GHCR
needs: ghcr_config
runs-on: ubuntu-latest
permissions:
packages: write
environment:
name: ghcr-deployment
url: ${{ vars.deployment_url }}
strategy:
matrix:
image: ${{ fromJson(needs.ghcr_config.outputs.external_images).images }}
fail-fast: false
steps:
- name: Login to GitHub CR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Delete old images
uses: actions/delete-package-versions@v5
with:
package-name: ${{ fromJson(needs.ghcr_config.outputs.external_images)[format('{0}', matrix.image)].package_name }}
package-type: 'container'
min-versions-to-keep: 1 # the used action does not support 0 here
continue-on-error: true
push_images:
name: Push new images to GHCR
needs: [ghcr_config, delete_images]
runs-on: ubuntu-latest
permissions:
packages: write
environment:
name: ghcr-deployment
url: ${{ vars.deployment_url }}
strategy:
matrix:
tag: ${{ fromJson(needs.ghcr_config.outputs.external_images).tags }}
fail-fast: false
steps:
- name: Login to GitHub CR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Set up context for buildx
run: |
docker context create builder_context
- name: Set up buildx runner
uses: docker/setup-buildx-action@v3
with:
endpoint: builder_context
- name: Prepare push
id: metadata
run: |
dockerfile=image.Dockerfile
echo 'FROM ${{ matrix.tag }}' > $dockerfile
repo_owner=${{ github.repository_owner }}
original_name=`echo ${{ matrix.tag }} | rev | cut -d : -f2- | rev`
original_tag=`echo ${{ matrix.tag }} | rev | cut -d : -f1 | rev`
external_images='${{ needs.ghcr_config.outputs.external_images }}'
package_name=`echo $external_images | jq -r ".\"$original_name\".package_name"`
platforms=`echo $external_images | jq -r ".\"$original_name\".platforms"`
# No need to specify labels - they propagate automatically
echo "dockerfile=$dockerfile" >> $GITHUB_OUTPUT
echo "tags=${{ vars.registry }}/${repo_owner,,}/${package_name}:${original_tag}" >> $GITHUB_OUTPUT
echo "platforms=$platforms" >> $GITHUB_OUTPUT
- name: Push image to GHCR
id: docker_build
uses: docker/build-push-action@v5
with:
context: .
file: ${{ steps.metadata.outputs.dockerfile }}
tags: ${{ steps.metadata.outputs.tags }}
labels: ${{ steps.metadata.outputs.labels }}
platforms: ${{ steps.metadata.outputs.platforms }}
push: true
delete_internal_images:
name: Clean up internal images on GHCR
needs: ghcr_config
runs-on: ubuntu-latest
permissions:
packages: write
environment:
name: ghcr-deployment
url: ${{ vars.deployment_url }}
strategy:
matrix:
package_name: ${{ fromJson(needs.ghcr_config.outputs.internal_images).package_names }}
fail-fast: false
steps:
- name: Login to GitHub CR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Delete old images
uses: actions/delete-package-versions@v5
with:
package-name: ${{ vars.packages_prefix }}${{ matrix.package_name }}
package-type: 'container'
min-versions-to-keep: ${{ fromJson(needs.ghcr_config.outputs.internal_images)[format('{0}', matrix.package_name)].nr_tags_to_keep }}
continue-on-error: true