forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_ci.sh
executable file
·156 lines (127 loc) · 4.85 KB
/
docker_ci.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
#!/bin/bash
# Do not ever set -x here, it is a security hazard as it will place the credentials below in the
# CI logs.
set -e
function is_windows() {
[[ "$(uname -s)" == *NT* ]]
}
ENVOY_DOCKER_IMAGE_DIRECTORY="${ENVOY_DOCKER_IMAGE_DIRECTORY:-${BUILD_STAGINGDIRECTORY:-.}/build_images}"
# Setting environments for buildx tools
config_env() {
# Install QEMU emulators
docker run --rm --privileged tonistiigi/binfmt --install all
# Remove older build instance
docker buildx rm multi-builder || :
docker buildx create --use --name multi-builder --platform linux/arm64,linux/amd64
}
build_platforms() {
TYPE=$1
if is_windows; then
echo "windows/amd64"
elif [[ "${TYPE}" == *-google-vrp ]]; then
echo "linux/amd64"
else
echo "linux/arm64,linux/amd64"
fi
}
build_args() {
TYPE=$1
if [[ "${TYPE}" == *-windows* ]]; then
printf ' -f ci/Dockerfile-envoy-windows --build-arg BUILD_OS=%s --build-arg BUILD_TAG=%s' "${WINDOWS_IMAGE_BASE}" "${WINDOWS_IMAGE_TAG}"
else
TARGET="${TYPE/-debug/}"
TARGET="${TARGET/-contrib/}"
printf ' -f ci/Dockerfile-envoy --target %s' "envoy${TARGET}"
fi
if [[ "${TYPE}" == *-contrib* ]]; then
printf ' --build-arg ENVOY_BINARY=envoy-contrib'
fi
if [[ "${TYPE}" == *-debug ]]; then
printf ' --build-arg ENVOY_BINARY_SUFFIX='
fi
}
use_builder() {
# BuildKit is not available for Windows images, skip this
if ! is_windows; then
docker buildx use multi-builder
fi
}
build_images() {
local _args args=()
TYPE=$1
BUILD_TAG=$2
use_builder "${TYPE}"
_args=$(build_args "${TYPE}")
read -ra args <<<"$_args"
PLATFORM="$(build_platforms "${TYPE}")"
if ! is_windows && ! [[ "${TYPE}" =~ debug ]]; then
args+=("-o" "type=oci,dest=${ENVOY_DOCKER_IMAGE_DIRECTORY}/envoy${TYPE}.tar")
fi
docker "${BUILD_COMMAND[@]}" --platform "${PLATFORM}" "${args[@]}" -t "${BUILD_TAG}" .
}
push_images() {
local _args args=()
TYPE=$1
BUILD_TAG=$2
use_builder "${TYPE}"
_args=$(build_args "${TYPE}")
read -ra args <<<"$_args"
PLATFORM="$(build_platforms "${TYPE}")"
# docker buildx doesn't do push with default builder
docker "${BUILD_COMMAND[@]}" --platform "${PLATFORM}" "${args[@]}" -t "${BUILD_TAG}" . --push ||
docker push "${BUILD_TAG}"
}
MAIN_BRANCH="refs/heads/main"
RELEASE_BRANCH_REGEX="^refs/heads/release/v.*"
RELEASE_TAG_REGEX="^refs/tags/v.*"
# For main builds and release branch builds use the dev repo. Otherwise we assume it's a tag and
# we push to the primary repo.
if [[ "${AZP_BRANCH}" =~ ${RELEASE_TAG_REGEX} ]]; then
IMAGE_POSTFIX=""
IMAGE_NAME="${AZP_BRANCH/refs\/tags\//}"
else
IMAGE_POSTFIX="-dev"
IMAGE_NAME="${AZP_SHA1}"
fi
# This prefix is altered for the private security images on setec builds.
DOCKER_IMAGE_PREFIX="${DOCKER_IMAGE_PREFIX:-envoyproxy/envoy}"
mkdir -p "${ENVOY_DOCKER_IMAGE_DIRECTORY}"
if is_windows; then
BUILD_TYPES=("-${WINDOWS_BUILD_TYPE}")
# BuildKit is not available for Windows images, use standard build command
BUILD_COMMAND=("build")
else
# "-google-vrp" must come afer "" to ensure we rebuild the local base image dependency.
BUILD_TYPES=("" "-debug" "-contrib" "-contrib-debug" "-distroless" "-google-vrp" "-tools")
# Configure docker-buildx tools
BUILD_COMMAND=("buildx" "build")
config_env
fi
# Test the docker build in all cases, but use a local tag that we will overwrite before push in the
# cases where we do push.
for BUILD_TYPE in "${BUILD_TYPES[@]}"; do
image_tag="${DOCKER_IMAGE_PREFIX}${BUILD_TYPE}${IMAGE_POSTFIX}:${IMAGE_NAME}"
build_images "${BUILD_TYPE}" "$image_tag"
done
# Only push images for main builds, release branch builds, and tag builds.
if [[ "${AZP_BRANCH}" != "${MAIN_BRANCH}" ]] &&
! [[ "${AZP_BRANCH}" =~ ${RELEASE_BRANCH_REGEX} ]] &&
! [[ "${AZP_BRANCH}" =~ ${RELEASE_TAG_REGEX} ]]; then
echo 'Ignoring non-main branch or tag for docker push.'
exit 0
fi
docker login -u "$DOCKERHUB_USERNAME" -p "$DOCKERHUB_PASSWORD"
for BUILD_TYPE in "${BUILD_TYPES[@]}"; do
push_images "${BUILD_TYPE}" "${DOCKER_IMAGE_PREFIX}${BUILD_TYPE}${IMAGE_POSTFIX}:${IMAGE_NAME}"
# Only push latest on main builds.
if [[ "${AZP_BRANCH}" == "${MAIN_BRANCH}" ]]; then
is_windows && docker tag "${DOCKER_IMAGE_PREFIX}${BUILD_TYPE}${IMAGE_POSTFIX}:${IMAGE_NAME}" "${DOCKER_IMAGE_PREFIX}${BUILD_TYPE}${IMAGE_POSTFIX}:latest"
push_images "${BUILD_TYPE}" "${DOCKER_IMAGE_PREFIX}${BUILD_TYPE}${IMAGE_POSTFIX}:latest"
fi
# Push vX.Y-latest to tag the latest image in a release line
if [[ "${AZP_BRANCH}" =~ ${RELEASE_TAG_REGEX} ]]; then
RELEASE_LINE=$(echo "$IMAGE_NAME" | sed -E 's/(v[0-9]+\.[0-9]+)\.[0-9]+/\1-latest/')
is_windows && docker tag "${DOCKER_IMAGE_PREFIX}${BUILD_TYPE}${IMAGE_POSTFIX}:${IMAGE_NAME}" "${DOCKER_IMAGE_PREFIX}${BUILD_TYPE}${IMAGE_POSTFIX}:${RELEASE_LINE}"
push_images "${BUILD_TYPE}" "${DOCKER_IMAGE_PREFIX}${BUILD_TYPE}${IMAGE_POSTFIX}:${RELEASE_LINE}"
fi
done