-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-docker-build
100 lines (83 loc) · 1.9 KB
/
custom-docker-build
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
#!/bin/bash
###
# HOW THIS WORK
#
# 1. TRY TO PULL DOCKER IMAGES FROM GITLAB; IF exist THEN QUIT
# 2. WRITE DOCKERFILE
# 3. BUILD AND PUSH
#
# If you want to rebuild, delete images on gitlab
#
# Test
#
# ./scripts/custom-docker-build nodejs-8.2.1-dumb-1.2.0 "registry.gitlab.com/particle4dev/docker-node:nodejs-8.2.1-dumb-1.2.0"
###
. "$CI_PROJECT_DIR/scripts/utils"
set -e
IFS=$'\n\t'
function parse_arguments() {
read base
read base_version
printf -- "-f $Dockerfile " "$base"
while read image; do
read version
case "$image" in
dumb) dumb_version=$version ;;
*) exit 1;;
esac
done
templates -d=$dumb_version -b=$base -v=$base_version $CI_PROJECT_DIR/Dockerfile.template
}
# templates -u=123 -p=345 ./bin/utils/attachments.tmpl
function templates {
for i in "$@"
do
case $i in
-b=*|--base=*)
CUSTOM_IMAGE_NAME="${i#*=}"
shift
;;
-v=*|--version=*)
CUSTOM_IMAGE_VERSION="${i#*=}"
shift
;;
-d=*|--dumb=*)
DUMB_VERSION="${i#*=}"
shift
;;
*)
# unknown option
;;
esac
done
if ! fileExists $@; then
echo "not found file"
return 1
fi
while IFS='' read -r line || [[ -n "$line" ]]; do
echo $line | sed -e "s;%CUSTOM_IMAGE_NAME%;$CUSTOM_IMAGE_NAME;g" -e "s;%CUSTOM_IMAGE_VERSION%;$CUSTOM_IMAGE_VERSION;g" -e "s;%DUMB_VERSION%;$DUMB_VERSION;g" >> $Dockerfile
done < $@
}
function generate_command() {
buildimage_name=$1; shift;
printf -- "docker build "
echo $buildimage_name | tr '-' '\n' | parse_arguments
for i in "$@"
do
printf -- "%s " "$i"
done
printf -- ".\\n"
}
exitCode=$($CI_PROJECT_DIR/scripts/pull-docker-image $3 | tail -n 1)
if [ $exitCode -eq "0" ]; then
echo "FOUND $3"; exit 0;
fi
Dockerfile="$CI_PROJECT_DIR/Dockerfile"
# Empty file
if fileExists $Dockerfile; then
cp /dev/null $Dockerfile
fi
docker_command=$(generate_command $@)
echo "$3: executing $docker_command"
eval $docker_command
docker push $3