-
Notifications
You must be signed in to change notification settings - Fork 9
/
docker_build
executable file
·99 lines (77 loc) · 2.73 KB
/
docker_build
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
#!/usr/bin/env bash
set -Eeuo pipefail
die() { echo "$1" 1>&2; exit 1; }
# Required env vars.
[ -n "${BIN_NAME:-}" ] || die "Must set BIN_NAME"
[ -n "${VERSION:-}" ] || die "Must set VERSION"
[ -n "${REVISION:-}" ] || die "Must set REVISION"
[ -n "${DOCKERFILE:-}" ] || die "Must set DOCKERFILE"
[ -n "${TARGET:-}" ] || die "Must set TARGET"
[ -n "${TARBALL_NAME:-}" ] || die "Must set TARBALL_NAME"
[ -n "${DEV_TARBALL_NAME:-}" ] || die "Must set DEV_TARBALL_NAME"
[ -n "${REDHAT_TARBALL_NAME:-}" ] || die "Must set REDHAT_TARBALL_NAME"
[ -n "${PLATFORM:-}" ] || die "Must set PLATFORM"
[ -n "${AUTO_TAG:-}" ] || die "Must set AUTO_TAG"
[ -n "${TAGS:-}${REDHAT_TAG:-}" ] || die "Must set either TAG or REDHAT_TAG"
[ -z "${TAGS:-}" ] || [ -z "${REDHAT_TAG:-}" ] || die "Must set either TAG or REDHAT_TAG (not both)"
WORKDIR="${WORKDIR:-.}"
export DEV_TAGS="${DEV_TAGS:-}"
export REDHAT_TAG="${REDHAT_TAG:-}"
# Convert all contiguous blocks of whitespace to single spaces using xargs.
# This is needed to get all tags on one line for the read -ra below.
PROD_TAGS="$(xargs <<< "$TAGS")"
DEV_TAGS="$(xargs <<< "$DEV_TAGS")"
read -ra DEV_TAGS_A <<< "$DEV_TAGS"
read -ra PROD_TAGS_A <<< "$PROD_TAGS"
ALL_TAGS=("${DEV_TAGS_A[@]}")
ALL_TAGS+=("$AUTO_TAG")
ALL_TAGS+=("${PROD_TAGS_A[@]}")
if [ -n "$REDHAT_TAG" ]; then
ALL_TAGS+=("${REDHAT_TAG}")
fi
echo "ALL_TAGS:" "${ALL_TAGS[@]}"
for T in "${ALL_TAGS[@]}"; do
TAG_FLAGS+=("--tag=$T")
done
BUILD_ARGS=("BIN_NAME=$BIN_NAME")
BUILD_ARGS+=("PRODUCT_VERSION=$VERSION")
BUILD_ARGS+=("PRODUCT_REVISION=$REVISION")
for B in "${BUILD_ARGS[@]}"; do
BA_FLAGS+=("--build-arg=$B")
done
TARBALL_PATH="$PWD/$TARBALL_NAME"
DEV_TARBALL_PATH="$PWD/$DEV_TARBALL_NAME"
REDHAT_TARBALL_PATH="$PWD/$REDHAT_TARBALL_NAME"
cd "$WORKDIR"
docker buildx create --use
echo "==> Building image with tags:"
for T in "${ALL_TAGS[@]}"; do
echo " - $T"
done
docker buildx build --load \
--target "$TARGET" \
--platform "$PLATFORM" \
"${TAG_FLAGS[@]}" \
"${BA_FLAGS[@]}" \
-f "$DOCKERFILE" \
.
echo "==> Saving prod tags in '$TARBALL_PATH'"
docker save --output "$TARBALL_PATH" "${PROD_TAGS_A[@]}" "$AUTO_TAG"
echo "==> Tarball created."
ls -lah "$TARBALL_PATH"
if [ -n "$DEV_TAGS" ]; then
echo "==> Saving dev tags in '$DEV_TARBALL_PATH'"
docker save --output "$DEV_TARBALL_PATH" "${DEV_TAGS_A[@]}" "$AUTO_TAG"
echo "==> Dev Tarball created."
ls -lah "$DEV_TARBALL_PATH"
else
echo "==> No dev tags specified, skipping export."
fi
if [ -n "$REDHAT_TAG" ]; then
echo "==> Saving Redhat tag '$REDHAT_TAG' to '$REDHAT_TARBALL_PATH'"
docker save --output "$REDHAT_TARBALL_PATH" "${REDHAT_TAG}" "$AUTO_TAG"
echo "==> Redhat Tarball created."
ls -lah "$REDHAT_TARBALL_PATH"
else
echo "==> No Redhat tag specified, skipping export."
fi