Skip to content

Commit 8952a53

Browse files
Add builder.use-once parameter
When `builder.use` is `true`, this plugin calls `docker buildx use` which sets the default builder for the entire Buildkite agent. This can cause issues for other Buildkite steps on the same agent. For example, another pipeline step might land on the same agent and expect to use the default `docker` driver, which does not require the user to specify `--load` when calling `docker build`. This results in the image not being available for subsequent commands (e.g. `docker tag` or `docker push`) and can break a pipeline that was previously working. This change adds the `builder.use-once` parameter. When `true`, the plugin will pass the `--builder` flag to `docker build` instead of calling `docker buildx use`. This ensures that the specified builder is only used for this particular step.
1 parent 4ac4d6d commit 8952a53

3 files changed

Lines changed: 17 additions & 10 deletions

File tree

commands/build.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ 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+
114118
if [[ ! "$(plugin_read_config SKIP_PULL "false")" == "true" ]] ; then
115119
build_params+=(--pull)
116120
fi

hooks/pre-command

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ 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")"
1112
builder_name="$(plugin_read_config BUILDER_NAME "")"
1213

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

6364
echo "~~~ :docker: Creating Builder Instance '${builder_name}' with Driver '${build_driver}'"
6465
docker buildx create "${builder_instance_args[@]}"
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"
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"
6768
fi
6869
else
6970
echo "~~~ :docker: Not Creating Builder Instance '${builder_name}' as already exists"
@@ -78,8 +79,10 @@ if [[ "${builder_use}" == "true" ]]; then
7879
echo "+++ 🚨 Builder Instance '${builder_name}' does not exist"
7980
exit 1
8081
fi
81-
else
82+
elif [[ "${builder_use_once}" == "false" ]]; then
8283
current_builder=$(docker buildx inspect | grep -m 1 "Name" | awk '{print $2}') || true
8384
current_builder_driver=$(docker buildx inspect | grep -m 1 "Driver" | awk '{print $2}') || true
8485
echo "~~~ :docker: Using Default Builder '${current_builder}' with Driver '${current_builder_driver}'"
86+
else
87+
echo "~~~ :docker: Using Builder Instance '${builder_name}' once"
8588
fi

tests/builder-instances.bats

Lines changed: 5 additions & 5 deletions
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' or 'use' parameters"
28+
assert_output "+++ 🚨 Builder Name cannot be empty when using 'create', 'use', or 'use-once' 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' or 'use' parameters"
37+
assert_output "+++ 🚨 Builder Name cannot be empty when using 'create', 'use', or 'use-once' parameters"
3838
}
3939

4040
@test "Create Builder Instance with invalid Driver" {
@@ -104,8 +104,8 @@ 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' parameter not specified"
108-
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"
108+
109109
assert_output --partial "~~~ :docker: Using Default Builder 'test' with Driver 'driver'"
110110

111111
unstub docker
@@ -198,7 +198,7 @@ load '../lib/shared'
198198

199199
assert_success
200200
assert_output "~~~ :docker: Cleaning up Builder Instance 'builder-name'"
201-
201+
202202
unstub docker
203203
}
204204

0 commit comments

Comments
 (0)