Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build target flag for docker module #798

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions modules/docker/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type BuildOptions struct {
// Build args to pass the 'docker build' command
BuildArgs []string

// Target build arg to pass to the 'docker build' command
Target string

// Custom CLI options that will be passed as-is to the 'docker build' command. This is an "escape hatch" that allows
// Terratest to not have to support every single command-line option offered by the 'docker build' command, and
// solely focus on the most important ones.
Expand Down Expand Up @@ -61,6 +64,10 @@ func formatDockerBuildArgs(path string, options *BuildOptions) ([]string, error)
args = append(args, "--build-arg", arg)
}

if len(options.Target) > 0 {
args = append(args, "--target", options.Target)
}

args = append(args, options.OtherOptions...)

args = append(args, path)
Expand Down
19 changes: 19 additions & 0 deletions modules/docker/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,22 @@ func TestBuild(t *testing.T) {
out := Run(t, tag, &RunOptions{Remove: true})
require.Contains(t, out, text)
}

func TestBuildWithTarget(t *testing.T) {
t.Parallel()

tag := "gruntwork-io/test-image:target1"
text := "Hello, World!"
text1 := "Hello, World! This is build target 1!"

options := &BuildOptions{
Tags: []string{tag},
BuildArgs: []string{fmt.Sprintf("text=%s", text), fmt.Sprintf("text1=%s", text1)},
Target: "step1",
}

Build(t, "../../test/fixtures/docker", options)

out := Run(t, tag, &RunOptions{Remove: true})
require.Contains(t, out, text1)
}
7 changes: 6 additions & 1 deletion test/fixtures/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# A "Hello, World" Docker image used in automated tests for the docker.Build command.
FROM alpine:3.7
FROM alpine:3.7 as step1
ARG text1
RUN echo $text1 > text.txt
CMD ["cat", "text.txt"]

FROM step1
ARG text
RUN echo $text > text.txt
CMD ["cat", "text.txt"]