Skip to content

Commit

Permalink
Support buildkit for docker run
Browse files Browse the repository at this point in the history
  • Loading branch information
james03160927 committed Nov 15, 2022
1 parent 4694196 commit c6920dd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
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"]

0 comments on commit c6920dd

Please sign in to comment.