Skip to content

Instantly share code, notes, and snippets.

@vkuznet
Created May 29, 2024 14:40
Show Gist options
  • Save vkuznet/9d4956c9afe158757897ea8316b05085 to your computer and use it in GitHub Desktop.
Save vkuznet/9d4956c9afe158757897ea8316b05085 to your computer and use it in GitHub Desktop.
Docker wrapper script for WMCore CI/CD
#!/bin/bash
# Author: Valentin Kuznetsov <[email protected]>
# helper script to handle docker actions
if [ $# -ne 4 ]; then
echo "Usage: docker.sh <action> <service> <tag> <registry> "
exit 1;
fi
# helper function to match the pattern X.Y.Z where X, Y, and Z are numbers
function match_tag {
local tag=$1
if [[ $tag =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
return 0
elif [[ $tag =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
return 0
else
return 1
fi
}
# make input variable assignments
action=$1
service=$2
tag=$3
registry=$4
rurl=$registry/cmsweb/$service
# check if action is supported
case "$action" in
"build"|"push"|"remove"|"delete")
;;
*)
echo "action: '$action' is not supported"
exit 1
;;
esac
# determine suffix of image tag
suffix=""
if match_tag "$tag"; then
suffix="-stable"
fi
# check build/push actions and build the image(s) if it will be required
if [ "$action" == "build" ] || [ "$action" == "push" ]; then
echo "check if image with service=$service and tag=$tag exist ..."
image_exist=`docker images | grep $tag | grep $service`
if [ -z "$image_exist" ]; then
echo "action: docker build --build-arg TAG=${tag} --tag ${rurl}:${tag}"
docker build --build-arg TAG=${tag} --tag ${rurl}:${tag} .
if [ "$suffix" == "-stable" ]; then
echo "action: docker build --build-arg TAG=${tag} --tag ${rurl}:${tag}${suffix}"
docker build --build-arg TAG=${tag} --tag ${rurl}:${tag}${suffix} .
fi
fi
docker images | grep $tag | grep $service
fi
if [ "$action" == "push" ]; then
echo "action: docker push ${rurl}:${tag}"
docker push ${rurl}:${tag}
fi
if [ "$action" == "remove" ] || [ "$action" == "delete" ]; then
echo "action: docker image rm -f ${rurl}:${tag}"
docker rmi ${rurl}:${tag}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment