Skip to content

Commit 6e27a4a

Browse files
authored
feat: add test-images.sh hook (#232)
* fix: name of retag-images * feat: add test_images.sh hook * Add IMAGE_... env variables * Make execution relative to root * Add test_images_report * Add back code * Adjust skip_push * Revert to main
1 parent 07619b9 commit 6e27a4a

File tree

1 file changed

+93
-15
lines changed

1 file changed

+93
-15
lines changed

.github/workflows/build-docker-artifacts.yml

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ on:
77
type: string
88
required: false
99
default: ""
10-
description: Comma separated list of build keys. Leave empty to run for all.
10+
description: "Comma separated list of build keys. Leave empty to run for all."
1111
push_to:
1212
type: string
1313
required: false
1414
default: ""
15-
description: Comma separated list of push keys. Leave empty to trigger for all.
15+
description: "Comma separated list of push keys. Leave empty to trigger for all."
1616
branch:
1717
type: string
1818
required: false
1919
skip_push:
20+
description: "Skip retagging and pushing the built images to customer registries"
2021
type: boolean
2122
required: false
2223
default: false
@@ -25,7 +26,7 @@ on:
2526
required: false
2627
default: true
2728
scan_high_severity:
28-
description: 'Include high severity'
29+
description: "Include high severity"
2930
type: boolean
3031
required: false
3132
default: true
@@ -60,7 +61,7 @@ jobs:
6061
result: ${{ steps.get-flavors.outputs.result }}
6162
# Do not run this on self-hosted, as it is faster and shouldn't be blocking anything
6263
# runs-on: ${{ inputs.runs_on || 'ubuntu-22.04' }}
63-
runs-on: 'ubuntu-22.04'
64+
runs-on: "ubuntu-22.04"
6465
steps:
6566
- name: Checkout repository
6667
uses: actions/checkout@v5
@@ -124,6 +125,7 @@ jobs:
124125
flavor_directory: `./deploy/build/${flavor.directory}`,
125126
build_time: buildTime,
126127
image_tag: imageTag,
128+
image_ref: `${{ vars.DV_AWS_ECR_REGISTRY }}/${component.ecr_repository}:${imageTag}`,
127129
image_tag_branch_name: imageTagBranchName,
128130
formatted_build_args: formattedBuildArgs,
129131
};
@@ -240,7 +242,7 @@ jobs:
240242
# TODO: As soon as we only have a single tag, we can push the same image to multiple repositories: https://docs.docker.com/build/ci/github-actions/push-multi-registries/
241243
# This will be useful for the images which don't change between flavors, e.g. the backend images
242244
tags: |
243-
${{ vars.DV_AWS_ECR_REGISTRY }}/${{ matrix.component.ecr_repository }}:${{ matrix.component.image_tag }}
245+
${{ matrix.component.image_ref }}
244246
labels: |
245247
name=${{ matrix.component.ecr_repository }}
246248
version=${{ matrix.component.image_tag_branch_name }}
@@ -269,36 +271,112 @@ jobs:
269271
- name: Run Trivy vulnerability scanner
270272
uses: aquasecurity/[email protected]
271273
with:
272-
image-ref: ${{ vars.DV_AWS_ECR_REGISTRY }}/${{ matrix.component.ecr_repository }}:${{ matrix.component.image_tag }}
274+
image-ref: ${{ matrix.component.image_ref }}
273275
# Disable scanning the current directory (defaults to .)
274-
scan-ref: '/dev/null'
275-
format: 'table'
276-
exit-code: '1'
276+
scan-ref: "/dev/null"
277+
format: "table"
278+
exit-code: "1"
277279
ignore-unfixed: false
278-
vuln-type: 'os,library'
280+
vuln-type: "os,library"
279281
severity: ${{ steps.set_severity.outputs.severity }}
280-
continue-on-error: false
282+
continue-on-error: false
281283

282284
- name: Push image
283-
if: ${{ inputs.skip_push != true }}
284285
# Instead of the docker/build-push-action@v6 which will rebuild the image, just push it directly
285-
run: docker push ${{ vars.DV_AWS_ECR_REGISTRY }}/${{ matrix.component.ecr_repository }}:${{ matrix.component.image_tag }}
286+
run: docker push ${{ matrix.component.image_ref }}
287+
288+
- name: Log out from Amazon ECR
289+
shell: bash
290+
run: docker logout ${{ steps.login-ecr.outputs.registry }}
291+
292+
test-images:
293+
name: Test images of flavor ${{ matrix.flavor.id || 'default' }}
294+
needs: [get-flavors, build-flavors]
295+
strategy:
296+
fail-fast: false
297+
matrix:
298+
flavor: ${{ fromJson(needs.get-flavors.outputs.result).flavors }}
299+
runs-on: ${{ inputs.runs_on || 'ubuntu-22.04' }}
300+
steps:
301+
- name: Checkout repository
302+
uses: actions/checkout@v5
303+
with:
304+
ref: ${{ inputs.branch || github.sha }}
305+
token: ${{ secrets.CHECKOUT_TOKEN || github.event.repository.private == true && secrets.DATAVISYN_BOT_REPO_TOKEN || github.token }}
306+
307+
- name: Checkout github-workflows repository
308+
uses: actions/checkout@v5
309+
with:
310+
repository: datavisyn/github-workflows
311+
ref: ${{ env.WORKFLOW_BRANCH }}
312+
path: ./tmp/github-workflows
313+
314+
- name: Configure AWS Credentials
315+
uses: aws-actions/[email protected]
316+
with:
317+
role-to-assume: ${{ vars.DV_AWS_ECR_ROLE }}
318+
aws-region: ${{ vars.DV_AWS_REGION }}
319+
320+
- name: Login to Amazon ECR
321+
id: login-ecr
322+
uses: aws-actions/[email protected]
323+
324+
- name: Run test-images.sh hook
325+
shell: bash
326+
id: test-images
327+
run: |
328+
hooks_folder="$(realpath -m "./deploy/build/${{ matrix.flavor.directory }}/hooks")"
329+
test_images_hook="$hooks_folder/test-images.sh"
330+
test_images_report="$hooks_folder/test-images-report"
331+
332+
if [[ -f "$test_images_hook" ]]; then
333+
# Iterate through all components and store their image ref in an environment variable
334+
for component in $(jq -c '.components[]' <<< "$FLAVOR"); do
335+
name=$(jq -r '.ecr_repository' <<< "$component")
336+
image_ref=$(jq -r '.image_ref' <<< "$component")
337+
# Replace all non-alphanumeric characters with underscores and convert to uppercase
338+
name_upper=$(echo "${name//[^[:alnum:]]/_}" | tr '[:lower:]' '[:upper:]')
339+
echo "Setting environment variable IMAGE_${name_upper}=${image_ref}"
340+
export "IMAGE_${name_upper}=${image_ref}"
341+
done;
342+
343+
# Create report folder to avoid any downstream Docker volume issues
344+
# TODO: For some reason this doesn't work yet, i.e. if a docker-compose script mounts a volume here, nothing shows up...
345+
mkdir -p "$test_images_report"
346+
chmod 777 "$test_images_report"
347+
echo "test_images_report=${test_images_report}" >> "$GITHUB_OUTPUT"
348+
349+
echo "Run $test_images_hook"
350+
chmod +x "$test_images_hook"
351+
bash "$test_images_hook"
352+
else
353+
echo "No $test_images_hook found, skipping tests."
354+
fi
355+
env:
356+
FLAVOR: ${{ toJSON(matrix.flavor) }}
357+
358+
- name: Upload test-images-report
359+
uses: actions/upload-artifact@v4
360+
if: ${{ steps.test-images.outputs.test_images_report }}
361+
with:
362+
name: "test-images-report-${{ matrix.flavor.id || 'default' }}"
363+
path: ${{ steps.test-images.outputs.test_images_report }}
286364

287365
- name: Log out from Amazon ECR
288366
shell: bash
289367
run: docker logout ${{ steps.login-ecr.outputs.registry }}
290368

291369
retag-images:
292370
name: Retag images of flavor ${{ matrix.flavor.id || 'default' }}
293-
needs: [get-flavors, build-flavors]
371+
needs: [get-flavors, test-images]
294372
if: ${{ inputs.skip_push != true }}
295373
strategy:
296374
fail-fast: false
297375
matrix:
298376
flavor: ${{ fromJson(needs.get-flavors.outputs.result).flavors }}
299377
# Do not run this on self-hosted, as it is faster and shouldn't be blocking anything
300378
# runs-on: ${{ inputs.runs_on || 'ubuntu-22.04' }}
301-
runs-on: 'ubuntu-22.04'
379+
runs-on: "ubuntu-22.04"
302380
steps:
303381
- name: Checkout repository
304382
uses: actions/checkout@v5

0 commit comments

Comments
 (0)