Skip to content

Commit 5ede176

Browse files
samchungy72636c
andauthored
Add option to skip pull from cache (#47)
* Skip pull on cache hit * add newlines * fix if * update readme * rename var * revert quote change * fix env var * set plugin tester version * Update README.md * add test * update return * Update README.md * Update ecr-registry-provider.bash * Update gcr-registry-provider.bash * Update README.md * fix tests * Update tests/ecr-registry-provider.bats Co-authored-by: Ryan Ling <[email protected]> * fix erroneously commited format change --------- Co-authored-by: Ryan Ling <[email protected]>
1 parent 72f5aea commit 5ede176

File tree

8 files changed

+104
-9
lines changed

8 files changed

+104
-9
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,19 @@ steps:
291291
- my-custom-plugin#v1.0.0:
292292
```
293293

294+
### Skipping image pull from cache
295+
296+
By default, this plugin will pull the image when a cache hit is found. In scenarios where you may be using a caching step to ensure that an image exists for future steps, this may not be required. You can use `skip-pull-from-cache` to allow the plugin to exit early without pulling the image.
297+
298+
```yaml
299+
steps:
300+
- label: Build Cache
301+
command: ':'
302+
plugins:
303+
- seek-oss/docker-ecr-cache#v2.0.0:
304+
skip-pull-from-cache: true
305+
```
306+
294307
### AWS ECR specific configuration
295308

296309
#### Specifying an ECR repository name

hooks/lib/ecr-registry-provider.bash

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ ecr_exists() {
3333
--query 'repositories[0].registryId'
3434
}
3535

36+
image_exists() {
37+
local repository_name="$(get_ecr_repository_name)"
38+
local image_tag="${1}"
39+
local image_meta="$(aws ecr list-images \
40+
--repository-name "${repository_name}" \
41+
--query "imageIds[?imageTag=='${image_tag}'].imageTag" \
42+
--output text)"
43+
44+
if [ "$image_meta" == "$image_tag" ]; then
45+
true
46+
else
47+
false
48+
fi
49+
}
50+
3651
get_ecr_arn() {
3752
local repository_name="${1}"
3853
aws ecr describe-repositories \

hooks/lib/gcr-registry-provider.bash

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ get_registry_url() {
1818
fi
1919
echo "${BUILDKITE_PLUGIN_DOCKER_ECR_CACHE_REGISTRY_HOSTNAME}/${BUILDKITE_PLUGIN_DOCKER_ECR_CACHE_GCP_PROJECT}/${BUILDKITE_PLUGIN_DOCKER_ECR_CACHE_ECR_NAME:-"$(get_default_image_name)"}"
2020
}
21+
22+
image_exists() {
23+
# TODO - implement check for cache in GCR
24+
false
25+
}

hooks/lib/stub-registry-provider.bash

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ get_registry_url() {
1515
compute_tag() {
1616
echo "stubbed-computed-tag"
1717
}
18+
19+
image_exists() {
20+
echo "stubbed image_exists"
21+
}

hooks/pre-command

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ read_build_args
3636
secrets_args=()
3737
read_secrets
3838

39+
if [ "${BUILDKITE_PLUGIN_DOCKER_ECR_CACHE_SKIP_PULL_FROM_CACHE:-}" == "true" ] && image_exists "$tag"; then
40+
echo "Image exists, skipping pull"
41+
exit 0;
42+
fi
43+
3944
echo "--- Pulling image"
45+
4046
if ! docker pull "${image}:${tag}"; then
4147
echo '--- Building image'
4248
image_build_args=(

plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@ configuration:
3333
type: string
3434
region:
3535
type: string
36+
skip-pull-from-cache:
37+
type: boolean
3638
required: []

tests/ecr-registry-provider.bats

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ pre_command_hook="$PWD/hooks/pre-command"
6969
"pull 1234567891012.dkr.ecr.ap-southeast-2.amazonaws.com/build-cache/example-org/example-pipeline:deadbee : echo not found && false" \
7070
"build --file=Dockerfile --tag=1234567891012.dkr.ecr.ap-southeast-2.amazonaws.com/build-cache/example-org/example-pipeline:deadbee . : echo building docker image" \
7171
"tag ${repository_uri}:deadbee ${repository_uri}:latest : echo tagged latest" \
72-
"push ${repository_uri}:deadbee : echo pushed deadbeef" \
72+
"push ${repository_uri}:deadbee : echo pushed deadbee" \
7373
"push ${repository_uri}:latest : echo pushed latest"
7474

7575
stub sha1sum \
7676
"Dockerfile : echo 'sha1sum(Dockerfile)'" \
7777
": echo sha1sum" \
7878
": echo sha1sum" \
79-
": echo deadbeef"
79+
": echo deadbee"
8080

8181
run "${pre_command_hook}"
8282

@@ -87,7 +87,7 @@ pre_command_hook="$PWD/hooks/pre-command"
8787
assert_output --partial "tag existing resource"
8888
assert_output --partial "put lifecycle policy"
8989
assert_output --partial "tagged latest"
90-
assert_output --partial "pushed deadbeef"
90+
assert_output --partial "pushed deadbee"
9191
assert_output --partial "pushed latest"
9292

9393
unstub aws
@@ -115,14 +115,14 @@ pre_command_hook="$PWD/hooks/pre-command"
115115
"pull 1234567891012.dkr.ecr.eu-west-1.amazonaws.com/build-cache/example-org/example-pipeline:deadbee : echo not found && false" \
116116
"build --file=Dockerfile --tag=1234567891012.dkr.ecr.eu-west-1.amazonaws.com/build-cache/example-org/example-pipeline:deadbee . : echo building docker image" \
117117
"tag ${repository_uri}:deadbee ${repository_uri}:latest : echo tagged latest" \
118-
"push ${repository_uri}:deadbee : echo pushed deadbeef" \
118+
"push ${repository_uri}:deadbee : echo pushed deadbee" \
119119
"push ${repository_uri}:latest : echo pushed latest"
120120

121121
stub sha1sum \
122122
"Dockerfile : echo 'sha1sum(Dockerfile)'" \
123123
": echo sha1sum" \
124124
": echo sha1sum" \
125-
": echo deadbeef"
125+
": echo deadbee"
126126

127127
run "${pre_command_hook}"
128128

@@ -133,7 +133,7 @@ pre_command_hook="$PWD/hooks/pre-command"
133133
assert_output --partial "tag existing resource"
134134
assert_output --partial "put lifecycle policy"
135135
assert_output --partial "tagged latest"
136-
assert_output --partial "pushed deadbeef"
136+
assert_output --partial "pushed deadbee"
137137
assert_output --partial "pushed latest"
138138

139139
unstub aws
@@ -163,14 +163,14 @@ pre_command_hook="$PWD/hooks/pre-command"
163163
"pull 1234567891012.dkr.ecr.ap-southeast-1.amazonaws.com/build-cache/example-org/example-pipeline:deadbee : echo not found && false" \
164164
"build --file=Dockerfile --tag=1234567891012.dkr.ecr.ap-southeast-1.amazonaws.com/build-cache/example-org/example-pipeline:deadbee . : echo building docker image" \
165165
"tag ${repository_uri}:deadbee ${repository_uri}:latest : echo tagged latest" \
166-
"push ${repository_uri}:deadbee : echo pushed deadbeef" \
166+
"push ${repository_uri}:deadbee : echo pushed deadbee" \
167167
"push ${repository_uri}:latest : echo pushed latest"
168168

169169
stub sha1sum \
170170
"Dockerfile : echo 'sha1sum(Dockerfile)'" \
171171
": echo sha1sum" \
172172
": echo sha1sum" \
173-
": echo deadbeef"
173+
": echo deadbee"
174174

175175
run "${pre_command_hook}"
176176

@@ -181,9 +181,48 @@ pre_command_hook="$PWD/hooks/pre-command"
181181
assert_output --partial "tag existing resource"
182182
assert_output --partial "put lifecycle policy"
183183
assert_output --partial "tagged latest"
184-
assert_output --partial "pushed deadbeef"
184+
assert_output --partial "pushed deadbee"
185185
assert_output --partial "pushed latest"
186186

187+
unstub aws
188+
unstub docker
189+
unstub sha1sum
190+
}
191+
192+
@test "ECR: Calls list-images to check existence of cache" {
193+
export AWS_DEFAULT_REGION="ap-southeast-2"
194+
export BUILDKITE_PLUGIN_DOCKER_ECR_CACHE_REGION="ap-southeast-1"
195+
export BUILDKITE_ORGANIZATION_SLUG="example-org"
196+
export BUILDKITE_PIPELINE_SLUG="example-pipeline"
197+
export BUILDKITE_PLUGIN_DOCKER_ECR_CACHE_SKIP_PULL_FROM_CACHE="true"
198+
local expected_repository_name="build-cache/example-org/example-pipeline"
199+
local repository_uri="1234567891012.dkr.ecr.ap-southeast-1.amazonaws.com/${expected_repository_name}"
200+
201+
stub aws \
202+
"sts get-caller-identity --query Account --output text : echo 1234567891012" \
203+
"ecr get-login-password --region ap-southeast-1 : echo secure-ecr-password" \
204+
"ecr describe-repositories --repository-names ${expected_repository_name} --output text --query repositories[0].registryId : echo looked up repository" \
205+
"ecr describe-repositories --repository-names ${expected_repository_name} --output text --query repositories[0].repositoryArn : echo arn:aws:ecr:ap-southeast-1:1234567891012:repository/${expected_repository_name}" \
206+
"ecr tag-resource --resource-arn arn:aws:ecr:ap-southeast-1:1234567891012:repository/build-cache/example-org/example-pipeline --cli-input-json \* : echo tag existing resource" \
207+
"ecr put-lifecycle-policy --repository-name build-cache/example-org/example-pipeline --lifecycle-policy-text \* : echo put lifecycle policy" \
208+
"ecr describe-repositories --repository-names ${expected_repository_name} --output text --query repositories[0].repositoryUri : echo ${repository_uri}" \
209+
"ecr list-images --repository-name ${expected_repository_name} --query imageIds[?imageTag==\'deadbee\'].imageTag --output text : echo 'deadbee'"
210+
211+
stub docker \
212+
"login --username AWS --password-stdin 1234567891012.dkr.ecr.ap-southeast-1.amazonaws.com : echo logging in to docker"
213+
214+
stub sha1sum \
215+
"Dockerfile : echo 'sha1sum(Dockerfile)'" \
216+
": echo sha1sum" \
217+
": echo sha1sum" \
218+
": echo deadbee"
219+
220+
run "${pre_command_hook}"
221+
222+
assert_success
223+
assert_output --partial "logging in to docker"
224+
assert_output --partial "looked up repository"
225+
assert_output --partial "Image exists, skipping pull"
187226
unstub aws
188227
unstub docker
189228
unstub sha1sum

tests/pre-command.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,14 @@ pre_command_hook="$PWD/hooks/pre-command"
9797
unstub mktemp
9898
unstub docker
9999
}
100+
101+
@test "Exits 0 if skip-pull-on-cache and image exists" {
102+
export BUILDKITE_PLUGIN_DOCKER_ECR_CACHE_REGISTRY_PROVIDER="stub"
103+
export BUILDKITE_PLUGIN_DOCKER_ECR_CACHE_SKIP_PULL_FROM_CACHE="true"
104+
local repository_uri="pretend.host/path/segment/image"
105+
106+
run "${pre_command_hook}"
107+
108+
assert_success
109+
assert_line "Image exists, skipping pull"
110+
}

0 commit comments

Comments
 (0)