-
Notifications
You must be signed in to change notification settings - Fork 18
314 lines (284 loc) · 11.8 KB
/
builder.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
name: Build ublue-builder
on:
workflow_dispatch: # allow manually triggering builds
pull_request:
branches:
- main
paths:
- 'ublue-builder/*'
push:
branches:
- main
paths:
- 'ublue-builder/*'
schedule:
- cron: '0 0 * * TUE'
env:
IMAGE_NAME: ublue-builder
IMAGE_DESC: "Builder image for Universal Blue package building"
IMAGE_REGISTRY: "ghcr.io/${{ github.repository_owner }}"
DEFAULT_TAG: "latest"
PLATFORMS: "amd64,arm64"
jobs:
generate_matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Set matrix
id: set-matrix
run: |
# turn the comma separated string into a list
platforms=()
IFS=',' read -r -a platforms <<< "${{ env.PLATFORMS }}"
MATRIX="{\"include\":[]}"
for platform in "${platforms[@]}"; do
MATRIX=$(echo $MATRIX | jq ".include += [{\"platform\": \"$platform\"}]")
done
echo "matrix=$(echo $MATRIX | jq -c '.')" >> $GITHUB_OUTPUT
build_push:
name: Build and push image
runs-on: ${{ matrix.platform == 'amd64' && 'ubuntu-24.04' || 'ubuntu-24.04-arm' }}
timeout-minutes: 30
needs: generate_matrix
permissions:
contents: read
packages: write
id-token: write
strategy:
fail-fast: false
matrix: ${{fromJson(needs.generate_matrix.outputs.matrix)}}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install dependencies
if: matrix.platform == 'arm64'
run: |
sudo apt update -y
sudo apt install -y \
podman
- name: Build Image
id: build_image
uses: redhat-actions/buildah-build@v2
with:
containerfiles: |
./${{ env.IMAGE_NAME }}/Containerfile
context: ${{ env.IMAGE_NAME }}
image: ${{ env.IMAGE_NAME }}
tags: ${{ env.DEFAULT_TAG }}
labels: ${{ steps.meta.outputs.labels }}
oci: true
- name: Login to GHCR
if: github.event_name != 'pull_request'
run: |
echo ${{ secrets.GITHUB_TOKEN }} | podman login -u ${{ github.actor }} --password-stdin ghcr.io
echo ${{ secrets.GITHUB_TOKEN }} | docker login -u ${{ github.actor }} --password-stdin ghcr.io
- name: Push to GHCR
if: github.event_name != 'pull_request'
id: push
env:
IMAGE_REGISTRY: ${{ env.IMAGE_REGISTRY }}
IMAGE_NAME: ${{ env.IMAGE_NAME }}
IMAGE_DIGEST: ${{ steps.build_image.outputs.digest }}
PLATFORM: ${{ matrix.platform }}
run: |
set -x
podman tag localhost/${IMAGE_NAME}:${DEFAULT_TAG} ${{ env.IMAGE_REGISTRY }}/${IMAGE_NAME}:${DEFAULT_TAG}
podman tag ${{ env.IMAGE_REGISTRY }}/${IMAGE_NAME}:${DEFAULT_TAG} $IMAGE_REGISTRY/$IMAGE_NAME:$DEFAULT_TAG-$PLATFORM
for i in {1..3}; do
podman push --digestfile=/tmp/digestfile $IMAGE_REGISTRY/$IMAGE_NAME:$DEFAULT_TAG-$PLATFORM && break || sleep $((5 * i));
done
REMOTE_IMAGE_DIGEST=$(cat /tmp/digestfile)
echo "remote_image_digest=$REMOTE_IMAGE_DIGEST" >> $GITHUB_OUTPUT
cat /tmp/digestfile
- name: Install Cosign
uses: sigstore/cosign-installer@d7d6bc7722e3daa8354c50bcb52f4837da5e9b6a # v3.8.1
if: github.event_name != 'pull_request'
- name: Sign Image
if: github.event_name != 'pull_request'
run: |
IMAGE_FULL="${{ env.IMAGE_REGISTRY }}/${IMAGE_NAME}"
cosign sign -y --key env://COSIGN_PRIVATE_KEY ${IMAGE_FULL}@${{ steps.push.outputs.remote_image_digest }}
env:
TAGS: ${{ steps.push.outputs.digest }}
COSIGN_EXPERIMENTAL: false
COSIGN_PRIVATE_KEY: ${{ secrets.SIGNING_SECRET }}
- name: Create Job Outputs
if: github.event_name != 'pull_request'
env:
IMAGE_NAME: ${{ env.IMAGE_NAME }}
PLATFORM: ${{ matrix.platform }}
DIGEST: ${{ steps.push.outputs.remote_image_digest }}
run: |
mkdir -p /tmp/outputs/digests
echo "${DIGEST}" > /tmp/outputs/digests/${IMAGE_NAME}-${PLATFORM}.txt
- name: Upload Output Artifacts
if: github.event_name != 'pull_request'
uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 # v4
with:
name: ${{ env.IMAGE_NAME }}-${{ matrix.platform }}
retention-days: 1
if-no-files-found: error
path: |
/tmp/outputs/digests/*.txt
manifest:
name: Create shared manifest
runs-on: ubuntu-latest
if: always()
needs:
- generate_matrix
- build_push
container:
image: quay.io/fedora/fedora:41
options: --privileged --security-opt seccomp=unconfined
permissions:
contents: read
packages: write
id-token: write
outputs:
image: ${{ steps.push_manifest.outputs.IMAGE }}
digest: ${{ steps.push_manifest.outputs.DIGEST }}
steps:
- name: Install dependencies
run: |
dnf install -y \
jq \
git
dnf copr enable -y rhcontainerbot/podman-next
dnf install -y podman
- name: Exit on failure
env:
JOBS: ${{ toJson(needs) }}
run: |
echo "Job status:"
echo $JOBS | jq -r 'to_entries[] | " - \(.key): \(.value.result)"'
for i in $(echo $JOBS | jq -r 'to_entries[] | .value.result'); do
if [ "$i" != "success" ] && [ "$i" != "skipped" ]; then
echo ""
echo "Status check not okay!"
exit 1
fi
done
- name: Get current date
id: date
run: |
# Should generate a timestamp like what is defined on the ArtifactHub documentation
# E.G: 2022-02-08T15:38:15Z'
# https://artifacthub.io/docs/topics/repositories/container-images/
# https://linux.die.net/man/1/date
echo "date=$(date -u +%Y\-%m\-%d\T%H\:%M\:%S\Z)" >> $GITHUB_OUTPUT
- name: Image Metadata
uses: docker/metadata-action@902fa8ec7d6ecbf8d84d538b9b233a880e428804 # v5
id: metadata
with:
tags: |
type=raw,value=latest
type=ref,event=pr
labels: |
io.artifacthub.package.readme-url=https://raw.githubusercontent.com/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}/refs/heads/main/README.md
org.opencontainers.image.created=${{ steps.date.outputs.date }}
org.opencontainers.image.description=${{ env.IMAGE_DESC }}
org.opencontainers.image.documentation=https://raw.githubusercontent.com/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}/refs/heads/main/README.md
org.opencontainers.image.source=https://github.com/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}/blob/main/Containerfile
org.opencontainers.image.title=${{ env.IMAGE_NAME }}
org.opencontainers.image.url=https://github.com/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}
org.opencontainers.image.vendor=${{ github.repository_owner }}
org.opencontainers.image.version=${{ env.DEFAULT_TAG }}
io.artifacthub.package.deprecated=false
io.artifacthub.package.keywords=ublue,universal-blue,copr,fedora
io.artifacthub.package.license=Apache-2.0
io.artifacthub.package.logo-url=https://avatars.githubusercontent.com/u/120078124?s=200&v=4
io.artifacthub.package.maintainers=[{\"name\":\"tulilirockz\",\"email\":\"tulilirockz@outlook.com\"}]
- name: Fetch Build Outputs
if: github.event_name != 'pull_request'
uses: actions/download-artifact@cc203385981b70ca67e1cc392babf9cc229d5806 # v4
with:
pattern: ${{ env.IMAGE_NAME }}-*
merge-multiple: true
path: /tmp/artifacts
- name: Load Outputs
if: github.event_name != 'pull_request'
id: load-outputs
run: |
set -x
DIGESTS_JSON=$(jq -n '{}')
for digest_file in /tmp/artifacts/*.txt; do
# Extract the platform from the file name
PLATFORM=$(basename $digest_file | rev | cut -d'-' -f1 | rev | cut -d'.' -f1)
DIGEST=$(cat $digest_file)
# Add the platform and digest to the JSON object
DIGESTS_JSON=$(echo "$DIGESTS_JSON" | jq --arg key "$PLATFORM" --arg value "$DIGEST" '. + {($key): $value}')
done
echo "DIGESTS_JSON=$(echo $DIGESTS_JSON | jq -c '.')" >> $GITHUB_OUTPUT
- name: Create Manifest
if: github.event_name != 'pull_request'
id: create-manifest
run: |
podman manifest create ${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}
echo "MANIFEST=${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}" >> $GITHUB_OUTPUT
- name: Populate Manifest
if: github.event_name != 'pull_request'
env:
MANIFEST: ${{ steps.create-manifest.outputs.MANIFEST }}
DIGESTS_JSON: ${{ steps.load-outputs.outputs.DIGESTS_JSON }}
LABELS: ${{ steps.metadata.outputs.labels }}
run: |
set -x
DIGESTS=$(echo "$DIGESTS_JSON" | jq -c '.')
# turn the comma separated string into a list
platforms=()
IFS=',' read -r -a platforms <<< "${{ env.PLATFORMS }}"
for platform in ${platforms[@]}; do
digest=$(echo $DIGESTS | jq -r ".$platform")
echo "Adding ${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}@$digest for $platform"
podman manifest add $MANIFEST ${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}@$digest --arch $platform
done
# Apply the labels to the manifest (separated by newlines)
while IFS= read -r label; do
echo "Applying label $label to manifest"
podman manifest annotate --index --annotation "$label" $MANIFEST
done <<< "$LABELS"
- name: Login to GHCR
if: github.event_name != 'pull_request'
run: echo ${{ secrets.GITHUB_TOKEN }} | podman login -u ${{ github.actor }} --password-stdin ghcr.io
- name: Push Manifest
if: github.event_name != 'pull_request'
id: push_manifest
env:
MANIFEST: ${{ steps.create-manifest.outputs.MANIFEST }}
TAGS: ${{ steps.metadata.outputs.tags }}
IMAGE_REGISTRY: ${{ env.IMAGE_REGISTRY }}
IMAGE_NAME: ${{ env.IMAGE_NAME }}
run: |
set -x
while IFS= read -r tag; do
podman manifest push --all=false --digestfile=/tmp/digestfile $MANIFEST $IMAGE_REGISTRY/$IMAGE_NAME:$tag
done <<< "$TAGS"
DIGEST=$(cat /tmp/digestfile)
echo "DIGEST=$DIGEST" >> $GITHUB_OUTPUT
echo "IMAGE=$IMAGE_REGISTRY/$IMAGE_NAME" >> $GITHUB_OUTPUT
# Cosign throws errors when ran inside the Fedora container for one reason or another
# so we move this to another step in order to run on Ubuntu
sign:
needs: manifest
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Login to GHCR
run: |
echo ${{ secrets.GITHUB_TOKEN }} | docker login -u ${{ github.actor }} --password-stdin ghcr.io
echo ${{ secrets.GITHUB_TOKEN }} | podman login -u ${{ github.actor }} --password-stdin ghcr.io
- name: Install Cosign
uses: sigstore/cosign-installer@d7d6bc7722e3daa8354c50bcb52f4837da5e9b6a # v3.8.1
- name: Sign Manifest
run: |
cosign sign -y --key env://COSIGN_PRIVATE_KEY $IMAGE@$DIGEST
env:
DIGEST: ${{ needs.manifest.outputs.digest }}
IMAGE: ${{ needs.manifest.outputs.image }}
COSIGN_EXPERIMENTAL: false
COSIGN_PRIVATE_KEY: ${{ secrets.SIGNING_SECRET }}