diff --git a/assets/common.sh b/assets/common.sh index 5e6b1851..c866b37c 100644 --- a/assets/common.sh +++ b/assets/common.sh @@ -197,7 +197,7 @@ docker_pull() { if docker pull "$1"; then printf "\nSuccessfully pulled ${GREEN}%s${NC}.\n\n" "$1" - return + return 0 fi echo @@ -206,5 +206,5 @@ docker_pull() { done printf "\n${RED}Failed to pull image %s.${NC}" "$1" - exit 1 + return 1 } diff --git a/assets/out b/assets/out index 83b761fc..d4c0278a 100755 --- a/assets/out +++ b/assets/out @@ -147,8 +147,9 @@ elif [ -n "$build" ]; then cache_from_args=() if [ "$cache" = "true" ]; then - docker_pull "${repository}:${cache_tag}" - cache_from_args+=("--cache-from ${repository}:${cache_tag}") + if docker_pull "${repository}:${cache_tag}"; then + cache_from_args+=("--cache-from ${repository}:${cache_tag}") + fi fi if [ -n "$cache_from" ]; then diff --git a/tests/fixtures/bin/docker b/tests/fixtures/bin/docker index 8e53c9a3..db02bd3f 100755 --- a/tests/fixtures/bin/docker +++ b/tests/fixtures/bin/docker @@ -14,3 +14,7 @@ fi if [ "$1" == "info" ] && [ -f /tmp/docker_failing ]; then exit 1 fi + +if [ "$1" == "pull" ] && [ "$2" == "broken-repo:latest" ]; then + exit 1 +fi diff --git a/tests/out_test.go b/tests/out_test.go index aa140d8c..ebe9045c 100644 --- a/tests/out_test.go +++ b/tests/out_test.go @@ -539,6 +539,39 @@ var _ = Describe("Out", func() { }) }) + Context("when cache is specified are specified", func() { + It("adds argument to cache_from", func() { + session := put(map[string]interface{}{ + "source": map[string]interface{}{ + "repository": "test", + }, + "params": map[string]interface{}{ + "build": "/docker-image-resource/tests/fixtures/build", + "cache": "true", + }, + }) + Expect(session.Err).To(gbytes.Say(dockerarg(`--cache-from`))) + Expect(session.Err).To(gbytes.Say(dockerarg(`test:latest`))) + }) + + It("does not add cache_from if pull fails", func() { + session := put(map[string]interface{}{ + "source": map[string]interface{}{ + "repository": "broken-repo", + }, + "params": map[string]interface{}{ + "build": "/docker-image-resource/tests/fixtures/build", + "cache": "true", + }, + }) + + Expect(session.Err).ToNot(gbytes.Say(dockerarg(`--cache-from`))) + Expect(session.Err).To(gbytes.Say(dockerarg(`broken-repo:latest`))) + Expect(session.Err).To(gbytes.Say(dockerarg(`build`))) + + }) + }); + Context("when cache_from images are specified", func() { BeforeEach(func() { os.Mkdir("/tmp/cache_from_1", os.ModeDir) @@ -564,7 +597,7 @@ var _ = Describe("Out", func() { session := put(map[string]interface{}{ "source": map[string]interface{}{ "repository": "test", - }, + }, "params": map[string]interface{}{ "build": "/docker-image-resource/tests/fixtures/build", "cache_from": []string{"cache_from_1", "cache_from_2"},