-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsr-dockerbuild
executable file
·296 lines (242 loc) · 8.2 KB
/
sr-dockerbuild
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env bash
#include the configuration file
source $(dirname $0)/s-config.sh
# this function takes two parameters:
# @param exitcode - it's a last operation exit code 0 -succees
# @param msg - this is optional message to be displayed on error
exit_on_error(){
local exitcode=$1
local msg=$2
if [ $exitcode != 0 ]; then
echo "Error" $msg
exit $exitcode
fi
}
debug() {
if [ ! -z $DEBUG_FILE ]; then
echo "$@" >> $DEBUG_FILE
fi
}
message(){
echo $@
debug $@
}
check_if_all_commited(){
debug "check_if_all_commited" $@
local no_of_changed_files=$(git status --porcelain | wc -l)
if [ $no_of_changed_files == 0 ]; then
debug "check_if_all_commited: no_of_changed_files=$no_of_changed_files"
return 0 # 0 means true
else
changed_files=$(git status --porcelain )
echo "$changed_files" | head -n 20
debug "check_if_all_commited: no_of_changed_files=$no_of_changed_files"
return 1 # 1 means false
fi
}
function print_help(){
echo "Usage: sr-dockerbuild [OPTIONS]"
echo " sr-dockerbuild [--help]"
echo
echo "sr-dockerbuild runs docker compilation process and publish results to a docker registry."
echo
echo "Options:"
echo
echo " -u|--user user name for docker repo [optional]"
echo " -p|--password password for accesing user privte account in docker repo [optional]"
echo " -i|--image override image name set by env DOCKER_IMAGE_NAME [optional]"
echo " -pu|--push push created image to docker repository [optional]"
echo " -nocache option -nocache for docker build commnad [optional]"
echo " -r|--remove remove local image after docker creation, make sense to use with --push [optional]"
echo
echo "Example:"
echo " ./sr-dockerbuild compile docker image locally, no push to remote server"
echo
echo "Example:"
echo " ./sr-dockerbuild --push compile docker image locally, no push to remote server"
echo
exit;
}
# login to docker
while [[ $# > 0 ]]
do
key="$1"
DOCKER_BUILD_NO_CACHE=" "
case $key in
-h|--help)
print_help;
;;
-u|--user)
DOCKER_USER="$2"
shift # past argument
;;
-p|--password)
DOCKER_PASSWORD="$2"
shift # past argument
;;
-i|--image)
DOCKER_IMAGE_NAME="$2"
shift # past argument
;;
-pu|--push)
DOCKER_ENABLE_PUSH="1"
;;
-nocache)
DOCKER_BUILD_NO_CACHE="-nocache"
;;
-r|--remove)
DOCKER_ENABLE_REMOVE="1"
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
if check_if_all_commited; then
message "All files are commited in the current branch.";
else
message "Please commit changes before you can generate changelog"
exit 1
fi
if [ "$DOCKER_USER" != "" ] && [ "$DOCKER_PASSWORD" != "" ]; then
echo "Performing docker login -u "$DOCKER_USER"...."
docker login -u "$DOCKER_USER" -p "$DOCKER_PASSWORD" -e "[email protected]" $DOCKER_REPOSITORY
exit_on_error $? "Can't login to the remote repo"
fi
#name of the current branch
#CURRNET_BRANCH=$(git rev-parse --abbrev-ref HEAD)
CURRNET_BRANCH=$(git status | sed -n '/On branch /s///p')
CURRENT_GIT_NAME=$(git remote -v | head -n1 | awk '{print $2}' | sed 's/.*\///' | sed 's/\.git//')
exit_on_error $? "Cannot obtain git name"
# id of the current commit
CURRNET_COMMIT=$(git rev-parse HEAD)
exit_on_error $? "Cannot obtain head commit hash"
# name of the current tag if it points onto the current commit
CURRNET_TAG=$(git tag --points-at $CURRNET_COMMIT)
#list of all commits in the current brach excluding any othre commits not related to the brnach
LIST_OF_ALL_COMMITS_IN_BRANCH=$(git rev-list --all --not $(git rev-list --all ^$CURRNET_BRANCH))
CURRENT_TICKET=""
TICKET_REGX='.*\/([A-Z]+\-[0-9]+)\-.*'
if [[ $CURRNET_BRANCH =~ $TICKET_REGX ]]
then
CURRENT_TICKET=${BASH_REMATCH[1]}
fi
if [[ $CURRNET_TAG != "" ]]
then
TAG_LABEL=$CURRNET_TAG
else
TAG_LABEL=""
fi
TAG_KEYS=()
## if we have git tag
if [[ $TAG_LABEL != "" ]]
then
TAG_KEYS+=(":$TAG_LABEL")
fi
DOCKER_BUILD_NAME=""
if [[ $CURRNET_BRANCH == "master" ]]
then
DOCKER_BUILD_NAME="$DOCKER_REPOSITORY/$DOCKER_IMAGE_NAME"
else
if [[ $CURRNET_BRANCH == "develop" ]]
then
DOCKER_BUILD_NAME="$DOCKER_REPOSITORY/$DOCKER_IMAGE_NAME:develop"
elif [[ $CURRENT_TICKET != "" ]]
then
DOCKER_BUILD_NAME="$DOCKER_REPOSITORY/$DOCKER_IMAGE_NAME:$CURRENT_TICKET"
else
DOCKER_BUILD_NAME="$DOCKER_REPOSITORY/$DOCKER_IMAGE_NAME:$CURRNET_COMMIT"
fi
fi
printf '%s\n' "${TAG_KEYS[@]}"
## run build and tag if needed
DOCKER_BUILD_COMMAND="docker build $DOCKER_BUILD_NO_CACHE -t $DOCKER_BUILD_NAME ."
echo $DOCKER_BUILD_COMMAND
eval $DOCKER_BUILD_COMMAND
exit_on_error $? "The build commannd failed: $DOCKER_BUILD_COMMAND"
## get the docker image sha
#DOCKER_BUILD_ID=$(docker inspect --format='{{.Id}}' $DOCKER_BUILD_NAME);
#exit_on_error $? "Obtaining docker build id failed"
DOCKER_BUILD_ID="ID-${CURRNET_COMMIT:0:10}"
echo "$DOCKER_BUILD_ID" > dockerimage.sha
TAG_KEYS+=(":$DOCKER_BUILD_ID")
echo "$DOCKER_BUILD_NAME" >> dockerimage.sha
## tag with other tags
for i in "${TAG_KEYS[@]}"
do
:
DOCKER_TAG_COMMAND="docker tag $DOCKER_BUILD_NAME $DOCKER_REPOSITORY/$DOCKER_IMAGE_NAME$i"
echo $DOCKER_TAG_COMMAND
eval $DOCKER_TAG_COMMAND
exit_on_error $? "Tagging failed: $DOCKER_TAG_COMMAND"
if [[ ${#i} == 65 ]]
then
# cut the 64-hex docker id
CUT_DOCKER_IMAGE_NAME="$DOCKER_REPOSITORY/$DOCKER_IMAGE_NAME$i"
echo ${CUT_DOCKER_IMAGE_NAME::${#CUT_DOCKER_IMAGE_NAME}-52} >> dockerimage.sha
else
echo "$DOCKER_REPOSITORY/$DOCKER_IMAGE_NAME$i" >> dockerimage.sha
fi
done
## push to docker repo
if [[ "$DOCKER_ENABLE_PUSH" == "1" ]]
then
echo "-------------------"
echo "Pushing images to the repository"
echo "-------------------"
DOCKER_PUSH_COMMAND="docker push $DOCKER_BUILD_NAME"
echo $DOCKER_PUSH_COMMAND
eval $DOCKER_PUSH_COMMAND
exit_on_error $? "Pushing image failed: $DOCKER_PUSH_COMMAND"
## push tagged images to docker repo
for i in "${TAG_KEYS[@]}"
do
:
DOCKER_PUSH_COMMAND="docker push $DOCKER_REPOSITORY/$DOCKER_IMAGE_NAME$i"
echo $DOCKER_PUSH_COMMAND
eval $DOCKER_PUSH_COMMAND
exit_on_error $? "Pushing image failed: $DOCKER_PUSH_COMMAND"
done
fi
## remove image
if [[ "$DOCKER_ENABLE_REMOVE" == "1" ]]
then
DOCKER_RM_COMMAND="docker rmi $DOCKER_BUILD_NAME"
echo $DOCKER_RM_COMMAND
eval $DOCKER_RM_COMMAND
exit_on_error $? "Removing local image failed: $DOCKER_RM_COMMAND"
## remove tagged images from local disk
for i in "${TAG_KEYS[@]}"
do
:
DOCKER_RM_COMMAND="docker rmi $DOCKER_REPOSITORY/$DOCKER_IMAGE_NAME$i"
echo $DOCKER_RM_COMMAND
eval $DOCKER_RM_COMMAND
exit_on_error $? "Removing local image failed: $DOCKER_RM_COMMAND"
done
fi
echo "=========================================================="
echo "Image tags"
cat dockerimage.sha
echo "=========================================================="
echo "Report on compilation of $CURRENT_GIT_NAME" > dockerimage.report.txt
COUNT=0;
while read p; do
if [ "$COUNT" == "0" ]; then
echo >> dockerimage.report.txt
echo "Compiled project into artefacts:" >> dockerimage.report.txt
echo >> dockerimage.report.txt
else
echo "Artefact $p" >> dockerimage.report.txt
echo " To get image:" >> dockerimage.report.txt
echo " docker pull $p " >> dockerimage.report.txt
echo " To run instruction howto:" >> dockerimage.report.txt
echo " docker run --rm $p howto" >> dockerimage.report.txt
echo "" >> dockerimage.report.txt
fi
(( COUNT++ )) ;
done < dockerimage.sha
#COMPOSE_FILE=docker-compose.yml.template
#BASE_COMPOSE_IMAGE="docker.onedata.org/onedata-documentation:"
#sed -re "s/(image: )${BASE_COMPOSE_IMAGE//\//\\/}/\1${DOCKER_BUILD_NAME//\//\\/}/" $COMPOSE_FILE > docker-compose.yml