-
Notifications
You must be signed in to change notification settings - Fork 17
/
build.sh
executable file
·174 lines (161 loc) · 5.57 KB
/
build.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
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
#!/bin/bash
# Author: Satish Gaikwad <[email protected]>
## Functions
__usage() {
if [[ "$1" != "" ]]; then
echo "ERR: $1 "
fi
echo "Usage: $0
-i|--image-name <DockerImageName>
-p|--platforms <PlatformsList>
-w|--work-dir <WorkDirPath>
-t|--git-tag <TagName(Required)>
--mark-latest
--push-images
--push-git-tags
--no-cache
-h|--help"
echo "Description:"
echo " -i|--image-name : Name of the docker image."
echo " e.g. satishweb/imagename. (Def: current directory name)"
echo " -p|--platforms : list of platforms to build for."
echo " (Def: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6)"
echo " -w|--work-dir : Docker buildx command work dir path"
echo " -t|--git-tag : Name for git tag to create (Required)"
echo " --mark-latest : Marks the latest image to given tag"
echo " --push-images : Enables pushing of docker images to docker hub"
echo " --push-git-tags : Enabled push of git tags to git remote origin"
echo " --no-cache : Avoid use of docker build cache"
echo " --docker-file : Path to the docker file from working dir"
echo " --docker-args : Additional docker command arguments to supply"
echo " -h|--help : Prints this help menu"
exit 1
}
__processParams() {
extraDockerArgs=""
imageTags=""
dockerFile=Dockerfile
while [ "$1" != "" ]; do
case $1 in
-i|--image-name) shift
[[ ! $1 ]] && __usage "Image name is missing"
image="$1"
;;
-p|--platforms) shift
[[ ! $1 ]] && __usage "Platforms list missing"
platforms="$1"
;;
-w|--work-dir) shift
[[ ! $1 ]] && __usage "Work dir path missing"
workDir="$1"
;;
-t|--git-tag) shift
[[ ! $1 ]] && __usage "Git tag name missing"
tagName="$(echo $1\
|sed -e 's/^[ \t]*//;s/[ \t]*$//;s/ /-/g'\
|sed $'s/[^[:print:]\t]//g')"
imageTags+=" $tagName"
;;
--mark-latest) latestImage=yes
imageTags+=" latest"
;;
--push-images) imgPush=yes
;;
--push-git-tags) tagPush=yes
;;
--no-cache) extraDockerArgs+=" --no-cache"
;;
--docker-file) shift
dockerFile="$1"
;;
--docker-args) shift
dockerArgs=$1
extraDockerArgs+=" dockerArgs"
;;
-h|--help) __usage
;;
* ) __usage "Missing or incorrect parameters"
esac
shift
done
[[ ! $platforms ]] && \
platforms="linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6"
[[ ! $imgPush ]] && imgPush=no
[[ ! $tagPush ]] && tagPush=no
[[ ! $tagName ]] && __usage "Work dir path missing"
[[ ! $workDir ]] && workDir=$(pwd)
[[ ! $image ]] && image=$(basename $(pwd))
OSF=$(echo ${dockerFile}|cut -d '.' -f 2)
}
__errCheck(){
# $1 = errocode
# $2 = msg
[[ "$1" != "0" ]] && echo "ERR: $2" && exit $1
}
__dockerBuild(){
# $1 = image name e.g. "satishweb/imagename"
# $2 = image tags e.g. "latest 1.1.1"
# $3 = platforms e.g. "linux/amd64,linux/arm64"
# $4 = work dir path
# $5 = Extra args for docker buildx command
tagParams=""
for i in $2; do tagParams+=" -t $1:$i"; done
docker buildx build --platform "$3" $5 $tagParams -f $4/$dockerFile .
__errCheck "$?" "Docker Build failed"
}
__validations() {
! [[ "$imgPush" =~ ^(yes|no)$ ]] && imgPush=no
! [[ "$tagPush" =~ ^(yes|no)$ ]] && tagPush=no
# Check for buildx env
if [[ "$(docker buildx ls\
|grep -e ".*default.*running.*linux/amd64"\
|wc -l\
)" -lt "1" ]]; then
__errCheck "1" "Docker buildx env is not setup, please fix it"
fi
}
__checkSource() {
# Lets do git pull if tag push is enabled
if [[ "$tagPush" == "yes" ]]; then
git checkout master >/dev/null 2>&1
__errCheck "$?" "Git checkout to master branch failed..."
git pull >/dev/null 2>&1
__errCheck "$?" "Git pull for master branch failed..."
fi
}
__setupDocker() {
# Lets prepare docker image
if [[ "$imgPush" == "yes" ]]; then
echo "INFO: Logging in to Docker HUB... (Interactive Mode)"
docker login 2>&1 | sed 's/^/INFO: DOCKER: /g'
__errCheck "$?" "Docker login failed..."
extraDockerArgs+=" --push"
fi
}
__createGitTag() {
# Lets create git tag
if [[ "$tagPush" == "yes" ]]; then
echo "INFO: Creating local git tag: $tagName"
git tag -d $tagName >/dev/null 2>&1
git tag $tagName >/dev/null 2>&1
echo "INFO: Pushing git tag to remote: $tagName"
git push --delete origin $tagName >/dev/null 2>&1
git push -f origin $tagName >/dev/null 2>&1
fi
}
## Main
__processParams $@
__validations
__checkSource
__setupDocker
# Lets identify current unbound version and setup image tags
echo "INFO: Building Docker Images (may take a while)"
echo "INFO: Docker image : $image"
echo "INFO: Platforms : $platforms"
echo "INFO: DockerFile : $dockerFile"
echo "INFO: Docker image tags : $imageTags $OSF"
echo "INFO: Image tags push? : $imgPush"
echo "INFO: Git tags : $tagName"
echo "INFO: Git tags push? : $tagPush"
__dockerBuild $image "$imageTags $OSF" "$platforms" "$workDir" "$extraDockerArgs"
__createGitTag $tagName