forked from opensearch-project/opensearch-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-image-multi-arch.sh
executable file
·120 lines (105 loc) · 3.46 KB
/
build-image-multi-arch.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
#!/bin/bash
# Copyright OpenSearch Contributors
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
# This script is to automate the docker multi arch image creation process
set -e
# Import libs
. ../../lib/shell/file_management.sh
# Variable
OLDIFS=$IFS
BUILDER_NUM=`date +%s`
BUILDER_NAME="multiarch_${BUILDER_NUM}"
DIR=""
function usage() {
echo ""
echo "This script is used to build the Docker image with multi architecture. It prepares the files required by the Dockerfile in a temporary directory, then builds and tags the Docker image."
echo "--------------------------------------------------------------------------"
echo "Usage: $0 [args]"
echo ""
echo "Required arguments:"
echo -e "-r REPO_NAME\tSpecify the image repo name such as 'ci-runner'"
echo -e "-v TAG_NAME\tSpecify the image tag name such as 'centos7-x64-arm64-jdkmulti-node10.24.1-cypress6.9.1-20211019'"
echo -e "-f DOCKERFILE\tSpecify the dockerfile full path, e.g. 'dockerfile/opensearch.al2.dockerfile'"
echo -e "-a ARCHITECTURE\tSpecify the architecture of the image, default to 'linux/amd64,linux/arm64', all options: 'linux/amd64,linux/arm64,linux/ppc64le'"
echo ""
echo "Optional arguments:"
echo -e "-h\t\tPrint this message."
echo "--------------------------------------------------------------------------"
}
function cleanup_docker_buildx() {
# Cleanup docker buildx
echo -e "\n* Cleanup docker buildx"
docker buildx use default
docker buildx rm $BUILDER_NAME > /dev/null 2>&1
}
function cleanup_all() {
cleanup_docker_buildx
File_Delete $DIR
}
while getopts ":hr:v:f:a:" arg; do
case $arg in
h)
usage
exit 1
;;
r)
REPO_NAME=$OPTARG
;;
v)
TAG_NAME=$OPTARG
;;
f)
DOCKERFILE=$OPTARG
;;
a)
ARCHITECTURE=$OPTARG
;;
:)
echo "-${OPTARG} requires an argument"
usage
exit 1
;;
?)
echo "Invalid option: -${arg}"
exit 1
;;
esac
done
# Validate the required parameters to present
if [ -z "$REPO_NAME" ] || [ -z "$TAG_NAME" ] || [ -z "$DOCKERFILE" ]; then
echo "You must specify '-r REPO_NAME', '-v TAG_NAME', '-f DOCKERFILE'"
usage
exit 1
else
echo "$TAG_NAME $DOCKERFILE"
fi
# Warning docker desktop
if (! docker buildx version)
then
echo -e "\n* You MUST have Docker Desktop to use buildx for multi-arch images."
exit 1
fi
# Architecture switch
if [ -z "$ARCHITECTURE" ]; then
ARCHITECTURE="linux/amd64,linux/arm64"
fi
# Prepare docker buildx
trap cleanup_all TERM INT EXIT
DIR=`Temp_Folder_Create`
echo "New workspace $DIR"
echo -e "\n* Prepare docker buildx"
docker buildx rm --all-inactive --force
docker buildx prune --all --force
docker buildx use default
docker buildx create --name ${BUILDER_NAME} --use
docker buildx inspect --bootstrap
# Check buildx status
echo -e "\n* Check buildx status"
docker buildx ls | grep $BUILDER_NAME
docker ps | grep $BUILDER_NAME
# Build multi-arch images
docker buildx build --platform "$ARCHITECTURE" --build-arg VERSION=${TAG_NAME} --build-arg BUILD_DATE=`date -u +%Y-%m-%dT%H:%M:%SZ` --build-arg NOTES=${NOTES} -t "opensearchstaging/${REPO_NAME}:${TAG_NAME}" -f "${DOCKERFILE}" --push .