-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbake-docker.sh
executable file
·159 lines (146 loc) · 4.91 KB
/
bake-docker.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
#!/usr/bin/env bash
# Copyright (c) 2024 Carnegie Mellon University
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# This script overview:
# - Builds the necessary images for cabot on multiple platforms and pushes them to cmucal.
# - Since push requires cmucal permissions, it is usually handled by an Action script.
# - If you want to debug by modifying the base image, perform a local build (-l).
# - Set up a local registry server, build, and overwrite the cmucal image tag.
# - It is also advisable to specify the platform (-p) in this case.
function help {
echo "Usage: $0 [-i] [-l] [-b <base_name>] [-P <platform>]"
echo ""
echo "-h show this help"
echo "-b <base_name> bake with base_name"
echo "-i image build for debug - shorthand for -l and -P with host platform"
echo "-l build using local registry"
echo "-P <platform> specify platform"
echo " build linux/arm64 and linux/amd64 if not specified"
echo "-t <tag>[,<tag>] tag or tags"
}
platform=
base_name=cabot-base
service=app-server
local=0
tags=
while getopts "hb:ilP:t:" arg; do
case $arg in
h)
help
exit
;;
b)
base_name=${OPTARG}
;;
i)
if [[ $(uname -m) = "x86_64" ]]; then
platform="linux/amd64"
elif [[ $(uname -m) = "aarch64" ]]; then
platform="linux/arm64"
fi
local=1
;;
l)
local=1
;;
P)
platform=${OPTARG}
;;
t)
tags=${OPTARG}
;;
esac
done
shift $((OPTIND-1))
if [[ -z $base_name ]]; then
help
exit 1
fi
if [[ -z $(docker network ls | grep "registry-network") ]]; then
docker network create registry-network
fi
if [[ $local -eq 1 ]]; then
export REGISTRY=registry:5000
# setup local docker registry for multiplatform support
if [[ -z $(docker ps -f "name=registry" -q) ]]; then
docker run -d \
--rm \
--name registry \
--network registry-network \
-p 127.0.0.1:9092:5000 \
registry:2.7
fi
else
export REGISTRY=cmucal
fi
# setup multiplatform builder
# docker buildx rm mybuilder
if [[ -z $(docker buildx ls | grep "mybuilder\*") ]]; then
echo "mybuilder is not selected"
if [[ -z $(docker buildx ls | grep mybuilder) ]]; then
echo "creating mybuilder"
docker buildx create --use --name mybuilder --driver docker-container \
--config buildkitd.toml \
--driver-opt network=registry-network # option to make the builder access to the registry on the localhost
else
echo "use mybuilder"
docker buildx use mybuilder
fi
fi
# tag option
tag_option=
if [[ -z $tags ]]; then
tags="latest,$(git rev-parse --abbrev-ref HEAD | tr '/' '-')"
fi
if [[ "$tags" == *,* ]]; then
tag_option="--set=${service}.tags=${REGISTRY}/cabot-${service}:{$tags}"
else
tag_option="--set=${service}.tags=${REGISTRY}/cabot-${service}:$tags"
fi
# platform option
platform_option=
if [[ -n $platform ]]; then
platform_option="--set=*.platform=\"$platform\""
fi
# bake
com="docker buildx bake -f docker-compose.yaml $platform_option $tag_option $service"
export BASE_IMAGE=$base_name
echo $com
eval $com
if [[ $? -ne 0 ]]; then
echo "failed to build image"
exit 1
fi
# reset buildx builder to default
docker buildx use default
# copy images from local registry
# this can override image tag
if [[ $local -eq 1 ]]; then
tags=($(eval "$com --print" 2> /dev/null | jq -r '.target[].tags[] | split("/")[-1]' | jq --raw-input | jq -r --slurp 'join(" ")'))
for tag in "${tags[@]}"; do
echo "Pulling tag ($tag) from $REGISTRY (platform=$(uname -m))"
com="docker pull localhost:9092/$tag"
echo $com
eval $com
com="docker image tag localhost:9092/$tag cmucal/$tag"
echo $com
eval $com
done
fi