Skip to content

Commit

Permalink
Merge pull request concourse#186 from irfanhabib/master
Browse files Browse the repository at this point in the history
Add support for specifying a target build stage for multi-stage builds
  • Loading branch information
vito authored Apr 3, 2018
2 parents 4ddaf9f + 7827d34 commit f1ae287
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ version is the image's digest.
be tagged as `latest` in addition to whatever other tag was specified.

* `build_args`: *Optional.* A map of Docker build arguments.

* `target_name`: *Optional.* Specify the name of the target build stage.
Only supported for multi-stage Docker builds

Example:

Expand Down
9 changes: 8 additions & 1 deletion assets/out
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import_file=$(jq -r '.params.import_file // ""' < $payload)

pull_repository=$(jq -r '.params.pull_repository // ""' < $payload)
pull_tag=$(jq -r '.params.pull_tag // "latest"' < $payload)
target_name=$(jq -r '.params.target_name // ""' < $payload)

if [ -n "$load" ]; then
docker load -i "${load}/image"
Expand Down Expand Up @@ -167,6 +168,12 @@ elif [ -n "$build" ]; then
fi
fi

target=()
if [ -n "${target_name}" ]; then
target+=("--target")
target+=("${target_name}")
fi

ECR_REGISTRY_PATTERN='/[a-zA-Z0-9][a-zA-Z0-9_-]*\.dkr\.ecr\.[a-zA-Z0-9][a-zA-Z0-9_-]*\.amazonaws\.com(\.cn)?[^ ]*/'
ecr_images=$(grep '^\s*FROM' ${dockerfile} | \
awk "match(\$0,${ECR_REGISTRY_PATTERN}){print substr(\$0, RSTART, RLENGTH)}" )
Expand All @@ -182,7 +189,7 @@ elif [ -n "$build" ]; then
done
fi

docker build -t "${repository}:${tag_name}" "${expanded_build_args[@]}" -f "$dockerfile" $cache_from "$build"
docker build -t "${repository}:${tag_name}" "${target[@]}" "${expanded_build_args[@]}" -f "$dockerfile" $cache_from "$build"
elif [ -n "$load_file" ]; then
if [ -n "$load_repository" ]; then
docker load -i "$load_file"
Expand Down
17 changes: 17 additions & 0 deletions tests/out_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ var _ = Describe("Out", func() {
})
})

Context("when configured with a target build stage", func() {
It("passes it to dockerd", func() {
session := put(map[string]interface{}{
"source": map[string]interface{}{
"repository": "test",
},
"params": map[string]interface{}{
"target_name": "test",
"build": "/docker-image-resource/tests/fixtures/build",
},
})

Expect(session.Err).To(gbytes.Say(dockerarg(`--target`)))
Expect(session.Err).To(gbytes.Say(dockerarg(`test`)))
})
})

Context("When using ECR", func() {
It("calls docker pull with the ECR registry", func() {
session := put(map[string]interface{}{
Expand Down

0 comments on commit f1ae287

Please sign in to comment.