Skip to content

Commit b5633d7

Browse files
Change behavior of builder.use to only use builder once
1 parent 8952a53 commit b5633d7

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

commands/build.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ fi
111111

112112
build_params+=(build)
113113

114-
if [[ -n "$(plugin_read_config BUILDER_NAME "")" ]] && [[ "$(plugin_read_config BUILDER_USE_ONCE "false")" == "true" ]]; then
115-
build_params+=("--builder" "$(plugin_read_config BUILDER_NAME "")")
116-
fi
117-
118114
if [[ ! "$(plugin_read_config SKIP_PULL "false")" == "true" ]] ; then
119115
build_params+=(--pull)
120116
fi
121117

118+
if [[ -n "$(plugin_read_config BUILDER_NAME "")" ]] && [[ "$(plugin_read_config BUILDER_USE "false")" == "true" ]]; then
119+
build_params+=("--builder" "$(plugin_read_config BUILDER_NAME "")")
120+
fi
121+
122122
if [[ "$(plugin_read_config NO_CACHE "false")" == "true" ]] ; then
123123
build_params+=(--no-cache)
124124
fi

hooks/pre-command

+5-9
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
88

99
builder_create="$(plugin_read_config BUILDER_CREATE "false")"
1010
builder_use="$(plugin_read_config BUILDER_USE "false")"
11-
builder_use_once="$(plugin_read_config BUILDER_USE_ONCE "false")"
1211
builder_name="$(plugin_read_config BUILDER_NAME "")"
1312

14-
if [[ "${builder_create}" == "true" ]] || [[ "${builder_use}" == "true" ]] || [[ "${builder_use_once}" == "true" ]]; then
13+
if [[ "${builder_create}" == "true" ]] || [[ "${builder_use}" == "true" ]]; then
1514
if [[ -z "${builder_name}" ]]; then
16-
echo "+++ 🚨 Builder Name cannot be empty when using 'create', 'use', or 'use-once' parameters"
15+
echo "+++ 🚨 Builder Name cannot be empty when using 'create' or 'use' parameters"
1716
exit 1
1817
fi
1918
fi
@@ -63,8 +62,8 @@ if [[ "${builder_create}" == "true" ]]; then
6362

6463
echo "~~~ :docker: Creating Builder Instance '${builder_name}' with Driver '${build_driver}'"
6564
docker buildx create "${builder_instance_args[@]}"
66-
if [[ "${builder_use}" == "false" ]] && [[ "${builder_use_once}" == "false" ]]; then
67-
echo "~~~ :warning: Builder Instance '${builder_name}' created but will not be used as 'use: true' or 'use-once: true' parameter not specified"
65+
if [[ "${builder_use}" == "false" ]]; then
66+
echo "~~~ :warning: Builder Instance '${builder_name}' created but will not be used as 'use: true' parameter not specified"
6867
fi
6968
else
7069
echo "~~~ :docker: Not Creating Builder Instance '${builder_name}' as already exists"
@@ -74,15 +73,12 @@ fi
7473
if [[ "${builder_use}" == "true" ]]; then
7574
if builder_instance_exists "${builder_name}"; then
7675
echo "~~~ :docker: Using Builder Instance '${builder_name}'"
77-
docker buildx use "${builder_name}"
7876
else
7977
echo "+++ 🚨 Builder Instance '${builder_name}' does not exist"
8078
exit 1
8179
fi
82-
elif [[ "${builder_use_once}" == "false" ]]; then
80+
else
8381
current_builder=$(docker buildx inspect | grep -m 1 "Name" | awk '{print $2}') || true
8482
current_builder_driver=$(docker buildx inspect | grep -m 1 "Driver" | awk '{print $2}') || true
8583
echo "~~~ :docker: Using Default Builder '${current_builder}' with Driver '${current_builder_driver}'"
86-
else
87-
echo "~~~ :docker: Using Builder Instance '${builder_name}' once"
8884
fi

tests/build.bats

+16
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ setup_file() {
3131
unstub docker
3232
}
3333

34+
@test "Build with builder" {
35+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD=myservice
36+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILDER_NAME=mybuilder
37+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILDER_USE=true
38+
39+
stub docker \
40+
"compose -f docker-compose.yml -p buildkite1111 build --pull --builder mybuilder myservice : echo built myservice"
41+
42+
run "$PWD"/hooks/command
43+
44+
assert_success
45+
assert_output --partial "built myservice"
46+
47+
unstub docker
48+
}
49+
3450
@test "Build with no-cache" {
3551
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD=myservice
3652
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_NO_CACHE=true

tests/builder-instances.bats

+4-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ load '../lib/shared'
2525
run "$PWD"/hooks/pre-command
2626

2727
assert_failure
28-
assert_output "+++ 🚨 Builder Name cannot be empty when using 'create', 'use', or 'use-once' parameters"
28+
assert_output "+++ 🚨 Builder Name cannot be empty when using 'create' or 'use' parameters"
2929
}
3030

3131
@test "Use Builder Instance with invalid Name" {
@@ -34,7 +34,7 @@ load '../lib/shared'
3434
run "$PWD"/hooks/pre-command
3535

3636
assert_failure
37-
assert_output "+++ 🚨 Builder Name cannot be empty when using 'create', 'use', or 'use-once' parameters"
37+
assert_output "+++ 🚨 Builder Name cannot be empty when using 'create' or 'use' parameters"
3838
}
3939

4040
@test "Create Builder Instance with invalid Driver" {
@@ -104,7 +104,7 @@ load '../lib/shared'
104104

105105
assert_success
106106
assert_output --partial "~~~ :docker: Creating Builder Instance 'builder-name' with Driver 'docker-container'"
107-
assert_output --partial "~~~ :warning: Builder Instance 'builder-name' created but will not be used as 'use: true' or 'use-once: true' parameter not specified"
107+
assert_output --partial "~~~ :warning: Builder Instance 'builder-name' created but will not be used as 'use: true' parameter not specified"
108108

109109
assert_output --partial "~~~ :docker: Using Default Builder 'test' with Driver 'driver'"
110110

@@ -131,8 +131,7 @@ load '../lib/shared'
131131
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILDER_NAME=builder-name
132132

133133
stub docker \
134-
"buildx inspect builder-name : exit 0" \
135-
"buildx use builder-name : exit 0"
134+
"buildx inspect builder-name : exit 0"
136135

137136
run "$PWD"/hooks/pre-command
138137

0 commit comments

Comments
 (0)