Skip to content

Commit 8ef6a02

Browse files
authored
feat: add image_tag_suffix support (#258)
* feat: add image_tag_suffix support * Get rid of additional image_tag * Add suffix and prefix support * Use mp/image_tag_suffix * Revert
1 parent 5a27f0f commit 8ef6a02

File tree

4 files changed

+41
-25
lines changed

4 files changed

+41
-25
lines changed

.github/workflows/build-docker-artifacts-config.schema.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
"type": "string",
4141
"description": "ECR repository to push the image to"
4242
},
43+
"image_tag_suffix": {
44+
"type": "string",
45+
"description": "Suffix to append to the image tag"
46+
},
47+
"image_tag_prefix": {
48+
"type": "string",
49+
"description": "Prefix to prepend to the image tag"
50+
},
4351
"scan_high_severity": {
4452
"type": "boolean",
4553
"default": true,
@@ -95,6 +103,14 @@
95103
"type": "object",
96104
"additionalProperties": false,
97105
"properties": {
106+
"image_tag_suffix": {
107+
"type": "string",
108+
"description": "Suffix to append to the image tag"
109+
},
110+
"image_tag_prefix": {
111+
"type": "string",
112+
"description": "Prefix to prepend to the image tag"
113+
},
98114
"source_repository": {
99115
"type": "string",
100116
"description": "Internal ECR repository name"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ jobs:
6767
const filteredRepositories = customer.repositories.filter(repo => !repo.skip);
6868
6969
// Comma separated list of source images, incl. registry, repository and tag
70-
const sourceImages = filteredRepositories.map(repo => `${process.env.SOURCE_ECR_REGISTRY}/${repo.source_repository}:${repo.source_image_tag || process.env.IMAGE_TAG}`).join(',');
71-
const destinationImages = filteredRepositories.map(repo => `${customer.registry}/${repo.target_repository}:${repo.target_image_tag || process.env.IMAGE_TAG}`).join(',');
70+
const sourceImages = filteredRepositories.map(repo => `${process.env.SOURCE_ECR_REGISTRY}/${repo.source_repository}:${repo.image_tag_prefix || ''}${repo.source_image_tag || process.env.IMAGE_TAG}${repo.image_tag_suffix || ''}`).join(',');
71+
const destinationImages = filteredRepositories.map(repo => `${customer.registry}/${repo.target_repository}:${repo.image_tag_prefix || ''}${repo.target_image_tag || process.env.IMAGE_TAG}${repo.image_tag_suffix || ''}`).join(',');
7272
7373
const result = {
7474
customer,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ jobs:
7575
7676
const customers = process.env.PUSH_TO ? process.env.PUSH_TO.split(',') : Object.keys(config.push || {});
7777
78-
const imageTagBranchName = "${{ github.ref }}".replace('refs/heads/', '').replace('refs/tags/', '').replace(/[^a-zA-Z0-9._-]/g, '-');
78+
const imageTagAfterRetag = "${{ github.ref }}".replace('refs/heads/', '').replace('refs/tags/', '').replace(/[^a-zA-Z0-9._-]/g, '-');
7979
8080
const result = customers.map((c) => ({
8181
repository: process.env.REPOSITORY,
8282
customer: c,
8383
customer_json: JSON.stringify(config.push[c]),
84-
image_tag: imageTagBranchName,
84+
image_tag: imageTagAfterRetag,
8585
}));
8686
console.log(result);
8787
return result;

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ jobs:
9797
const config = require('./deploy/build/config.json');
9898
9999
const buildTime = new Date().toISOString().replace(/:/g, '').replace(/\..+/, 'Z');
100-
const imageTagBranchName = "${{ github.ref }}".replace('refs/heads/', '').replace('refs/tags/', '').replace(/[^a-zA-Z0-9._-]/g, '-');
101-
const imageTag = `tagged-${imageTagBranchName}-${buildTime}`;
102100
103101
const builds = process.env.BUILDS ? process.env.BUILDS.split(',') : Object.keys(config.build);
104102
const push_to = process.env.PUSH_TO ? process.env.PUSH_TO.split(',') : Object.keys(config.push || {});
@@ -116,9 +114,6 @@ jobs:
116114
skip_retag: skip_push,
117115
// Add metadata to the flavor object (will be used as matrix input)
118116
build_time: buildTime,
119-
image_tag: imageTag,
120-
image_tag_branch_name: imageTagBranchName,
121-
ecr_repositories: flavor.components.map(component => component.ecr_repository),
122117
test_images: {
123118
enabled: testImageEnabled,
124119
script_path: testImagesHookScript,
@@ -129,6 +124,9 @@ jobs:
129124
const formattedBuildArgs = component.build_args ?
130125
Object.entries(component.build_args).map(([key, value]) => `${key}=${value}`).join('\n') : '';
131126
127+
const imageTagAfterRetag = (component.image_tag_prefix || '') + "${{ github.ref }}".replace('refs/heads/', '').replace('refs/tags/', '').replace(/[^a-zA-Z0-9._-]/g, '-') + (component.image_tag_suffix || '');
128+
const imageTagBeforeRetag = `tagged-${imageTagAfterRetag}-${buildTime}`;
129+
132130
return {
133131
...component,
134132
// We can skip the push only if we don't want to test the image below
@@ -138,9 +136,9 @@ jobs:
138136
flavor_id: id,
139137
flavor_directory: `./deploy/build/${flavor.directory}`,
140138
build_time: buildTime,
141-
image_tag: imageTag,
142-
image_ref: `${{ vars.DV_AWS_ECR_REGISTRY }}/${component.ecr_repository}:${imageTag}`,
143-
image_tag_branch_name: imageTagBranchName,
139+
image_tag_before_retag: imageTagBeforeRetag,
140+
image_ref: `${{ vars.DV_AWS_ECR_REGISTRY }}/${component.ecr_repository}:${imageTagBeforeRetag}`,
141+
image_tag_after_retag: imageTagAfterRetag,
144142
formatted_build_args: formattedBuildArgs,
145143
};
146144
}),
@@ -163,7 +161,7 @@ jobs:
163161
PUSH_TO: ${{ inputs.push_to }}
164162

165163
build-flavors:
166-
name: Build ${{ matrix.component.directory }} of ${{ matrix.component.flavor.directory }} (${{ matrix.component.ecr_repository }}:${{ matrix.component.image_tag }})
164+
name: Build ${{ matrix.component.directory }} of ${{ matrix.component.flavor.directory }} (${{ matrix.component.ecr_repository }}:${{ matrix.component.image_tag_before_retag }})
167165
needs: get-flavors
168166
strategy:
169167
fail-fast: ${{ inputs.fail_fast }}
@@ -247,6 +245,8 @@ jobs:
247245
# Disable provenance as it creates weird multi-arch images: https://github.com/docker/build-push-action/issues/755
248246
provenance: false
249247
# Disable the cache to avoid outdated (base) images
248+
# TODO: are we sure we want this? We might benefit a lot from caching! But also, our base images like datavisyn/base/python:main are updated frequently and have no version tag...
249+
# maybe no-cache-filters works? Or we do this on our self-hosted runners: https://docs.docker.com/build/ci/github-actions/cache/#local-cache
250250
no-cache: true
251251
build-args: |
252252
GIT_BRANCH=${{ steps.get-branch.outputs.branch }}
@@ -265,12 +265,12 @@ jobs:
265265
${{ matrix.component.image_ref }}
266266
labels: |
267267
name=${{ matrix.component.ecr_repository }}
268-
version=${{ matrix.component.image_tag_branch_name }}
268+
version=${{ matrix.component.image_tag_after_retag }}
269269
org.opencontainers.image.description=Image for ${{ matrix.component.ecr_repository }}
270270
org.opencontainers.image.source=${{ github.event.repository.html_url }}
271271
org.opencontainers.image.url=${{ github.event.repository.html_url }}
272272
org.opencontainers.image.title=${{ matrix.component.ecr_repository }}
273-
org.opencontainers.image.version=${{ matrix.component.image_tag_branch_name }}
273+
org.opencontainers.image.version=${{ matrix.component.image_tag_after_retag }}
274274
org.opencontainers.image.created=${{ matrix.component.build_time }}
275275
org.opencontainers.image.revision=${{ github.sha }}
276276
env:
@@ -426,19 +426,19 @@ jobs:
426426
- name: Retag images
427427
shell: bash
428428
run: |
429-
image_tag="${{ matrix.flavor.image_tag }}"
430-
image_tag_branch_name="${{ matrix.flavor.image_tag_branch_name }}"
429+
for component in $(jq -c '.components[]' <<< "$FLAVOR"); do
430+
repository_name=$(jq -r '.ecr_repository' <<< "$component")
431+
image_tag_before_retag=$(jq -r '.image_tag_before_retag' <<< "$component")
432+
image_tag_after_retag=$(jq -r '.image_tag_after_retag' <<< "$component")
431433
432-
echo "image_tag=$image_tag"
433-
echo "image_tag_branch_name=$image_tag_branch_name"
434+
echo "Processing repository: $repository_name, image_tag_before_retag: $image_tag_before_retag, image_tag_after_retag: $image_tag_after_retag"
434435
435-
for repository_name in $(jq -r '.ecr_repositories[]' <<< "$FLAVOR"); do
436-
IMAGE_META=$(aws ecr describe-images --repository-name "$repository_name" --image-ids imageTag="$image_tag" --output json | jq --arg var "${image_tag_branch_name}" '.imageDetails[0].imageTags | index( $var )')
437-
if [[ -z "${IMAGE_META}" || ${IMAGE_META} == "null" ]]; then
438-
MANIFEST=$(aws ecr batch-get-image --repository-name "$repository_name" --image-ids imageTag="$image_tag" --output json | jq --raw-output --join-output '.images[0].imageManifest')
439-
aws ecr put-image --repository-name "$repository_name" --image-tag "$image_tag_branch_name" --image-manifest "$MANIFEST"
436+
IMAGE_META=$(aws ecr describe-images --repository-name "$repository_name" --image-ids imageTag="$image_tag_before_retag" --output json | jq --arg var "${image_tag_after_retag}" '.imageDetails[0].imageTags | index($var)')
437+
if [[ -z "${IMAGE_META}" || "${IMAGE_META}" == "null" ]]; then
438+
MANIFEST=$(aws ecr batch-get-image --repository-name "$repository_name" --image-ids imageTag="$image_tag_before_retag" --output json | jq --raw-output --join-output '.images[0].imageManifest')
439+
aws ecr put-image --repository-name "$repository_name" --image-tag "$image_tag_after_retag" --image-manifest "$MANIFEST"
440440
else
441-
echo "image already tagged!"
441+
echo "Image already tagged for repository: $repository_name!"
442442
fi
443443
done;
444444
env:

0 commit comments

Comments
 (0)