Skip to content

Commit

Permalink
Merge pull request #12005 from vkuznet/fix-issue-11762
Browse files Browse the repository at this point in the history
New docker wrapper script to manage docker image actions
  • Loading branch information
anpicci authored Aug 21, 2024
2 parents 01e0747 + 24584be commit a40bb12
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/docker_images_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ jobs:
cat Dockerfile
echo "Sleeping 5min to ensure that PyPi packages are available..."
sleep 300
docker build --build-arg TAG=${PYPI_TAG} --tag ${CERN_REGISTRY}/cmsweb/${SERVICE_NAME}:${PYPI_TAG} .
echo "Image build process completed. Current images are:"
docker images
echo "Now push new image to the CERN registry"
docker push ${CERN_REGISTRY}/cmsweb/${SERVICE_NAME}:${PYPI_TAG}
curl -ksLO https://raw.githubusercontent.com/dmwm/WMCore/master/bin/docker.sh
chmod +x docker.sh
echo "build new image"
./docker.sh build ${SERVICE_NAME} ${PYPI_TAG} ${CERN_REGISTRY}
echo "push new image"
./docker.sh push ${SERVICE_NAME} ${PYPI_TAG} ${CERN_REGISTRY}
81 changes: 81 additions & 0 deletions bin/docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/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> "
echo "Supported actions: build, push"
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")
;;
*)
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" ]; then
echo "action: docker build --build-arg TAG=${tag} --tag ${rurl}:${tag}"
docker build --build-arg TAG=${tag} --tag ${rurl}:${tag} .
docker images | grep $tag | grep $service

if [ -z "$suffix" ]; then
echo "Building stable image for tag=$tag is not appropriate as tag is not matched X.Y.Z or X.Y.Z.P pattern"
exit 0
fi

echo "action: docker build --build-arg TAG=${tag} --tag ${rurl}:${tag}${suffix}"
docker build --build-arg TAG=${tag} --tag ${rurl}:${tag}${suffix} .
docker images | grep ${tag}${suffix} | grep $service
fi

if [ "$action" == "push" ]; then
image_exist=`docker images | grep $tag | grep $service | grep -v ${tag}${suffix}`
image_exist_stable=`docker images | grep ${tag}${suffix} | grep $service`
echo $image_exist
echo $image_exist_stable

if [ -z "$image_exist" ]; then
echo "Images ${rurl}:${tag} not found"
exit 1
fi
echo "action: docker push ${rurl}:${tag}"
docker push ${rurl}:${tag}

if [ -n "$suffix" ] && [ -z "$image_exist_stable" ]; then
echo "Images ${rurl}:${tag}${suffix} not found"
exit 2
fi
echo "action: docker push ${rurl}:${tag}${suffix}"
docker push ${rurl}:${tag}${suffix}
fi

0 comments on commit a40bb12

Please sign in to comment.