Skip to content
Snippets Groups Projects
Select Git revision
  • fbd81cdd0af0f13b4f4fb71ae5fb3c58807f123d
  • master default
  • rtde
  • tmp-gpg-key-workaround-2
  • tmp-gpg-key-workaround
  • 68-git-lfs-error-in-ddeploy-job
  • split-build-and-test
  • 66-jazzy-support
  • 62-deploy-jobs-do-not-pull-files-from-lfs-manual-lfs-pull
  • 62-deploy-jobs-do-not-pull-files-from-lfs-custom-docker-image
  • py3-without-industrial-ci-test
  • 58-add-yolo-pip-package-support
  • 55-collision-between-test-jobs-due-to-dds-autodiscovery-ros2
  • 52-ddeploy-job-failing-when-enforcing-labels-alt-quick-dind-test
  • 48-python3_syntax
  • 46-default-docker-image-name-too-long
  • 45-double-pipeline-triggered-if-merge-request-has-melodic-branch-name
  • 40-repo-is-ros-testing
  • test-badges
  • test-lfs-concept
  • add-packages
21 results

enforce_labels.bash

Blame
  • enforce_labels.bash 1.20 KiB
    #!/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