-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New docker wrapper script to manage docker actions on stable images, …
…and its implementation in the CI/CD template
- Loading branch information
Showing
2 changed files
with
87 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |