forked from wso2/product-microgateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-ubuntu-multi-platform-images.sh
executable file
·136 lines (120 loc) · 4.88 KB
/
build-ubuntu-multi-platform-images.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
#!/bin/bash
# --------------------------------------------------------------------
# Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------
set -e
BUILDX_VERSION=v0.7.0
PLATFORMS="linux/amd64,linux/arm64"
HOST_OS=linux # for other platforms check corresponding release: https://github.com/docker/buildx/releases/
if [[ $(uname) == 'Darwin' ]]; then
HOST_OS='darwin'
fi
HOST_ARCH=amd64
if [[ $(uname -m) == *"arm"* ]]; then
HOST_ARCH="arm64"
fi
echo "Reading product version..."
if [ -z $2 ]; then
MVN_PROJECT_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
else
MVN_PROJECT_VERSION=$2
fi
echo "Product version: $MVN_PROJECT_VERSION"
ADAPTER_IMAGE="wso2/choreo-connect-adapter:${MVN_PROJECT_VERSION}-ubuntu"
ENFORCER_IMAGE="wso2/choreo-connect-enforcer:${MVN_PROJECT_VERSION}-ubuntu"
ROUTER_IMAGE="wso2/choreo-connect-router:${MVN_PROJECT_VERSION}-ubuntu"
GREEN='\033[0;32m' # Green colour
BOLD="\033[1m"
NC='\033[0m' # No colour
subcommand=$1
# Download the buildx plugin for docker
if [ ! -f docker-ubuntu-build/target/buildx ]; then
echo "BuildX plugin not found"
rm -rf docker-ubuntu-build
mkdir -p docker-ubuntu-build/target
echo "Downloading the buildx plugin..."
wget https://github.com/docker/buildx/releases/download/${BUILDX_VERSION}/buildx-${BUILDX_VERSION}.${HOST_OS}-${HOST_ARCH} \
-nv -O docker-ubuntu-build/target/buildx
fi
chmod a+x docker-ubuntu-build/target/buildx
mkdir -p ~/.docker/cli-plugins
cp docker-ubuntu-build/target/buildx ~/.docker/cli-plugins
if [[ $HOST_ARCH == "amd64" ]]; then
echo "Installing QEMU user mode emulation binaries"
docker run --rm --privileged multiarch/qemu-user-static:6.1.0-8 --reset -p yes
fi
# create build instance and use it
docker buildx create --name cc-builder --use 2> /dev/null || docker buildx use cc-builder
# build_images builds multi arch images of choreo connect components
BUILT_IMAGES_ARR=()
build_images() {
platforms=$1 # eg: linux/amd64,linux/arm64
image_suffix=$2 # suffix such as platform
action=$3 # --push or --load
# replace slashes
image_suffix=$(echo "$image_suffix" | tr "/" "-")
### adapter
adapter_img="${ADAPTER_IMAGE}${image_suffix}"
printf "${GREEN}${BOLD}Building Adapter image ($adapter_img)...${NC}\n"
docker buildx build -t "$adapter_img" --platform "$platforms" "${action}" \
-f adapter/src/main/resources/Dockerfile.ubuntu \
"adapter/target/docker/wso2/choreo-connect-adapter/${MVN_PROJECT_VERSION}/build/"
BUILT_IMAGES_ARR+=("$adapter_img")
### enforcer
enforcer_img="${ENFORCER_IMAGE}${image_suffix}"
printf "${GREEN}${BOLD}Building Enforcer image ($enforcer_img)...${NC}\n"
docker buildx build -t "$enforcer_img" --platform "$platforms" "${action}" \
-f enforcer-parent/enforcer/src/main/resources/Dockerfile.ubuntu \
"enforcer-parent/enforcer/target/docker/wso2/choreo-connect-enforcer/${MVN_PROJECT_VERSION}/build"
BUILT_IMAGES_ARR+=("$enforcer_img")
### router
router_img="${ROUTER_IMAGE}${image_suffix}"
printf "${GREEN}${BOLD}Building Router image ($router_img)...${NC}\n"
docker buildx build -t "$router_img" --platform "$platforms" "${action}" \
-f router/src/main/resources/Dockerfile.ubuntu \
"router/target/docker/wso2/choreo-connect-router/${MVN_PROJECT_VERSION}/build"
BUILT_IMAGES_ARR+=("$router_img")
}
if [ "$subcommand" = "push" ]; then
# Build and push images
echo "Building and pushing images to registry..."
build_images "$PLATFORMS" "" "--push"
elif [ "$subcommand" = "all" ]; then
# Build images for all platforms
echo "Building images for all platforms..."
for platform in $(echo $PLATFORMS | tr "," " "); do
echo "Build all component images for platform: $platform"
build_images "$platform" "-${platform}" "--load"
done
else
# Build images for the platform of host machine
echo "Building images matched for the platform of the host machine..."
platform="linux/amd64"
if [[ $HOST_ARCH == "arm64" ]]; then
echo "Building images for platform: ARM"
platform="linux/arm64"
else
echo "Building images for platform: AMD"
fi
build_images "$platform" "-${platform}" "--load"
fi
docker buildx rm cc-builder
# log built images
printf "${BOLD}Built images${NC}\n"
for img in "${BUILT_IMAGES_ARR[@]}"
do
echo $img
done
printf "${GREEN}${BOLD}Build success${NC}\n"