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

Support buildkit for docker run #1199

Merged
merged 1 commit into from
Nov 16, 2022
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
16 changes: 16 additions & 0 deletions modules/docker/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ type BuildOptions struct {
// solely focus on the most important ones.
OtherOptions []string

// Whether ot not to enable buildkit. You can find more information about buildkit here https://docs.docker.com/build/buildkit/#getting-started.
EnableBuildKit bool

// Additional environment variables to pass in when running docker build command.
Env map[string]string

// Set a logger that should be used. See the logger package for more info.
Logger *logger.Logger
}
Expand All @@ -64,10 +70,20 @@ func Build(t testing.TestingT, path string, options *BuildOptions) {
func BuildE(t testing.TestingT, path string, options *BuildOptions) error {
options.Logger.Logf(t, "Running 'docker build' in %s", path)

env := make(map[string]string)
if options.Env != nil {
env = options.Env
}

if options.EnableBuildKit {
env["DOCKER_BUILDKIT"] = "1"
}

cmd := shell.Command{
Command: "docker",
Args: formatDockerBuildArgs(path, options),
Logger: options.Logger,
Env: env,
}

if err := shell.RunCommandE(t, cmd); err != nil {
Expand Down
17 changes: 17 additions & 0 deletions modules/docker/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ func TestBuild(t *testing.T) {
require.Contains(t, out, text)
}

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

tag := "gruntwork-io/test-image-with-buildkit:v1"
testToken := "testToken"
options := &BuildOptions{
Tags: []string{tag},
EnableBuildKit: true,
OtherOptions: []string{"--secret", fmt.Sprintf("id=github-token,env=%s", "GITHUB_OAUTH_TOKEN")},
Env: map[string]string{"GITHUB_OAUTH_TOKEN": testToken},
}

Build(t, "../../test/fixtures/docker-with-buildkit", options)
out := Run(t, tag, &RunOptions{Remove: false})
require.Contains(t, out, testToken)
}

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

Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/docker-with-buildkit/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# A "Hello, World" Docker image used in automated tests for the docker.Build command.
FROM ubuntu:20.04 as with-secrets

RUN --mount=type=secret,id=github-token echo "$(cat /run/secrets/github-token)" > text.txt
CMD ["cat", "text.txt"]