Skip to content

Commit

Permalink
Add target as an attribute to dockerfile_build (bazelbuild#1944)
Browse files Browse the repository at this point in the history
* Buildifer
  • Loading branch information
jakegut authored Nov 22, 2021
1 parent 84043cc commit 61e2df8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ dockerfile_image(
"ALPINE_version": "3.9",
},
dockerfile = "//testdata/dockerfile_build:Dockerfile",
target = "test",
vars = [
"SOME_VAR",
],
Expand Down
8 changes: 7 additions & 1 deletion contrib/dockerfile_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _docker(repository_ctx):
repository_ctx: The repository context
Returns:
Tha path to the docker tool
The path to the docker tool
"""

if repository_ctx.attr.docker_path:
Expand Down Expand Up @@ -75,6 +75,9 @@ def _impl(repository_ctx):
str(dockerfile_path.dirname),
])

if repository_ctx.attr.target:
command.extend(["--target", repository_ctx.attr.target])

build_result = repository_ctx.execute(command)
if build_result.return_code:
fail("docker build command failed: {} ({})".format(
Expand Down Expand Up @@ -120,6 +123,9 @@ dockerfile_image = repository_rule(
"vars": attr.string_list(
doc = "List of environment vars to include in the build.",
),
"target": attr.string(
doc = "Specify which intermediate stage to finish at, passed to `--target`.",
),
},
implementation = _impl,
)
10 changes: 9 additions & 1 deletion testdata/dockerfile_build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ARG ALPINE_version
ARG SOME_VAR=default_value

FROM alpine:${ALPINE_version}
FROM alpine:${ALPINE_version} AS base
ENTRYPOINT ["echo"]
CMD ["Hello World!"]

Expand All @@ -19,3 +19,11 @@ EXPOSE 9876/udp
VOLUME /myVol1 /usr/myVol2

RUN apk add gcc python2

FROM base AS test

RUN echo "Hello from test stage" > /target_test.txt

FROM base

RUN echo "A file that should not exist" > /target_test_base.txt
9 changes: 9 additions & 0 deletions tests/contrib/configs/dockerfile_image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ fileExistenceTests:
- name: 'copy_txt_file_exists'
path: '/file_to_copy.txt'
shouldExist: true
- name: 'target_txt_exists'
path: '/target_test.txt'
shouldExist: true
- name: 'base_txt_not_exists'
path: '/target_test_base.txt'
shouldExist: false

fileContentTests:
- name: "Alpine version check"
Expand All @@ -42,6 +48,9 @@ fileContentTests:
- name: 'copy_txt_file_contains'
path: '/file_to_copy.txt'
expectedContents: ['Another file to copy']
- name: 'target_txt_contains'
path: '/target_test.txt'
expectedContents: ['Hello from test stage']

commandTests:
- name: "gcc_test"
Expand Down

0 comments on commit 61e2df8

Please sign in to comment.