-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild-image.sh
More file actions
executable file
·201 lines (177 loc) · 4.96 KB
/
Copy pathbuild-image.sh
File metadata and controls
executable file
·201 lines (177 loc) · 4.96 KB
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
#!/bin/bash
# Variables to set
IMAGE_BASE=komacke/hamclock
HC_UID=1199
HC_GID=1199
ON_TAG=false
# Don't set anything past here
TAG=$(git describe --exact-match --tags 2>/dev/null)
if [ $? -ne 0 ]; then
echo "NOTE: Not currently on a tag. Using 'latest'."
TAG=latest
GIT_VERSION=$(git rev-parse --short HEAD)
ON_TAG=false
else
GIT_VERSION=$TAG
ON_TAG=true
fi
IMAGE=$IMAGE_BASE:$TAG
CONTAINER=${IMAGE_BASE##*/}
# Get our directory locations in figured out
HERE="$(cd "$(dirname "$0")" && pwd)"
THIS="$(basename "$0")"
cd $HERE
usage() {
cat<<EOF
$THIS:
Builds the latest docker image based on the current git branch. It will figure out
if on a git tag and will use that for the docker image tag. Otherwise falls back
to 'latest'.
-m: multi-platform image buld for: linux/amd64 linux/arm64 linux/arm/v7
- argument is ignored when run with -c
- remember to setup a buildx container:
docker buildx create --name ohb --driver docker-container --use
docker buildx inspect --bootstrap
-n: add --no-cache to build
-s: set hamclock size to one of the following: 800x480 1600x960 2400x1440 3200x1920
EOF
exit 0
}
main() {
RETVAL=0
MULTI_PLATFORM=false
NOCACHE=false
if [[ "$@" =~ --help ]]; then
usage
fi
while getopts ":hmns:" opt; do
case $opt in
h)
usage
;;
m)
MULTI_PLATFORM=true
;;
n)
NOCACHE=true
;;
s)
HC_SIZE="$OPTARG"
;;
\?) # Handle invalid options
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:) # Handle options requiring an argument but none provided
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
do_all
build_done_message
}
do_all() {
warn_image_tag
warn_local_edits
build_image
}
warn_image_tag() {
if [ $TAG != latest ]; then
if [ $MULTI_PLATFORM == true ]; then
docker manifest inspect $IMAGE >/dev/null
if [ $? -eq 0 ]; then
echo
echo "WARNING: the multiplatform docker image for '$IMAGE' already exists in Docker Hub. Please"
echo " remove it if you want to rebuild."
exit 2
fi
elif docker image list --format '{{.Repository}}:{{.Tag}}' | grep -qs $IMAGE; then
echo
echo "WARNING: the docker image for '$IMAGE' already exists. Please remove it if you want to rebuild."
exit 2
fi
fi
}
warn_local_edits() {
# check if there are local edits in the filesystem. We probably don't want to push them
git diff-index --quiet HEAD --
LOCAL_EDITS=$?
if [ $LOCAL_EDITS -ne 0 ]; then
if [ $MULTI_PLATFORM == true ]; then
echo
echo "ERROR: There are local edits. stash or reset them before pushing"
echo " images to Docker Hub."
exit 3
else
echo
echo "WARNING: there are local edits. If you didn't intend that, stash"
echo " them and build again."
fi
fi
return $LOCAL_EDITS
}
poke_version_cpp() {
if [ "$1" == restore ]; then
git restore ESPHamClock/version.cpp
elif [ $ON_TAG == true ]; then
# update version in HC source
HC_TAG=${GIT_VERSION%.*}
HC_TAG=${HC_TAG#v}
HC_TAG=${HC_TAG#V}
sed -i 's/\(hc_version = "\)[^"]\+\(".*\)/\1'$HC_TAG'\2/' ESPHamClock/version.cpp
fi
}
build_image() {
if [ $NOCACHE == true ]; then
NOCACHE_ARG="--no-cache"
fi
if [ -n "$HC_SIZE" ]; then
SET_HC_SIZE="--build-arg HC_SIZE=${HC_SIZE}"
fi
# Build the image
echo
echo "Building image for '$IMAGE_BASE:$TAG'"
pushd "$HERE/.." >/dev/null
echo $GIT_VERSION > git.version
poke_version_cpp
if [ $MULTI_PLATFORM == true ]; then
docker buildx build \
$NOCACHE_ARG \
--pull \
--build-arg HC_UID=$HC_UID \
--build-arg HC_GID=$HC_GID \
$SET_HC_SIZE \
-t $IMAGE \
-f docker/Dockerfile \
--platform linux/amd64,linux/arm64 \
--push \
.
RETVAL=$?
else
docker build \
$NOCACHE_ARG \
--pull \
--build-arg HC_UID=$HC_UID \
--build-arg HC_GID=$HC_GID \
$SET_HC_SIZE \
-t $IMAGE \
-f docker/Dockerfile \
.
RETVAL=$?
fi
poke_version_cpp restore
rm -f git.version
popd >/dev/null
}
build_done_message() {
if [ $RETVAL -eq 0 ]; then
# basic info
echo
echo "Completed building '$IMAGE'."
else
echo "build failed with error: $RETVAL"
fi
}
main "$@"
exit $RETVAL