Skip to content

Commit a0c7ae2

Browse files
authored
Merge pull request #130 from rajatvig/add-download-config-option
feat: add download configuration option to support pre-installed binaries
2 parents 46b60ac + 4a9d769 commit a0c7ae2

5 files changed

Lines changed: 147 additions & 3 deletions

File tree

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,30 @@ steps:
281281
trigger: "deploy-foo-service"
282282
```
283283

284+
### `download` (optional)
285+
286+
Default: `true`
287+
288+
By setting `download` to `false`, the plugin will use a pre-installed binary instead of downloading it on each run. The binary `monorepo-diff-buildkite-plugin` must be available in your PATH (typically `/usr/bin`).
289+
290+
This option is useful for:
291+
- Reducing build time by avoiding repeated downloads
292+
- Improving security by using pre-vetted binaries
293+
- Organizations with policies against runtime binary downloads
294+
295+
```yaml
296+
steps:
297+
- label: "Triggering pipelines"
298+
plugins:
299+
- monorepo-diff#v1.6.0:
300+
download: false
301+
diff: "git diff --name-only HEAD~1"
302+
watch:
303+
- path: "foo-service/"
304+
config:
305+
trigger: "deploy-foo-service"
306+
```
307+
284308
### `hooks` (optional)
285309

286310
Currently supports a list of `commands` you wish to execute after the `watched` pipelines have been triggered

docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
tests:
3+
image: buildkite/plugin-tester:latest
4+
volumes:
5+
- .:/plugin

hooks/command

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ get_version() {
100100
else
101101
_version=""
102102
fi
103-
103+
104104
RETVAL="$_version"
105105
}
106106

@@ -152,8 +152,25 @@ download_binary_and_run() {
152152

153153
# todo: move it to a more secure place
154154
chmod +x "${executable}"
155-
155+
156156
./"${executable}"
157157
}
158158

159-
download_binary_and_run "$@" || exit 1
159+
run_preinstalled_binary() {
160+
local _executable="monorepo-diff-buildkite-plugin"
161+
162+
if ! check_cmd "$_executable"; then
163+
err "Binary '$_executable' not found in PATH. Please install it or set download: true"
164+
fi
165+
166+
${_executable} "$@"
167+
}
168+
169+
# Check if download option is disabled (default is true for backward compatibility)
170+
download_enabled="${BUILDKITE_PLUGIN_MONOREPO_DIFF_DOWNLOAD:-true}"
171+
172+
if [[ "$download_enabled" == "false" ]]; then
173+
run_preinstalled_binary "$@" || exit 1
174+
else
175+
download_binary_and_run "$@" || exit 1
176+
fi

plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ configuration:
77
properties:
88
diff:
99
type: string
10+
download:
11+
type: boolean
1012
log_level:
1113
type: string
1214
interpolation:

tests/command.bats

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env bats
2+
3+
load "$BATS_PLUGIN_PATH/load.bash"
4+
5+
# Uncomment the following line to debug stubbed commands
6+
# export COMMAND_STUB_DEBUG=/dev/tty
7+
8+
setup() {
9+
export BUILDKITE_PLUGIN_MONOREPO_DIFF_BUILDKITE_PLUGIN_TEST_MODE=true
10+
# Set a minimal plugin config (only used by Go binary, not the bash hook's download logic)
11+
export BUILDKITE_PLUGINS='[{"monorepo-diff": {}}]'
12+
13+
# Create a mock binary in the current directory for download mode tests
14+
cat > "$PWD/monorepo-diff-buildkite-plugin" << 'MOCKBIN'
15+
#!/bin/bash
16+
echo "Mock binary executed with args: $@"
17+
MOCKBIN
18+
chmod +x "$PWD/monorepo-diff-buildkite-plugin"
19+
}
20+
21+
teardown() {
22+
# Clean up the mock binary
23+
rm -f "$PWD/monorepo-diff-buildkite-plugin"
24+
}
25+
26+
@test "download=false with binary in PATH succeeds" {
27+
# Buildkite sets this env var based on plugin config
28+
export BUILDKITE_PLUGIN_MONOREPO_DIFF_DOWNLOAD=false
29+
export PATH="$PWD:$PATH"
30+
31+
run "$PWD/hooks/command"
32+
33+
assert_success
34+
assert_output --partial "Mock binary executed"
35+
}
36+
37+
@test "download=false without binary in PATH fails with error" {
38+
# Buildkite sets this env var based on plugin config
39+
export BUILDKITE_PLUGIN_MONOREPO_DIFF_DOWNLOAD=false
40+
41+
# Remove mock binary from current directory and don't add to PATH
42+
rm -f "$PWD/monorepo-diff-buildkite-plugin"
43+
export PATH="/usr/bin:/bin"
44+
45+
run "$PWD/hooks/command"
46+
47+
assert_failure
48+
assert_output --partial "Binary 'monorepo-diff-buildkite-plugin' not found in PATH"
49+
assert_output --partial "Please install it or set download: true"
50+
}
51+
52+
@test "download=true executes binary in test mode" {
53+
export BUILDKITE_PLUGIN_MONOREPO_DIFF_DOWNLOAD=true
54+
55+
# In test mode, it skips download but still executes the mock binary we created
56+
run "$PWD/hooks/command"
57+
58+
assert_success
59+
assert_output --partial "Mock binary executed"
60+
}
61+
62+
@test "download defaults to true when not set" {
63+
# Don't set BUILDKITE_PLUGIN_MONOREPO_DIFF_DOWNLOAD - should default to true
64+
65+
# Should default to true, skip download in test mode, and execute mock binary
66+
run "$PWD/hooks/command"
67+
68+
assert_success
69+
assert_output --partial "Mock binary executed"
70+
}
71+
72+
@test "download=false passes arguments to preinstalled binary" {
73+
export BUILDKITE_PLUGIN_MONOREPO_DIFF_DOWNLOAD=false
74+
export PATH="$PWD:$PATH"
75+
76+
run "$PWD/hooks/command" arg1 arg2
77+
78+
assert_success
79+
assert_output --partial "Mock binary executed with args: arg1 arg2"
80+
}
81+
82+
@test "download=true in test mode skips actual download" {
83+
export BUILDKITE_PLUGIN_MONOREPO_DIFF_DOWNLOAD=true
84+
85+
# Stub curl to verify it's NOT called in test mode
86+
stub curl \
87+
": echo 'ERROR: curl should not be called in test mode'; exit 1"
88+
89+
run "$PWD/hooks/command"
90+
91+
# Should succeed without calling curl
92+
assert_success
93+
94+
# Clean up stub (it should not have been called)
95+
unstub curl || true
96+
}

0 commit comments

Comments
 (0)