Skip to content

Commit ce37561

Browse files
authored
Merge pull request #134 from buildkite-plugins/toote_AWS_profile
Support AWS profiles
2 parents 2084293 + 395bda6 commit ce37561

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ Store things in an S3 bucket. You need to make sure that the `aws` command is av
9797
**AWS Authentication**: For AWS role assumption, use the [aws-assume-role-with-web-identity](https://github.com/buildkite-plugins/aws-assume-role-with-web-identity-buildkite-plugin) plugin. Alternatively, configure AWS credentials via IAM instance profiles, environment variables, or AWS CLI configuration.
9898

9999
You also need the agent to have access to the following defined environment variables:
100-
* `BUILDKITE_PLUGIN_S3_CACHE_BUCKET`: the bucket to use (backend will fail if not defined)
100+
* `BUILDKITE_PLUGIN_S3_CACHE_BUCKET`: the bucket to use (**mandatory**, backend will fail if not defined)
101101
* `BUILDKITE_PLUGIN_S3_CACHE_PREFIX`: optional prefix to use for the cache within the bucket
102102
* `BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT`: optional S3 custom endpoint to use
103+
* `BUILDKITE_PLUGIN_S3_CACHE_PROFILE`: optional profile (that [must exist in the agent's config](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-role.html)) to use for CLI calls
103104

104105
Setting the `BUILDKITE_PLUGIN_S3_CACHE_ONLY_SHOW_ERRORS` environment variable will reduce logging of file operations towards S3.
105106

@@ -112,6 +113,7 @@ env:
112113
BUILDKITE_PLUGIN_S3_CACHE_PREFIX: "buildkite/cache"
113114
BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT: "https://<your-endpoint>"
114115
BUILDKITE_PLUGIN_S3_CACHE_ONLY_SHOW_ERRORS: "true"
116+
BUILDKITE_PLUGIN_S3_CACHE_PROFILE: cache-role
115117
116118
steps:
117119
- label: ':nodejs: Install dependencies'

backends/cache_s3

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ build_key() {
1616
aws_cmd() {
1717
aws_cmd=(aws)
1818

19+
if [ -n "${BUILDKITE_PLUGIN_S3_CACHE_PROFILE}" ]; then
20+
aws_cmd+=(--profile "${BUILDKITE_PLUGIN_S3_CACHE_PROFILE}")
21+
fi
22+
1923
if [ -n "${BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT}" ]; then
2024
aws_cmd+=(--endpoint-url "${BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT}")
2125
fi

tests/cache_s3.bats

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ setup() {
6464
's3 sync --only-show-errors \* \* : echo ' \
6565
's3api head-object --bucket \* --key \* : false ' \
6666
's3 sync --only-show-errors \* \* : echo ' \
67-
's3 sync \* \* : echo ' \
68-
's3api head-object --bucket \* --key \* : false ' \
69-
's3 sync \* \* : echo '
67+
's3api list-objects-v2 --bucket \* --prefix \* --max-items 1 --query Contents : echo exists'
7068

7169
run "${PWD}/backends/cache_s3" save from to
7270

@@ -78,14 +76,7 @@ setup() {
7876
assert_success
7977
assert_output ''
8078

81-
unset BUILDKITE_PLUGIN_S3_CACHE_ONLY_SHOW_ERRORS
82-
83-
run "${PWD}/backends/cache_s3" save from to
84-
85-
assert_success
86-
assert_output ''
87-
88-
run "${PWD}/backends/cache_s3" get from to
79+
run "${PWD}/backends/cache_s3" exists to
8980

9081
assert_success
9182
assert_output ''
@@ -100,11 +91,7 @@ setup() {
10091
'--endpoint-url https://s3.somewhere.com s3 sync \* \* : echo ' \
10192
'--endpoint-url https://s3.somewhere.com s3api head-object --bucket \* --key \* : false ' \
10293
'--endpoint-url https://s3.somewhere.com s3 sync \* \* : echo ' \
103-
'--endpoint-url https://s3.somewhere.com s3api list-objects-v2 --bucket \* --prefix \* --max-items 1 --query Contents : echo exists' \
104-
's3 sync \* \* : echo ' \
105-
's3api head-object --bucket \* --key \* : false ' \
106-
's3 sync \* \* : echo ' \
107-
's3api list-objects-v2 --bucket \* --prefix \* --max-items 1 --query Contents : echo exists'
94+
'--endpoint-url https://s3.somewhere.com s3api list-objects-v2 --bucket \* --prefix \* --max-items 1 --query Contents : echo exists'
10895

10996
run "${PWD}/backends/cache_s3" save from to
11097

@@ -120,8 +107,16 @@ setup() {
120107

121108
assert_success
122109
assert_output ''
110+
}
111+
112+
@test 'Profile is passed when environment is set' {
113+
export BUILDKITE_PLUGIN_S3_CACHE_PROFILE=custom-profile
123114

124-
unset BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT
115+
stub aws \
116+
'--profile custom-profile s3 sync \* \* : echo ' \
117+
'--profile custom-profile s3api head-object --bucket \* --key \* : false ' \
118+
'--profile custom-profile s3 sync \* \* : echo ' \
119+
'--profile custom-profile s3api list-objects-v2 --bucket \* --prefix \* --max-items 1 --query Contents : echo exists' \
125120

126121
run "${PWD}/backends/cache_s3" save from to
127122

0 commit comments

Comments
 (0)