This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
293 lines (288 loc) · 10.8 KB
/
build.yaml
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
name: build
on:
push:
branches:
- main
paths:
- images/**
- '**.mk'
- .github/workflows/build.yaml
pull_request:
branches:
- main
paths:
- images/**
- '**.mk'
- .github/workflows/build.yaml
workflow_dispatch:
inputs:
folders:
description: 'Folders to build'
required: true
default: ''
permissions: read-all
jobs:
generate_build_input:
name: "Generate build input"
runs-on: ubuntu-latest
outputs:
folders_to_build: ${{ env.FOLDERS }}
steps:
- name: Check out source code
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Read modified folders from parameters
if: ${{ github.event_name == 'workflow_dispatch' }}
run: |
echo "CANDIDATE_FOLDERS=${{ github.event.inputs.folders }}" >> $GITHUB_ENV
- name: Read modified files from commit
id: changed_files
if: ${{ github.event_name != 'workflow_dispatch' }}
uses: tj-actions/changed-files@v35
with:
files: |
images/**
*.mk
- name: Identify modified folders from commit
if: ${{ github.event_name != 'workflow_dispatch' }}
run: |
set -x
cd ${GITHUB_WORKSPACE}
folders=""
for changed_file in ${{ steps.changed_files.outputs.all_modified_files }}; do
if [[ "${changed_file}" == *.mk ]]; then
# use make magic to identify which makefiles under images include a particular .mk under repo root
for folder in $(ls ./images/); do
if make -pn -C ./images/${folder} | grep $(basename ${changed_file}); then
folders="${folders} ${folder}"
fi
done
fi
folder=$(echo ${changed_file} | awk -F / '{print $2}')
folders="${folders} ${folder}"
done
folders=$(echo ${folders} | tr ' ' '\n' | sort -u | tr '\n' ' ')
echo CANDIDATE_FOLDERS="${folders}" >> $GITHUB_ENV
- name: Validate folders to build
run: |
set -x
for folder in ${CANDIDATE_FOLDERS}; do
[ -f "${GITHUB_WORKSPACE}/images/${folder}/stacker.yaml" ] && [ -f "${GITHUB_WORKSPACE}/images/${folder}/Makefile" ] && folders="${folders} ${folder}"
done
folders=$(echo ${folders} | xargs echo -n)
if [ -z "${folders}" ]; then
echo "No valid folders to build"
if [[ ${{ github.event_name }} == 'workflow_dispatch' ]]; then
echo "Manually triggered build needs valid folders to build"
exit 1
else
echo "Build will not fail as it is triggered automatically"
echo "FOLDERS=" >> $GITHUB_ENV
exit 0
fi
fi
# Obtain images modified indirectly
folders=$(make build-candidates SUBDIRS="${folders}" | tail -n 1)
echo "FOLDERS=${folders}" >> $GITHUB_ENV
build:
name: Build only
needs: [generate_build_input]
if: |
(github.ref != 'refs/heads/main') &&
(needs.generate_build_input.outputs.folders_to_build != '')
runs-on: ubuntu-latest
timeout-minutes: 30
strategy:
max-parallel: 3
matrix:
os:
- linux
arch:
- amd64
distro:
- debian-bullseye
- ubuntu-jammy
- rockylinux-9
steps:
- name: Check out source code
uses: actions/checkout@v3
- name: Setup env vars
run: |
distro=${{ matrix.distro }}
x=$(echo $distro | tr '-' ' ')
DISTRO=$(echo $x| awk '{print $1}')
echo DISTRO=$DISTRO >> $GITHUB_ENV
DISTRO_REL=$(echo $x| awk '{print $2}')
echo DISTRO_REL=$DISTRO_REL >> $GITHUB_ENV
echo ARCH=${{ matrix.arch }} >> $GITHUB_ENV
echo OS=${{ matrix.os }} >> $GITHUB_ENV
echo SUBDIRS="${{needs.generate_build_input.outputs.folders_to_build}}" >> $GITHUB_ENV
echo ZOT_VERSION="v2.0.0-rc3" >> $GITHUB_ENV
echo DEFAULT_BRANCH=origin/${{ github.event.repository.default_branch }} >> $GITHUB_ENV
- name: Run zot container image with podman
run: |
wget -N https://raw.githubusercontent.com/project-zot/zot/${ZOT_VERSION}/examples/config-cve.json
sed -i s/127\.0\.0\.1/0.0.0.0/g config-cve.json
sed -i s/8080/5000/g config-cve.json
podman run -d -p 5000:5000 -v $PWD/config-cve.json:/etc/zot/config.json ghcr.io/project-zot/zot-linux-amd64:${ZOT_VERSION}
- name: Fetch all commits
run: |
# Needed in order to check commit order later
cd ${GITHUB_WORKSPACE}
git fetch --all
- name: Build container images
run: |
cd ${GITHUB_WORKSPACE}
echo "Building ${SUBDIRS}"
# for building PRs PUBLISH_URL points to the public server, so we can download unchanged images
make build \
SUBDIRS="${SUBDIRS}" \
PUBLISH_URL=docker://zothub.io/c3
- name: Test container images
run: |
cd ${GITHUB_WORKSPACE}
echo "Testing ${SUBDIRS}"
make test SUBDIRS="${SUBDIRS}"
- name: Push container images
run: |
cd ${GITHUB_WORKSPACE}
echo "Pushing ${SUBDIRS}"
# for publishing PRs PUBLISH_URL points to the local server so we can later run the CVE scanner
# without having to push images to the public server
make publish \
SUBDIRS="${SUBDIRS}" \
PUBLISH_URL=docker://localhost:5000/c3 \
PUBLISH_EXTRA_ARGS="--skip-tls" \
PULL_EXTRA_ARGS="--src-tls-verify=false"
- name: Scan container images
run: |
set -x
cd ${GITHUB_WORKSPACE}
# download zli
wget -N https://github.com/project-zot/zot/releases/download/${ZOT_VERSION}/zli-linux-amd64 -O hack/tools/bin/zli
chmod +x hack/tools/bin/zli
hack/tools/bin/zli config add local http://localhost:5000
hack/tools/bin/zli config local verify-tls false
hack/tools/bin/zli config local showspinner false
# there is an assumption that every folder contains a single image defined
# in the stacker yaml, having the same name as the folder
for folder in ${SUBDIRS}; do
tags="$(make -C images/${folder} --no-print-directory tags)"
for tag in ${tags}; do
hack/tools/bin/zli cve local -I c3/${DISTRO}/${folder}-${ARCH}:${tag}
done
done
build_publish:
name: Build and publish
needs: [generate_build_input]
if: |
(github.ref == 'refs/heads/main') &&
(needs.generate_build_input.outputs.folders_to_build != '')
runs-on: ubuntu-latest
timeout-minutes: 30
env:
DOCKER_CONFIG: $HOME/.docker
strategy:
max-parallel: 3
matrix:
os:
- linux
arch:
- amd64
distro:
- debian-bullseye
- ubuntu-jammy
- rockylinux-9
steps:
- name: Check out source code
uses: actions/checkout@v3
- name: Setup env vars
run: |
distro=${{ matrix.distro }}
x=$(echo $distro | tr '-' ' ')
DISTRO=$(echo $x| awk '{print $1}')
echo DISTRO=$DISTRO >> $GITHUB_ENV
DISTRO_REL=$(echo $x| awk '{print $2}')
echo DISTRO_REL=$DISTRO_REL >> $GITHUB_ENV
echo ARCH=${{ matrix.arch }} >> $GITHUB_ENV
echo OS=${{ matrix.os }} >> $GITHUB_ENV
echo SUBDIRS="${{needs.generate_build_input.outputs.folders_to_build}}" >> $GITHUB_ENV
echo ZOT_VERSION="v1.4.3" >> $GITHUB_ENV
echo DEFAULT_BRANCH=origin/${{ github.event.repository.default_branch }} >> $GITHUB_ENV
- name: Fetch all commits
run: |
cd ${GITHUB_WORKSPACE}
# Needed in order to check commit order later
git fetch --all
- name: Build container images
run: |
cd ${GITHUB_WORKSPACE}
echo "Building ${SUBDIRS}"
make build \
SUBDIRS="${SUBDIRS}" \
PUBLISH_URL=docker://zothub.io/c3
- name: Test container images
run: |
cd ${GITHUB_WORKSPACE}
echo "Testing ${SUBDIRS}"
make test SUBDIRS="${SUBDIRS}"
- name: Push container images
env:
PUBLISH_USERNAME: ${{ secrets.ZOTHUB_USERNAME }}
PUBLISH_PASSWORD: ${{ secrets.ZOTHUB_PASSWORD }}
run: |
cd ${GITHUB_WORKSPACE}
echo "Pushing ${SUBDIRS}"
make publish \
SUBDIRS="${SUBDIRS}" \
PUBLISH_URL=docker://zothub.io/c3
- name: Scan container images
run: |
set -x
cd ${GITHUB_WORKSPACE}
# download zli
wget -N https://github.com/project-zot/zot/releases/download/${ZOT_VERSION}/zli-linux-amd64 -O hack/tools/bin/zli
chmod +x hack/tools/bin/zli
hack/tools/bin/zli config add zothub https://zothub.io
hack/tools/bin/zli config zothub showspinner false
# there is an assumption that every folder contains a single image defined
# in the stacker yaml, having the same name as the folder
for folder in ${SUBDIRS}; do
tags="$(make -C images/${folder} --no-print-directory tags)"
for tag in ${tags}; do
hack/tools/bin/zli cve zothub -I c3/${DISTRO}/${folder}-${ARCH}:${tag}
done
done
- name: Login to zothub.io Registry
uses: docker/login-action@v2
with:
registry: zothub.io
username: ${{ secrets.ZOTHUB_USERNAME }}
password: ${{ secrets.ZOTHUB_PASSWORD }}
- name: Install go
uses: actions/setup-go@v3
with:
go-version: 1.18
check-latest: true
- name: Install Cosign
uses: sigstore/cosign-installer@main
with:
cosign-release: main
- name: Check cosign install!
run: cosign version
- name: Sign image with a key
run: |
# there is an assumption that every folder contains a single image defined
# in the stacker yaml, having the same name as the folder
for folder in ${SUBDIRS}; do
tags="$(make -C images/${folder} --no-print-directory tags)"
for tag in ${tags}; do
cosign sign --key env://COSIGN_PRIVATE_KEY zothub.io/c3/${DISTRO}/${folder}-${ARCH}:${tag}
# Need to retest squashfs builds, getting some errors locally
# cosign sign --key env://COSIGN_PRIVATE_KEY zothub.io/c3/${DISTRO}/${folder}-${ARCH}:${tag}-squashfs
done
done
env:
COSIGN_PRIVATE_KEY: ${{secrets.COSIGN_PRIVATE_KEY}}
COSIGN_PASSWORD: ${{secrets.COSIGN_PASSWORD}}