-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.sh
executable file
·175 lines (162 loc) · 5.72 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
175
#!/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)>
-a|--extra-args <ExtraArgsForDockerBuildCommand>
--mark-latest
--push-images
--push-git-tags
--no-cache
--load
-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 " -a|--extra-args : Extra docker buidl args to pass to docker build command"
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 " --load : Make locally built images available to build"
echo " -h|--help : Prints this help menu"
exit 1
}
__processParams() {
extraDockerArgs=""
imageTags=""
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"
;;
-a|--extra-args) shift
[[ ! $1 ]] && __usage "extra args missing"
extraDockerArgs+=" $1"
;;
--mark-latest) imageTags+=" latest"
;;
--push-images) imgPush=yes
;;
--push-git-tags) tagPush=yes
;;
--no-cache) extraDockerArgs+=" --no-cache"
;;
--load) extraDockerArgs+=" --load"
;;
-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)")
}
__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
# shellcheck disable=SC2086
sudo docker buildx build --platform "$3" $5 $tagParams "$4"
__errCheck "$?" "Docker Build failed"
}
__validations() {
! [[ "$imgPush" =~ ^(yes|no)$ ]] && imgPush=no
! [[ "$tagPush" =~ ^(yes|no)$ ]] && tagPush=no
# Check for buildx env
if [[ "$(sudo docker buildx ls\
|grep -ce '.*default.*running.*linux/amd64' \
)" -lt "1" ]]; then
__errCheck "1" "Docker buildx env is not setup, please fix it"
fi
}
__checkSource() {
echo hi
# # Lets do git pull if push is enabled
# if [[ "$imgPush" == "yes" ]]; then
# # git checkout main >/dev/null 2>&1
# # __errCheck "$?" "Git checkout to main branch failed..."
# # git pull >/dev/null 2>&1
# # __errCheck "$?" "Git pull for main branch failed..."
# fi
}
__setupDocker() {
# Lets prepare docker image
if [[ "$imgPush" == "yes" ]]; then
echo "INFO: Logging in to Docker HUB... (Interactive Mode)"
sudo docker login 2>&1 | sed 's/^/INFO: DOCKER: /g'
__errCheck "$?" "Docker login failed..."
extraDockerArgs+=" --push"
fi
sudo docker buildx create --name builder >/dev/null 2>&1
sudo docker buildx use builder >/dev/null 2>&1
__errCheck "$?" "Could not use docker buildx default runner..."
}
__createGitTag() {
# Lets create git tag
echo "INFO: Creating local git tag: $tagName"
git tag -d "$tagName" >/dev/null 2>&1
git tag "$tagName" >/dev/null 2>&1
if [[ "$tagPush" == "yes" ]]; then
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: Docker image tags : $imageTags"
echo "INFO: Image tags push? : $imgPush"
echo "INFO: Git tags : $tagName"
echo "INFO: Git tags push? : $tagPush"
__dockerBuild "$image" "$imageTags" "$platforms" "$workDir" "$extraDockerArgs"
__createGitTag "$tagName"