-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.sh
More file actions
executable file
·368 lines (326 loc) · 10.9 KB
/
builder.sh
File metadata and controls
executable file
·368 lines (326 loc) · 10.9 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env bash
set -eo pipefail
# Include the utils library
source scripts/lib_utils.sh
CLI="docker"
MANIFEST_FILE="manifest.yaml"
IMAGE_TAG="latest"
IMAGE_FORMAT="oci"
UBUNTU_VERSION="24.04"
APP_UID="1000"
BUILD_DIR="./build"
check_for_manifest(){
if [[ ! -f "$MANIFEST_FILE" ]]; then
log_error "Manifest file not found"
exit 1
fi
}
retrieve_name_from_manifest(){
local name
name=$(yq e '.name' $MANIFEST_FILE)
echo $name
}
retrieve_registry_from_manifest(){
local registry
registry=$(yq e '.registry' $MANIFEST_FILE)
echo $registry
}
clean_build_dir(){
if [[ -d "${BUILD_DIR}" ]]; then
log_trace "Removing existing build directory"
rm -rf "${BUILD_DIR}"
fi
mkdir -p "${BUILD_DIR}"
}
hadolint_validate(){
local hadolint_exec
local hadolint_exit_code
log_info "Validating Containerfile with hadolint"
${CLI} pull -q ghcr.io/hadolint/hadolint:latest > /dev/null
log_trace "$(${CLI} run --rm -i hadolint/hadolint:latest hadolint -v)"
set +e
hadolint_exec=$(
${CLI} run --rm -i -v "$(pwd)/.hadolint.yaml:/.hadolint.yaml:ro" hadolint/hadolint:latest hadolint --config /.hadolint.yaml - < Containerfile \
2>&1
)
hadolint_exit_code=$?
set -e
if [[ $hadolint_exit_code -ne 0 ]]; then
echo -e "${WHITE_GRAY}${hadolint_exec}${NC}"
log_error "Hadolint validation failed"
exit 1
else
log_success "Hadolint validation passed"
fi
}
buildah_build(){
local buildah_exec
local buildah_exit_code
local buildah_args
local buildah_labels
local buildah_labels_array
local manifest_args
log_info "Build Containerfile for ${IMAGE_NAME}:${IMAGE_TAG}"
log_trace "$(buildah --version)"
# Extract build args from manifest
buildah_args=()
for arg in $(yq e '.build.args[]' $MANIFEST_FILE); do
buildah_args+="--build-arg ${arg} "
done
# Extract labels from manifest
buildah_labels=()
buildah_labels_array=()
while IFS= read -r label; do
if [[ -n "${label}" ]]; then
# Parse key=value and remove quotes from value if present
# Handle both key=value and key="value" formats
if [[ "${label}" =~ ^([^=]+)=(.*)$ ]]; then
local label_key="${BASH_REMATCH[1]}"
local label_value="${BASH_REMATCH[2]}"
# Remove surrounding quotes from value if present
label_value=$(echo "${label_value}" | sed -e 's/^"//' -e 's/"$//')
# Reconstruct label without quotes in value
label="${label_key}=${label_value}"
fi
# Add to both string (for logging) and array (for command)
buildah_labels+="--label ${label} "
buildah_labels_array+=("--label" "${label}")
fi
done < <(yq e '.build.labels[]' $MANIFEST_FILE)
log_trace "Buildah args: ${buildah_args}"
log_trace "Buildah labels: ${buildah_labels}"
set +e
buildah_exec=$(
buildah build \
--squash \
--pull-always \
--format ${IMAGE_FORMAT} \
${buildah_args} \
"${buildah_labels_array[@]}" \
--tag ${IMAGE_NAME}:${IMAGE_TAG} \
. \
2>&1
)
buildah_exit_code=$?
set -e
if [[ $buildah_exit_code -ne 0 ]]; then
log_error "Build failed"
log_error "${buildah_exec}"
exit 1
else
log_success "Build completed successfully"
fi
# Copy to docker-daemon after successful build
# Save image to tar first, then load into docker daemon
# Store tar path in a variable for later use with skopeo
export BUILD_TAR="${BUILD_DIR}/${IMAGE_NAME}-${IMAGE_TAG}-temp.tar"
log_info "Saving image to temporary tar: ${BUILD_TAR}"
set +e
buildah_exec=$(
buildah push ${IMAGE_NAME}:${IMAGE_TAG} oci-archive:${BUILD_TAR} \
2>&1
)
buildah_exit_code=$?
set -e
if [[ $buildah_exit_code -ne 0 ]]; then
log_error "Failed to save image to tar"
log_error "${buildah_exec}"
exit 1
else
log_success "Image saved to tar successfully"
# Verify labels are in the tar file
if command -v skopeo &> /dev/null; then
local tar_labels
tar_labels=$(skopeo inspect oci-archive:${BUILD_TAR} --format '{{.Labels}}' 2>/dev/null || echo "")
if [[ -n "${tar_labels}" ]]; then
log_trace "Labels in tar file: ${tar_labels}"
else
log_warn "No labels found in tar file"
fi
fi
fi
log_info "Loading image into Docker daemon: ${IMAGE_NAME}:${IMAGE_TAG}"
set +e
buildah_exec=$(
docker load -i ${BUILD_TAR} \
2>&1
)
buildah_exit_code=$?
set -e
if [[ $buildah_exit_code -ne 0 ]]; then
log_error "Failed to load image into Docker daemon"
log_error "${buildah_exec}"
exit 1
fi
# docker load might not preserve the tag, so we need to tag it
# Extract the loaded image name/ID from the output
# Format can be: "Loaded image: name:tag" or "Loaded image ID: sha256:..."
local loaded_image=""
if echo "${buildah_exec}" | grep -qi "Loaded image:"; then
# Extract image name:tag format
loaded_image=$(echo "${buildah_exec}" | grep -i "Loaded image:" | sed -E 's/.*Loaded image: //' | head -n1 | tr -d '\r\n')
elif echo "${buildah_exec}" | grep -qi "Loaded image ID:"; then
# Extract just the sha256:... part
loaded_image=$(echo "${buildah_exec}" | grep -i "Loaded image ID:" | sed -E 's/.*Loaded image ID: //' | head -n1 | tr -d '\r\n')
fi
if [[ -n "${loaded_image}" && "${loaded_image}" != "${IMAGE_NAME}:${IMAGE_TAG}" ]]; then
log_info "Tagging loaded image ${loaded_image} as ${IMAGE_NAME}:${IMAGE_TAG}"
set +e
buildah_exec=$(
docker tag "${loaded_image}" "${IMAGE_NAME}:${IMAGE_TAG}" \
2>&1
)
buildah_exit_code=$?
set -e
if [[ $buildah_exit_code -ne 0 ]]; then
log_error "Failed to tag image"
log_error "${buildah_exec}"
exit 1
fi
fi
log_success "Image loaded into Docker daemon successfully"
}
podman_save_image_to_tar(){
local podman_exec
local podman_exit_code
log_info "Saving image to tar ${IMAGE_NAME}:${IMAGE_TAG}"
log_trace "$(podman --version)"
set +e
podman_exec=$(
${CLI} save \
--output ${BUILD_DIR}/${IMAGE_NAME}-${IMAGE_TAG}.tar \
${IMAGE_NAME}:${IMAGE_TAG} \
2>&1
)
podman_exit_code=$?
set -e
if [[ $podman_exit_code -ne 0 ]]; then
echo -e "${WHITE_GRAY}${podman_exec}${NC}"
log_error "Saving image to tar failed"
exit 1
else
log_success "Image saved to ${BUILD_DIR}/${IMAGE_NAME}-${IMAGE_TAG}.tar"
fi
}
docker_save_image_to_tar(){
local docker_exec
local docker_exit_code
log_info "Saving image to tar ${IMAGE_NAME}:${IMAGE_TAG}"
log_trace "$(docker --version)"
set +e
docker_exec=$(
${CLI} save \
--output ${BUILD_DIR}/${IMAGE_NAME}-${IMAGE_TAG}.tar \
${IMAGE_NAME}:${IMAGE_TAG} \
2>&1
)
docker_exit_code=$?
set -e
if [[ $docker_exit_code -ne 0 ]]; then
echo -e "${WHITE_GRAY}${docker_exec}${NC}"
log_error "Saving image to tar failed"
exit 1
else
log_success "Image saved to ${BUILD_DIR}/${IMAGE_NAME}-${IMAGE_TAG}.tar"
fi
}
dive_scan() {
local dive_scan
log_info "Running dive scan on ${IMAGE_NAME}:${IMAGE_TAG}"
log_trace "$(dive --version)"
set +e
dive_scan=$(\
dive \
--ci \
--source=${CLI} \
${IMAGE_NAME}:${IMAGE_TAG} \
2>&1 \
)
set -e
if [[ $dive_scan == *"FAIL"* ]]; then
echo -e "${WHITE_GRAY}${dive_scan}${NC}"
log_error "Dive scan failed"
exit 1
else
log_success "Dive scan passed"
fi
}
trivy_scan () {
local trivy_scan_exec
local trivy_scan_exit_code
log_info "Running trivy scan on ${IMAGE_NAME}:${IMAGE_TAG}"
log_trace "$(trivy --version)"
set +e
trivy_scan_exec=$(\
trivy image \
--scanners vuln \
--ignore-unfixed \
--pkg-types library \
--skip-dirs /home/runner/externals \
--skip-dirs /usr/local/lib/docker \
--skip-files /usr/bin/dockerd \
--ignorefile .trivyignore \
--input ${BUILD_DIR}/${IMAGE_NAME}-${IMAGE_TAG}.tar \
--format github \
--severity HIGH,CRITICAL \
--exit-code 2 \
${IMAGE_NAME}:${IMAGE_TAG} \
2>&1
)
# Detect exit code
trivy_scan_exit_code=$?
set -e
if [[ $trivy_scan_exit_code -eq 2 ]]; then
echo -e "${WHITE_GRAY}${trivy_scan_exec}${NC}"
log_error "Trivy scan failed"
exit 1
elif [[ $trivy_scan_exit_code -eq 1 ]]; then
echo -e "${WHITE_GRAY}${trivy_scan_exec}${NC}"
log_error "Trivy scan error"
else
log_success "Trivy scan passed"
fi
}
# Main
clean_build_dir
check_for_manifest # Check for manifest file existence
IMAGE_NAME=$(retrieve_name_from_manifest) # Retrieve image name from manifest
log_info "Starting build process"
log_trace "CLI: ${CLI}"
log_trace "IMAGE_NAME: ${IMAGE_NAME}"
log_trace "IMAGE_TAG: ${IMAGE_TAG}"
log_trace "IMAGE_FORMAT: ${IMAGE_FORMAT}"
hadolint_validate # Validate/Lint Containerfile
buildah_build # Build Containerfile
if [[ $CLI == "podman" ]]; then
podman_save_image_to_tar # Save image to tar (for trivy scan)
elif [[ $CLI == "docker" ]]; then
docker_save_image_to_tar # Save image to tar (for trivy scan)
else
log_error "Invalid CLI"
exit 1
fi
dive_scan # Filesystem scan and analysis
trivy_scan # Vulnerability scan
# Deploy to registry with skopeo using tags in manifest
# Use oci-archive (tar file) as source to avoid Docker API version issues
registry=$(retrieve_registry_from_manifest)
if [[ -n "${BUILD_TAR}" && -f "${BUILD_TAR}" ]]; then
log_info "Pushing image to registry: ${registry}:${IMAGE_TAG}"
# Use --all to copy all formats and preserve metadata including labels
skopeo copy --all oci-archive:${BUILD_TAR} docker://${registry}:${IMAGE_TAG}
log_success "Image pushed to registry successfully"
# Verify labels in registry
log_info "Verifying labels in registry..."
registry_labels=$(skopeo inspect docker://${registry}:${IMAGE_TAG} --format '{{.Labels}}' 2>/dev/null || echo "")
if [[ -n "${registry_labels}" ]]; then
log_trace "Labels in registry: ${registry_labels}"
else
log_warn "No labels found in registry image"
fi
# Clean up temp tar file after registry push
rm -f ${BUILD_TAR}
else
log_error "Build tar file not found, cannot push to registry"
exit 1
fi