-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildctl
executable file
·166 lines (142 loc) · 4.37 KB
/
buildctl
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
#! /usr/bin/env bash
#*******************************************************************************
# Copyright (c) 2018 Eclipse Foundation and others.
# This program and the accompanying materials are made available
# under the terms of the Eclipse Public License 2.0
# which is available at http://www.eclipse.org/legal/epl-v20.html,
# or the MIT License which is available at https://opensource.org/licenses/MIT.
# SPDX-License-Identifier: EPL-2.0 OR MIT
#*******************************************************************************
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
IFS=$'\n\t'
SCRIPT_FOLDER="$(dirname "$(readlink -f "${0}")")"
stop_daemon() {
"${SCRIPT_FOLDER}/buildkitd" stop || :
}
build2() {
local images="${1}" # comma separated list of image references
local dockerfile="${2:-Dockerfile}"
local context="${3:-"$(dirname "${dockerfile}")"}"
local push="${4:-"false"}"
# e.g., allows for additionnal --opt build-args
if [[ $# -gt 4 ]]; then
shift 4
else
shift $#
fi
local first_image_name
first_image_name="$(echo "${images}" | cut -d, -f1)"
"${SCRIPT_FOLDER}/bin/buildctl" build \
--progress=plain \
--frontend=dockerfile.v0 \
--local context="${context}" \
--local dockerfile="$(dirname "${dockerfile}")" \
--opt filename="$(basename "${dockerfile}")" \
--output "type=image,\"name=${images}\",push=${push}" \
--export-cache "type=registry,mode=max,ref=${first_image_name}-buildcache" \
"${@}"
}
build_with_cache2() {
local images="${1}" # comma separated list of image references
local dockerfile="${2:-Dockerfile}"
local context="${3:-"$(dirname "${dockerfile}")"}"
local push="${4:-"false"}"
# e.g., allows for additionnal --opt build-args
if [[ $# -gt 4 ]]; then
shift 4
else
shift $#
fi
local first_image_name
first_image_name="$(echo "${images}" | cut -d, -f1)"
"${SCRIPT_FOLDER}/bin/buildctl" build \
--progress=plain \
--frontend=dockerfile.v0 \
--local context="${context}" \
--local dockerfile="$(dirname "${dockerfile}")" \
--opt filename="$(basename "${dockerfile}")" \
--output "type=image,\"name=${images}\",push=${push}" \
--export-cache "type=registry,mode=max,ref=${first_image_name}-buildcache" \
--import-cache "type=registry,ref=${first_image_name}-buildcache" \
"${@}"
}
build() {
local image="${1}"
local tag="${2}"
local dockerfile="${3:-Dockerfile}"
local context="${4:-"$(dirname "${dockerfile}")"}"
local push="${5:-"false"}"
local latest="${6:-"false"}"
if [[ $# -gt 6 ]]; then
shift 6
else
shift $#
fi
local name="${image}:${tag}"
if [[ "${latest}" == "true" ]]; then
name="${name},${image}:latest"
fi
"${SCRIPT_FOLDER}/bin/buildctl" build \
--progress=plain \
--frontend=dockerfile.v0 \
--local context="${context}" \
--local dockerfile="$(dirname "${dockerfile}")" \
--opt filename="$(basename "${dockerfile}")" \
--output "type=image,\"name=${name}\",push=${push}" \
--export-cache "type=registry,mode=max,ref=${image}:${tag}-buildcache" \
"${@}"
}
build_with_cache() {
local image="${1}"
local tag="${2}"
local dockerfile="${3:-Dockerfile}"
local context="${4:-"$(dirname "${dockerfile}")"}"
local push="${5:-"false"}"
local latest="${6:-"false"}"
if [[ $# -gt 6 ]]; then
shift 6
else
shift $#
fi
local name="${image}:${tag}"
if [[ "${latest}" == "true" ]]; then
name="${name},${image}:latest"
fi
"${SCRIPT_FOLDER}/bin/buildctl" build \
--progress=plain \
--frontend=dockerfile.v0 \
--local context="${context}" \
--local dockerfile="$(dirname "${dockerfile}")" \
--opt filename="$(basename "${dockerfile}")" \
--output "type=image,\"name=${name}\",push=${push}" \
--export-cache "type=registry,mode=max,ref=${image}:${tag}-buildcache" \
--import-cache "type=registry,ref=${image}:${tag}-buildcache" \
"${@}"
}
prune() {
"${SCRIPT_FOLDER}/bin/buildctl" prune \
--keep-storage "${1:-10240}"
}
main() {
echo "You must enter a valid command"
exit 1
}
run() {
local args=$*
local f="${1:-}"
if [[ "$f" == "" ]]; then
main
else
if [[ "${BUILDCTL_STOP_DAEMON_AFTER_USE:-}" == "true" ]]; then
trap stop_daemon EXIT
fi
"${SCRIPT_FOLDER}/buildkitd" start
BUILDKIT_HOST="$("${SCRIPT_FOLDER}/buildkitd" host)"
export BUILDKIT_HOST
$args
fi
}
run "$@"