-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild
executable file
·120 lines (107 loc) · 4.36 KB
/
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
#
# Builds the nextstrain/base and nextstrain/base-builder images with useful
# caching and pushes to a registry.
#
# By default this tags images using "latest" and pushes to localhost:5000, but
# you can provide a custom tag with -t <tag> and specify a different
# registry with -r <registry>.
#
# Set CACHE_DATE in your environment to force layers after our custom cache
# point to be re-built. See the ARG CACHE_DATE line in the Dockerfile for more
# information.
#
set -euo pipefail
devel="$(dirname "$0")"
# Set default values.
platform="$("$devel"/platform)"
registry=localhost:5000
tag=latest
log_directory="logs/"
# Read command-line arguments.
while getopts "p:r:t:l:" opt; do
case "$opt" in
p) platform="$OPTARG";;
r) registry="$OPTARG";;
t) tag="$OPTARG";;
l) log_directory="$OPTARG";;
*) echo "Usage: $0 [-p <platform>] [-r <registry>] [-t <tag>] [-l <log directory>]" 1>&2; exit 1;;
esac
done
# shellcheck disable=SC2155
export GIT_REVISION=$(git describe --tags --abbrev=40 --always --dirty || true)
# The nextstrain/base Dockerfile is a multi-stage with both a "builder" target
# and a main target. To enable proper caching via --cache-from we need both
# these images available to pull layers from. This means pulling both in at
# the start and pushing both up at the end.
#
# Calling `docker run nextstrain/base` will still only pull down the small base
# image rather than pulling down the larger nextstrain/base-builder image.
# `buildx create` is necessary to use a driver that supports multi-platform
# images.
builder=nextstrain-builder
if ! docker buildx inspect "$builder" &>/dev/null; then
# Using a persistent builder allows for faster local development.
# However, if this is changed and it was previously run on your machine,
# you may need to remove the builder manually before running the script:
# docker buildx rm "nextstrain-builder"
docker buildx create --name "$builder" --driver docker-container --driver-opt network=host
fi
mkdir -p "$log_directory"
BUILDER_BUILD_PLATFORM_IMAGE=nextstrain/base-builder-build-platform
BUILDER_TARGET_PLATFORM_IMAGE=nextstrain/base-builder-target-platform
FINAL_IMAGE=nextstrain/base
docker buildx build \
--target builder-build-platform \
--builder "$builder" \
--platform "$platform" \
--build-arg CACHE_DATE \
--cache-from "$BUILDER_BUILD_PLATFORM_IMAGE:latest" \
--cache-from "$BUILDER_BUILD_PLATFORM_IMAGE:$tag" \
--cache-from "$registry/$BUILDER_BUILD_PLATFORM_IMAGE:latest" \
--cache-from "$registry/$BUILDER_BUILD_PLATFORM_IMAGE:$tag" \
--cache-to type=inline \
--tag "$registry/$BUILDER_BUILD_PLATFORM_IMAGE:$tag" \
--push \
--provenance false \
--progress=plain \
. 2>&1 | tee "$log_directory"/builder-build-platform
docker buildx build \
--target builder-target-platform \
--builder "$builder" \
--platform "$platform" \
--build-arg CACHE_DATE \
--cache-from "$BUILDER_TARGET_PLATFORM_IMAGE:latest" \
--cache-from "$BUILDER_TARGET_PLATFORM_IMAGE:$tag" \
--cache-from "$registry/$BUILDER_TARGET_PLATFORM_IMAGE:latest" \
--cache-from "$registry/$BUILDER_TARGET_PLATFORM_IMAGE:$tag" \
--cache-to type=inline \
--tag "$registry/$BUILDER_TARGET_PLATFORM_IMAGE:$tag" \
--push \
--provenance false \
--progress=plain \
. 2>&1 | tee "$log_directory"/builder-target-platform
docker buildx build \
--target final \
--builder "$builder" \
--platform "$platform" \
--build-arg GIT_REVISION \
--build-arg CACHE_DATE \
--cache-from "$BUILDER_BUILD_PLATFORM_IMAGE:latest" \
--cache-from "$BUILDER_BUILD_PLATFORM_IMAGE:$tag" \
--cache-from "$BUILDER_TARGET_PLATFORM_IMAGE:latest" \
--cache-from "$BUILDER_TARGET_PLATFORM_IMAGE:$tag" \
--cache-from "$FINAL_IMAGE:latest" \
--cache-from "$FINAL_IMAGE:$tag" \
--cache-from "$registry/$BUILDER_BUILD_PLATFORM_IMAGE:latest" \
--cache-from "$registry/$BUILDER_BUILD_PLATFORM_IMAGE:$tag" \
--cache-from "$registry/$BUILDER_TARGET_PLATFORM_IMAGE:latest" \
--cache-from "$registry/$BUILDER_TARGET_PLATFORM_IMAGE:$tag" \
--cache-from "$registry/$FINAL_IMAGE:latest" \
--cache-from "$registry/$FINAL_IMAGE:$tag" \
--cache-to type=inline \
--tag "$registry/$FINAL_IMAGE:$tag" \
--push \
--provenance false \
--progress=plain \
. 2>&1 | tee "$log_directory"/final