Skip to content

Commit 5b115d1

Browse files
committed
feat: optimize skip_push and skip_retag
1 parent 6e27a4a commit 5b115d1

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,37 @@ jobs:
102102
103103
const builds = process.env.BUILDS ? process.env.BUILDS.split(',') : Object.keys(config.build);
104104
const push_to = process.env.PUSH_TO ? process.env.PUSH_TO.split(',') : Object.keys(config.push || {});
105+
const skip_push = process.env.SKIP_PUSH === 'true';
105106
106107
const flavors = builds.map(id => [id, config.build[id]]).filter(([id, flavor]) => flavor.skip !== true).map(([id, flavor]) => {
108+
const hooksDirectory = path.join('./deploy/build', flavor.directory, 'hooks');
109+
const testImagesHookScript = path.join(hooksDirectory, 'test-images.sh');
110+
const testImagesHookReport = path.join(hooksDirectory, 'test-images-report');
111+
const testImageEnabled = fs.existsSync(testImagesHookScript);
112+
107113
return {
108114
...flavor,
109115
id,
116+
skip_retag: skip_push,
110117
// Add metadata to the flavor object (will be used as matrix input)
111118
build_time: buildTime,
112119
image_tag: imageTag,
113120
image_tag_branch_name: imageTagBranchName,
114121
ecr_respositories: flavor.components.map(component => component.ecr_repository),
122+
test_images: {
123+
enabled: testImageEnabled,
124+
script_path: testImagesHookScript,
125+
report_path: testImagesHookReport,
126+
},
115127
components: flavor.components.map(component => {
116128
// Format build arguments as build-args string
117129
const formattedBuildArgs = component.build_args ?
118130
Object.entries(component.build_args).map(([key, value]) => `${key}=${value}`).join('\n') : '';
119131
120132
return {
121133
...component,
134+
// We can skip the push only if we don't want to test the image below
135+
skip_push: skip_push && !testImageEnabled,
122136
// Add metadata to the component object (will be used as matrix input),
123137
flavor,
124138
flavor_id: id,
@@ -133,16 +147,18 @@ jobs:
133147
};
134148
});
135149
136-
const flattenedComponents = flavors.flatMap(flavor => flavor.components);
137-
138150
const result = {
139151
flavors,
140-
components: flattenedComponents,
152+
flavorsToTest: flavors.filter(flavor => flavor.test_images.enabled),
153+
flavorsToRetag: flavors.filter(flavor => flavor.skip_retag !== true),
154+
componentsToBuild: flavors.flatMap(flavor => flavor.components),
141155
push_to: push_to.join(','),
156+
skip_push,
142157
};
143158
console.log(result);
144159
return result;
145160
env:
161+
SKIP_PUSH: ${{ inputs.skip_push }}
146162
BUILDS: ${{ inputs.builds }}
147163
PUSH_TO: ${{ inputs.push_to }}
148164

@@ -152,7 +168,7 @@ jobs:
152168
strategy:
153169
fail-fast: ${{ inputs.fail_fast }}
154170
matrix:
155-
component: ${{ fromJson(needs.get-flavors.outputs.result).components }}
171+
component: ${{ fromJson(needs.get-flavors.outputs.result).componentsToBuild }}
156172
runs-on: ${{ vars.BUILD_DOCKER_RUNS_ON_OVERRIDE || vars.RUNS_ON_OVERRIDE || inputs.runs_on || 'ubuntu-22.04' }}
157173
steps:
158174
- name: View flavor and component
@@ -282,6 +298,7 @@ jobs:
282298
continue-on-error: false
283299

284300
- name: Push image
301+
if: ${{ matrix.component.skip_push != true }}
285302
# Instead of the docker/build-push-action@v6 which will rebuild the image, just push it directly
286303
run: docker push ${{ matrix.component.image_ref }}
287304

@@ -295,7 +312,7 @@ jobs:
295312
strategy:
296313
fail-fast: false
297314
matrix:
298-
flavor: ${{ fromJson(needs.get-flavors.outputs.result).flavors }}
315+
flavor: ${{ fromJson(needs.get-flavors.outputs.result).flavorsToTest }}
299316
runs-on: ${{ inputs.runs_on || 'ubuntu-22.04' }}
300317
steps:
301318
- name: Checkout repository
@@ -304,13 +321,6 @@ jobs:
304321
ref: ${{ inputs.branch || github.sha }}
305322
token: ${{ secrets.CHECKOUT_TOKEN || github.event.repository.private == true && secrets.DATAVISYN_BOT_REPO_TOKEN || github.token }}
306323

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-
314324
- name: Configure AWS Credentials
315325
uses: aws-actions/[email protected]
316326
with:
@@ -325,13 +335,12 @@ jobs:
325335
shell: bash
326336
id: test-images
327337
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"
338+
test_images_hook=$(jq -r '.test_images.script_path' <<< "$FLAVOR")
339+
test_images_report=$(jq -r '.test_images.report_path' <<< "$FLAVOR")
331340
332341
if [[ -f "$test_images_hook" ]]; then
333342
# Iterate through all components and store their image ref in an environment variable
334-
for component in $(jq -c '.components[]' <<< "$FLAVOR"); do
343+
for component in $(jq -c '.componentsToBuild[]' <<< "$FLAVOR"); do
335344
name=$(jq -r '.ecr_repository' <<< "$component")
336345
image_ref=$(jq -r '.image_ref' <<< "$component")
337346
# Replace all non-alphanumeric characters with underscores and convert to uppercase
@@ -369,11 +378,10 @@ jobs:
369378
retag-images:
370379
name: Retag images of flavor ${{ matrix.flavor.id || 'default' }}
371380
needs: [get-flavors, test-images]
372-
if: ${{ inputs.skip_push != true }}
373381
strategy:
374382
fail-fast: false
375383
matrix:
376-
flavor: ${{ fromJson(needs.get-flavors.outputs.result).flavors }}
384+
flavor: ${{ fromJson(needs.get-flavors.outputs.result).flavorsToRetag }}
377385
# Do not run this on self-hosted, as it is faster and shouldn't be blocking anything
378386
# runs-on: ${{ inputs.runs_on || 'ubuntu-22.04' }}
379387
runs-on: "ubuntu-22.04"
@@ -429,7 +437,7 @@ jobs:
429437
push-to-repositories:
430438
name: Push images to push targets
431439
# if? When should we do this? Always? Only for certain branches? If so, how should we define that, in the config.json?
432-
if: ${{ inputs.skip_push != true && fromJson(needs.get-flavors.outputs.result).push_to != '' }}
440+
if: ${{ fromJson(needs.get-flavors.outputs.result).skip_push != true && fromJson(needs.get-flavors.outputs.result).push_to != '' }}
433441
needs: [retag-images, get-flavors]
434442
uses: datavisyn/github-workflows/.github/workflows/build-docker-artifacts-trigger-push.yml@main
435443
secrets: inherit

0 commit comments

Comments
 (0)