forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-docker.sh
More file actions
executable file
·128 lines (111 loc) · 3.24 KB
/
build-docker.sh
File metadata and controls
executable file
·128 lines (111 loc) · 3.24 KB
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
#!/usr/bin/env bash
set -euo pipefail
# build-docker.sh
#
# SUMMARY
#
# Builds the Vector docker images and optionally
# pushes it to the Docker registry
set -x
CHANNEL="${CHANNEL:-"$(cargo vdev release channel)"}"
VERSION="${VECTOR_VERSION:-"$(cargo vdev version)"}"
DATE="${DATE:-"$(date -u +%Y-%m-%d)"}"
PLATFORM="${PLATFORM:-}"
PUSH="${PUSH:-"true"}"
REPOS="${REPOS:-"timberio/vector"}"
IFS=, read -ra REPO_LIST <<< "$REPOS"
IFS=, read -ra REQUESTED_PLATFORMS <<< "$PLATFORM"
declare -A SUPPORTED_PLATFORMS=(
[debian]="linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8"
[alpine]="linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8"
[distroless-static]="linux/amd64,linux/arm/v7,linux/arm64/v8"
[distroless-libc]="linux/amd64,linux/arm/v7,linux/arm64/v8"
)
#
# Functions
#
evaluate_supported_platforms_for_base() {
local BASE="$1"
IFS=, read -ra SUPPORTED_PLATFORMS_FOR_BASE <<< "${SUPPORTED_PLATFORMS["$BASE"]}"
local BUILDABLE_PLATFORMS=""
for platform in "${REQUESTED_PLATFORMS[@]}"
do
if [[ ${SUPPORTED_PLATFORMS_FOR_BASE[*]} =~ $platform ]]
then
BUILDABLE_PLATFORMS+="$platform,"
else
>&2 echo "WARN: skipping $platform for $BASE, no base image for platform"
fi
done
echo "${BUILDABLE_PLATFORMS%?}"
}
build() {
local BASE="$1"
local VERSION="$2"
local DOCKERFILE="distribution/docker/$BASE/Dockerfile"
local BUILDABLE_PLATFORMS=""
if [ -n "$PLATFORM" ]; then
BUILDABLE_PLATFORMS=$(evaluate_supported_platforms_for_base "$BASE")
fi
# Collect all tags
TAGS=()
for REPO in "${REPO_LIST[@]}"; do
TAGS+=(--tag "$REPO:$VERSION-$BASE")
done
# Build once with all tags
if [ -n "$PLATFORM" ]; then
ARGS=()
if [[ "$PUSH" == "true" ]]; then
ARGS+=(--push)
fi
docker buildx build \
--platform="$BUILDABLE_PLATFORMS" \
"${TAGS[@]}" \
target/artifacts \
-f "$DOCKERFILE" \
"${ARGS[@]}"
else
docker build \
"${TAGS[@]}" \
target/artifacts \
-f "$DOCKERFILE"
if [[ "$PUSH" == "true" ]]; then
for TAG in "${TAGS[@]}"; do
docker push "$TAG"
done
fi
fi
}
#
# Build
#
echo "Building Docker images for $REPOS"
if [[ "$CHANNEL" == "release" ]]; then
VERSION_EXACT="$VERSION"
# shellcheck disable=SC2001
VERSION_MINOR_X=$(echo "$VERSION" | sed 's/\.[0-9]*$/.X/g')
# shellcheck disable=SC2001
VERSION_MAJOR_X=$(echo "$VERSION" | sed 's/\.[0-9]*\.[0-9]*$/.X/g')
for VERSION_TAG in "$VERSION_EXACT" "$VERSION_MINOR_X" "$VERSION_MAJOR_X" latest; do
build alpine "$VERSION_TAG"
build debian "$VERSION_TAG"
build distroless-static "$VERSION_TAG"
build distroless-libc "$VERSION_TAG"
done
elif [[ "$CHANNEL" == "nightly" ]]; then
for VERSION_TAG in "nightly-$DATE" nightly; do
build alpine "$VERSION_TAG"
build debian "$VERSION_TAG"
build distroless-static "$VERSION_TAG"
build distroless-libc "$VERSION_TAG"
done
elif [[ "$CHANNEL" == "custom" ]]; then
build alpine "$VERSION"
build debian "$VERSION"
build distroless-static "$VERSION"
build distroless-libc "$VERSION"
elif [[ "$CHANNEL" == "test" ]]; then
build "${BASE:-"alpine"}" "${TAG:-"test"}"
build "${BASE:-"distroless-libc"}" "${TAG:-"test"}"
build "${BASE:-"distroless-static"}" "${TAG:-"test"}"
fi