diff --git a/ci-templates/industrial-ci-templates.yml b/ci-templates/industrial-ci-templates.yml index 4ab2d62ce61d01deef395e51e87b1a0648212902..a8ef12a5a652a3df3ceb2149b2e3d9cb73a9bc0c 100644 --- a/ci-templates/industrial-ci-templates.yml +++ b/ci-templates/industrial-ci-templates.yml @@ -22,7 +22,7 @@ DOCKER_PUSH_TAG: ${CI_COMMIT_REF_SLUG} DDEPLOY_YAML: ddeploy.yaml before_script: - - apk add --update python3 git py3-pip git-lfs + - apk add --update python3 git py3-pip git-lfs bash curl jq # forward the SSH authentication into the Docker executor - "which ssh-agent || ( apk update && apk add openssh-client )" - eval $(ssh-agent -s) @@ -42,6 +42,7 @@ - ddeploy --yaml ${DDEPLOY_YAML} # Get ID of image created by ddeploy - 'DOCKER_ID=$(docker images --format="{{.ID}}" | head -1)' + - curl -Ls https://git.code.tecnalia.com/tecnalia_robotics-public/gitlab_templates/raw/master/scripts/enforce_labels.bash | bash -s -- ${DOCKER_ID} # Tag and push with the branch or tag name. - echo "Pushing to ${DOCKER_PUSH_REGISTRY}/${DOCKER_PUSH_NAME}:${DOCKER_PUSH_TAG}" - docker tag ${DOCKER_ID} ${DOCKER_PUSH_REGISTRY}/${DOCKER_PUSH_NAME}:${DOCKER_PUSH_TAG} diff --git a/scripts/enforce_labels.bash b/scripts/enforce_labels.bash new file mode 100644 index 0000000000000000000000000000000000000000..f0ab26de089cee76c817dafde9c98025964bae9b --- /dev/null +++ b/scripts/enforce_labels.bash @@ -0,0 +1,35 @@ +#!/bin/bash +# From a given list of labels, check which of them are already defined in the provided docker image; if any of the labels +# is not defined, a new image with the same name is generated with the label(s) added. +# Labels already defined are not modified. + +if [ "$#" -ne 1 ]; then + echo "Usage: $0 IMAGE_NAME" + exit 1 +fi + +image_name="$1" + +# list of labels to be enforced +declare -A enforced_labels=( ["com.jfrog.artifactory.retention.maxCount"]="10" ["com.jfrog.artifactory.retention.maxDays"]="7") + +if ! docker inspect --type=image "$1" > /dev/null ; then + echo "Error: image does not exist" + exit 0 +fi + +# check if every label is defined +for label in "${!enforced_labels[@]}" ; do + if ! docker inspect -f "{{json .Config.Labels }}" "$image_name" | jq -e '.['\""$label"\"']' > /dev/null ; then + echo "Key not found" + echo "$label = ${enforced_labels[$label]}" + # add missing label to the arguments to be provided to `docker build` + label_args+=" --label $label=\"${enforced_labels[$label]}\"" + fi +done + +# generate new image +if [ -n "${label_args+x}" ]; then + run_command="echo \"FROM $image_name\" | docker build $label_args -t $image_name -" + eval "$run_command" +fi