This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
docker-image-release.sh
executable file
·130 lines (113 loc) · 3.24 KB
/
docker-image-release.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env bash
#
# script filename
SCRIPT_NAME=$(basename $BASH_SOURCE)
echo ""
echo ""
# -------
# display help
displayHelp() {
echo "Usage ${SCRIPT_NAME} [-h] [-g tag] [-i tag] [-d]"
echo ""
echo " prepare a docker image and push it to the dockerhub repo "
echo " in repository scicatproject/backend with tag specified by -i option"
echo ""
echo " arguments:"
echo " -h : show this help and exit"
echo " -g tag : git tag or commit we would like to use to create the docker image."
echo " If not specified, the program will used the latest commit in the current git branch"
echo " -i tag : tag used to tag the docker image"
echo " If not specified, the docker imag ewill be tagged with "
echo " the git branch name followed by git tag, separted by dash"
echo " Example:"
echo " - master-5d5f42af1ca6816a13b6db60b4778388dc4bf431"
echo " -d : dry run. Check and print arguments and commands but does not take any action"
echo ""
}
# internal variables
#
# git tag
gitTag=""
# git repo
gitRepo=""
# docker image tag
dockerTag=""
# docker repository
dockerRepo=scicatproject/backend
# dry run
dryRun="n"
#
# manage options
while getopts "hdg:i:" option; do
case $option in
h) # display help
displayHelp
exit 0
;;
g) # user specified the git tag to use
gitTag=${OPTARG}
;;
i) # user specified the docke rimage tag to use
dockerTag=${OPTARG}
;;
d) # dry run
dryRun="y"
;;
\?) # invalid option
echo "Invalid Option"
echo
displayHelp
exit 1
;;
esac
done
# code repository and branch
# these are not needed anymore as we assume that this script will only be run from within the repo
# after it has been cloned from github.
#
# I leave them here as a reference as they change as of 2021/11/09
# githut repository = https://github.com/scicatproject/frontend.git
# available branches
# - master,
gitRepo="$(git branch --show-current)"
# check if the user provided a tag or not
if [ "-${gitTag}-" == "--" ]; then
# not git tag from the user
# define git tag as <branch>-<latest commit>
gitTag="$(git rev-parse HEAD)"
else
# check out on the specific commit or tag
git checkout ${gitTag}
fi
# docker image tag
if [ "${dockerTag}-" == "--" ]; then
dockerTag="${gitRepo}-${gitTag}"
fi
dockerImage="${dockerRepo}:${dockerTag}"
#
# gives some feedback to the user
echo "Git repo : ${gitRepo}"
echo "Git commit tag : ${gitTag}"
echo "Docker image tag : ${dockerTag}"
echo "Docker image : ${dockerImage}"
echo ""
if [ "-${dryRun}-" == "-y-" ]; then
echo "Dry Run. Exiting"
exit 0
fi
#
# create docker image
# if it is already present, remove old image
if [[ "$(docker images -q ${dockerImage} 2> /dev/null)" != "" ]]; then
echo "Image already present. Removing it and recreating it"
docker rmi ${dockerImage}
echo ""
fi
echo "Creating image"
docker build -t ${dockerImage} -f CI/ESS/Dockerfile .
echo ""
# push image on docker hub repository
docker push ${dockerImage}
echo ""
echo "Done"
echo ""