diff --git a/pipelines/template-build/template-build.yaml b/pipelines/template-build/template-build.yaml index 47aa9aa864..31cc368994 100644 --- a/pipelines/template-build/template-build.yaml +++ b/pipelines/template-build/template-build.yaml @@ -224,6 +224,15 @@ spec: value: $(tasks.build-container.results.IMAGE_URL) - name: IMAGE_DIGEST value: $(tasks.build-container.results.IMAGE_DIGEST) + - name: apply-tags + runAfter: + - build-container + taskRef: + name: apply-tags + version: "0.1" + params: + - name: IMAGE + value: $(tasks.build-container.results.IMAGE_URL) finally: - name: show-sbom diff --git a/task/apply-tags/0.1/README.md b/task/apply-tags/0.1/README.md new file mode 100644 index 0000000000..4029e924f5 --- /dev/null +++ b/task/apply-tags/0.1/README.md @@ -0,0 +1,13 @@ +# apply-tags task + +Apply-tags task will apply additional tags to the specified IMAGE. These additional tags can be provided via the ADDITIONAL_TAGS array parameter or they can also be provided in the image label "konflux.additional-tags". If you specify more than one additional tag in the label, they must be separated by a comma, e.g: + +``` +LABEL konflux.additional-tags="tag1, tag2" +``` + +## Parameters +|name|description|default value|required| +|---|---|---|---| +|IMAGE|Reference of image that was pushed to registry in the buildah task.||true| +|ADDITIONAL_TAGS|Additional tags that will be applied to the image in the registry.|[]|false| diff --git a/task/apply-tags/0.1/apply-tags.yaml b/task/apply-tags/0.1/apply-tags.yaml new file mode 100644 index 0000000000..cdf30d7591 --- /dev/null +++ b/task/apply-tags/0.1/apply-tags.yaml @@ -0,0 +1,63 @@ +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + labels: + app.kubernetes.io/version: "0.1" + annotations: + tekton.dev/pipelines.minVersion: "0.12.1" + tekton.dev/tags: "appstudio, hacbs" + name: apply-tags +spec: + description: >- + Applies additional tags to the built image. + params: + - name: IMAGE + description: Reference of image that was pushed to registry in the buildah task. + type: string + - name: ADDITIONAL_TAGS + description: Additional tags that will be applied to the image in the registry. + type: array + default: [] + steps: + - name: apply-additional-tags-from-parameter + image: registry.access.redhat.com/ubi9/skopeo:9.4-6@sha256:c4d70dec3eb0a0c831490192145ea25431fe04d1cf307f8d61e2d87adb41e7e3 + args: + - $(params.ADDITIONAL_TAGS) + env: + - name: IMAGE + value: $(params.IMAGE) + script: | + #!/bin/bash + + if [ "$#" -ne 0 ]; then + IMAGE_WITHOUT_TAG=$(echo "$IMAGE" | sed 's/:[^:]*$//') + for tag in "$@"; do + echo "Applying tag $tag" + skopeo copy docker://$IMAGE docker://$IMAGE_WITHOUT_TAG:$tag + done + else + echo "No additional tags parameter specified" + fi + + - name: apply-additional-tags-from-image-label + image: registry.access.redhat.com/ubi9/skopeo:9.4-6@sha256:c4d70dec3eb0a0c831490192145ea25431fe04d1cf307f8d61e2d87adb41e7e3 + env: + - name: IMAGE + value: $(params.IMAGE) + script: | + #!/bin/bash + + ADDITIONAL_TAGS_FROM_IMAGE_LABEL=$(skopeo inspect --format '{{ index .Labels "konflux.additional-tags" }}' docker://$IMAGE) + + if [ -n "${ADDITIONAL_TAGS_FROM_IMAGE_LABEL}" ]; then + IFS=', ' read -r -a tags_array <<< "$ADDITIONAL_TAGS_FROM_IMAGE_LABEL" + + IMAGE_WITHOUT_TAG=$(echo "$IMAGE" | sed 's/:[^:]*$//') + for tag in "${tags_array[@]}" + do + echo "Applying tag $tag" + skopeo copy docker://$IMAGE docker://$IMAGE_WITHOUT_TAG:$tag + done + else + echo "No additional tags specified in the image labels" + fi diff --git a/task/apply-tags/OWNERS b/task/apply-tags/OWNERS new file mode 100644 index 0000000000..68cb38c859 --- /dev/null +++ b/task/apply-tags/OWNERS @@ -0,0 +1 @@ +Konflux Build team \ No newline at end of file