Skip to content

Commit bbbd571

Browse files
authored
Merge pull request #51 from opencastsoftware/custom-s3-endpoint
Add support for custom S3-compatible services using BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT
2 parents 06d4652 + cc2203b commit bbbd571

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Defines how the cache is stored and restored. Can be any string (see [Customizab
4848

4949
Very basic local filesystem backend.
5050

51-
The `BUILDKITE_PLUGIN_FS_CACHE_FOLDER` environment variable defines where the copies are (default: `/var/cache/buildkite`). If you don't change it, you will need to make sure that the folder exists and `buildkite-agent` has the propper permissions, otherwise the plugin will fail.
51+
The `BUILDKITE_PLUGIN_FS_CACHE_FOLDER` environment variable defines where the copies are (default: `/var/cache/buildkite`). If you don't change it, you will need to make sure that the folder exists and `buildkite-agent` has the propper permissions, otherwise the plugin will fail.
5252

5353
**IMPORTANT**: the `fs` backend just copies files to a different location in the current agent, as it is not a shared or external resource, its caching possibilities are quite limited.
5454

@@ -59,6 +59,7 @@ Store things in an S3 bucket. You need to make sure that the `aws` command is av
5959
You also need the agent to have access to the following defined environment variables:
6060
* `BUILDKITE_PLUGIN_S3_CACHE_BUCKET`: the bucket to use (backend will fail if not defined)
6161
* `BUILDKITE_PLUGIN_S3_CACHE_PREFIX`: optional prefix to use for the cache within the bucket
62+
* `BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT`: optional S3 custom endpoint to use
6263

6364
Setting the `BUILDKITE_PLUGIN_S3_CACHE_ONLY_SHOW_ERRORS` environment variable will reduce logging of file operations towards S3.
6465

@@ -91,7 +92,7 @@ When restoring from cache, **all levels, in the described order, up to the one s
9192

9293
One of the greatest flexibilities of this plugin is its flexible backend architecture. You can provide whatever value you want for the `backend` option of this plugin (`X` for example) as long as there is an executable script accessible to the agent named `cache_X` that respects the following execution protocol:
9394

94-
* `cache_X exists $KEY`
95+
* `cache_X exists $KEY`
9596

9697
Should exit successfully (0 return code) if any previous call to this very same plugin was made with `cache_x save $KEY`. Any other exit code will mean that there is no valid cache and will be ignored.
9798

@@ -122,7 +123,7 @@ You can always have more complicated logic by using the plugin multiple times wi
122123
```yaml
123124
steps:
124125
- label: ':nodejs: Install dependencies'
125-
command: npm ci
126+
command: npm ci
126127
plugins:
127128
- cache#v0.5.0:
128129
manifest: package-lock.json

backends/cache_s3

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,30 @@ s3_sync() {
1818
local to="$2"
1919

2020
aws_cmd=(aws s3 sync)
21+
2122
if [ -n "${BUILDKITE_PLUGIN_S3_CACHE_ONLY_SHOW_ERRORS}" ]; then
2223
aws_cmd+=(--only-show-errors)
2324
fi
2425

26+
if [ -n "${BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT}" ]; then
27+
aws_cmd+=(--endpoint-url "${BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT}")
28+
fi
29+
2530
"${aws_cmd[@]}" "${from}" "${to}"
2631
}
2732

33+
s3_listobjects() {
34+
local prefix="$1"
35+
36+
aws_cmd=(aws s3api list-objects-v2 --bucket "${BUILDKITE_PLUGIN_S3_CACHE_BUCKET}" --prefix "$(build_key "${prefix}")" --max-items 1)
37+
38+
if [ -n "${BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT}" ]; then
39+
aws_cmd+=(--endpoint-url "${BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT}")
40+
fi
41+
42+
"${aws_cmd[@]}"
43+
}
44+
2845
restore_cache() {
2946
local from="$1"
3047
local to="$2"
@@ -39,7 +56,7 @@ save_cache() {
3956

4057
exists_cache() {
4158
if [ -z "$1" ]; then exit 1; fi
42-
[ -n "$(aws s3api list-objects-v2 --bucket "${BUILDKITE_PLUGIN_S3_CACHE_BUCKET}" --prefix "$(build_key "$1")" --max-items 1)" ]
59+
[ -n "$(s3_listobjects "$1")" ]
4360
}
4461

4562
OPCODE="$1"

tests/cache_s3.bats

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,52 @@ setup() {
9191
unstub aws
9292
}
9393

94+
@test 'Endpoint URL flag passed when environment is set' {
95+
export BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT=https://s3.somewhere.com
96+
97+
stub aws \
98+
's3 sync --endpoint-url https://s3.somewhere.com \* \* : echo ' \
99+
's3 sync --endpoint-url https://s3.somewhere.com \* \* : echo ' \
100+
's3api list-objects-v2 --bucket \* --prefix \* --max-items 1 --endpoint-url https://s3.somewhere.com : echo exists' \
101+
's3 sync \* \* : echo ' \
102+
's3 sync \* \* : echo ' \
103+
's3api list-objects-v2 --bucket \* --prefix \* --max-items 1 : echo exists'
104+
105+
run "${PWD}/backends/cache_s3" save from to
106+
107+
assert_success
108+
assert_output ''
109+
110+
run "${PWD}/backends/cache_s3" get from to
111+
112+
assert_success
113+
assert_output ''
114+
115+
run "${PWD}/backends/cache_s3" exists to
116+
117+
assert_success
118+
assert_output ''
119+
120+
unset BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT
121+
122+
run "${PWD}/backends/cache_s3" save from to
123+
124+
assert_success
125+
assert_output ''
126+
127+
run "${PWD}/backends/cache_s3" get from to
128+
129+
assert_success
130+
assert_output ''
131+
132+
run "${PWD}/backends/cache_s3" exists to
133+
134+
assert_success
135+
assert_output ''
136+
137+
unstub aws
138+
}
139+
94140
@test 'File exists and can be restored after save' {
95141
touch "${BATS_TEST_TMPDIR}/new-file"
96142
mkdir "${BATS_TEST_TMPDIR}/s3-cache"

0 commit comments

Comments
 (0)