forked from SumoLogic/sumologic-otel-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-push-multiplatform.sh
executable file
·122 lines (103 loc) · 3.39 KB
/
build-push-multiplatform.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
#!/bin/bash
set -eo pipefail
while ! docker buildx ls; do
echo "Cannot connect to docker daemon"
sleep 1
done
readonly DOCKER_BUILDX_LS_OUT=$(docker buildx ls <<-END
END
)
# check for arm support only if we try to build it
if echo ${PLATFORM} | grep -q arm && ! grep -q arm <<< ${DOCKER_BUILDX_LS_OUT}; then
echo "Your Buildx seems to lack ARM architecture support"
echo "${DOCKER_BUILDX_LS_OUT}"
exit 1
fi
if [[ -z "${BUILD_TAG}" ]]; then
echo "No BUILD_TAG passed in, using 'latest' as default"
BUILD_TAG="latest"
fi
if [[ -z "${LATEST_TAG_FIPS_SUFFIX}" ]]; then
LATEST_TAG_FIPS_SUFFIX=""
fi
if [[ -z "${REPO_URL}" ]]; then
echo "No REPO_URL passed in"
exit 1
fi
if [[ -z "${PLATFORM}" ]]; then
echo "No PLATFORM passed in"
exit 1
fi
PUSH=""
if [[ $# -eq 1 ]] && [[ "${1}" == "--push" ]]; then
PUSH="true"
fi
# build_push builds a container image for a designated platform and then pushes
# it to container repository specified by REPO_URL variable.
#
# PLATFORM variable is the platform for which to build the image as accepted
# by docker buildx build command.
# e.g.linux/amd64, linux/arm64, linux/ppc64le, linux/s390x, linux/386,
# linux/arm/v7, linux/arm/v6
function build_push() {
local BUILD_ARCH
set -x
case "${PLATFORM}" in
"linux/amd64"|"linux_amd64")
readonly BUILD_ARCH="amd64"
PLATFORM="linux/amd64"
;;
"linux/arm64"|"linux_arm64")
readonly BUILD_ARCH="arm64"
PLATFORM="linux/arm64"
;;
# Can't really enable it for now because:
# !shopify/sarama@v1.29.0/gssapi_kerberos.go:62:10: constant 4294967295 overflows int
# ref: https://github.com/SumoLogic/sumologic-otel-collector/runs/2805247906
# If we'd like to support arm then we'd need to provide a patch in sarama.
#
# "linux/arm/v7"|"linux_arm_v7"|"linux/arm"|"linux_arm")
# readonly BUILD_ARCH="arm"
# PLATFORM="linux/arm/v7"
# ;;
*)
echo "Unsupported platform ${PLATFORM}"
exit 1
;;
esac
local TAG
readonly TAG="${REPO_URL}:${BUILD_TAG}-${BUILD_ARCH}"
local LATEST_TAG
readonly LATEST_TAG="${REPO_URL}:latest${LATEST_TAG_FIPS_SUFFIX}-${BUILD_ARCH}"
if [[ "${PUSH}" == true ]]; then
echo "Building tag: ${TAG}"
docker buildx build \
--push \
--file "${DOCKERFILE}" \
--build-arg BUILD_TAG="${BUILD_TAG}" \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--platform="${PLATFORM}" \
--tag "${TAG}" \
.
# This is needed on CI because the above build command does not include
# --load flag, which is forbidded to be used together with --push, hence
# the docker pull.
docker pull "${TAG}"
echo "Tagging: ${LATEST_TAG}"
docker tag "${TAG}" "${LATEST_TAG}"
docker push "${LATEST_TAG}"
else
echo "Building tag: latest${LATEST_TAG_FIPS_SUFFIX}"
# load flag is needed so that docker loads this image
# for subsequent steps on github actions
docker buildx build \
--file "${DOCKERFILE}" \
--build-arg BUILD_TAG="latest${LATEST_TAG_FIPS_SUFFIX}" \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--platform="${PLATFORM}" \
--load \
--tag "${REPO_URL}:latest${LATEST_TAG_FIPS_SUFFIX}" \
.
fi
}
build_push "${PUSH}"