-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathbuild-push-multiplatform.sh
executable file
·175 lines (151 loc) · 5.44 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
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
#!/bin/bash
set -eo pipefail
if echo "${PLATFORM}" | grep -v windows; then
DOCKER_BUILDX_LS_OUT=$(docker buildx ls <<-END
END
)
readonly DOCKER_BUILDX_LS_OUT
# 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
fi
if [[ -z "${BUILD_TAG}" ]]; then
echo "No BUILD_TAG passed in, using 'latest' as default"
BUILD_TAG="latest"
fi
if [[ -z "${BUILD_TYPE_SUFFIX}" ]]; then
BUILD_TYPE_SUFFIX=""
fi
if [[ -z "${REPO_URL}" ]]; then
echo "No REPO_URL passed in"
exit 1
fi
if [[ -n "${BASE_IMAGE_TAG}" ]]; then
BASE_IMAGE_TAG="-${BASE_IMAGE_TAG}"
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
local BASE_IMAGE_TAG_SUFFIX
set -x
case "${PLATFORM}" in
"linux/amd64"|"linux_amd64")
readonly BUILD_ARCH="amd64"
readonly BUILD_PLATFORM="linux"
PLATFORM="linux/amd64"
;;
"linux/arm64"|"linux_arm64")
readonly BUILD_ARCH="arm64"
readonly BUILD_PLATFORM="linux"
PLATFORM="linux/arm64"
;;
"windows/amd64"|"windows_amd64")
readonly BUILD_ARCH="amd64"
readonly BASE_IMAGE_TAG_SUFFIX="windows"
PLATFORM="windows/amd64"
;;
"windows/amd64/ltsc2022"|"windows_amd64_ltsc2022")
readonly BUILD_ARCH="amd64"
readonly BUILD_PLATFORM="windows"
readonly BASE_IMAGE_TAG_SUFFIX="-ltsc2022"
readonly BASE_IMAGE_TAG="ltsc2022"
PLATFORM="windows/amd64"
;;
"windows/amd64/ltsc2019"|"windows_amd64_ltsc2019")
readonly BUILD_ARCH="amd64"
readonly BUILD_PLATFORM="windows"
readonly BASE_IMAGE_TAG_SUFFIX="-ltsc2019"
readonly BASE_IMAGE_TAG="ltsc2019"
PLATFORM="windows/amd64"
;;
# 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_TYPE_SUFFIX}-${BUILD_PLATFORM}-${BUILD_ARCH}${BASE_IMAGE_TAG_SUFFIX}"
local LATEST_TAG
readonly LATEST_TAG="${REPO_URL}:latest${BUILD_TYPE_SUFFIX}-${BUILD_PLATFORM}-${BUILD_ARCH}${BASE_IMAGE_TAG_SUFFIX}"
# --provenance=false for docker buildx ensures that we create manifest instead of manifest list
if [[ "${PUSH}" == true ]]; then
echo "Building tags: ${TAG}, ${LATEST_TAG}"
if [[ "${BUILD_PLATFORM}" == "windows" ]]; then
docker build \
--file "${DOCKERFILE}" \
--build-arg BUILD_TAG="${BUILD_TAG}" \
--build-arg BASE_IMAGE_TAG="${BASE_IMAGE_TAG}" \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--platform="${PLATFORM}" \
--tag "${LATEST_TAG}" \
.
docker tag "${LATEST_TAG}" "${TAG}"
docker push "${LATEST_TAG}"
docker push "${TAG}"
else
docker buildx build \
--push \
--file "${DOCKERFILE}" \
--build-arg BUILD_TAG="${BUILD_TAG}" \
--build-arg BASE_IMAGE_TAG="${BASE_IMAGE_TAG}" \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--platform="${PLATFORM}" \
--tag "${LATEST_TAG}" \
--tag "${TAG}" \
--provenance=false \
.
fi
else
echo "Building tag: latest${BUILD_TYPE_SUFFIX}"
if [[ "${BUILD_PLATFORM}" == "windows" ]]; then
docker build \
--file "${DOCKERFILE}" \
--build-arg BUILD_TAG="latest${BUILD_TYPE_SUFFIX}" \
--build-arg BASE_IMAGE_TAG="${BASE_IMAGE_TAG}" \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--platform="${PLATFORM}" \
--tag "${REPO_URL}:latest${BUILD_TYPE_SUFFIX}" \
.
else
# 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${BUILD_TYPE_SUFFIX}" \
--build-arg BASE_IMAGE_TAG="${BASE_IMAGE_TAG}" \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--platform="${PLATFORM}" \
--load \
--tag "${REPO_URL}:latest${BUILD_TYPE_SUFFIX}" \
--provenance=false \
.
fi
fi
}
build_push "${PUSH}"