From 3310bc82c553dbeed3784bd2c4334a2019b68b9b Mon Sep 17 00:00:00 2001 From: James Kwon Date: Thu, 17 Nov 2022 11:29:51 -0500 Subject: [PATCH] support buildkit for dockcer-compose --- modules/docker/docker_compose.go | 13 ++++++++++ modules/docker/docker_compose_test.go | 26 +++++++++++++++++++ .../docker-compose-with-buildkit/Dockerfile | 5 ++++ .../bash_script.sh | 4 +++ .../docker-compose.yml | 11 ++++++++ 5 files changed, 59 insertions(+) create mode 100644 modules/docker/docker_compose_test.go create mode 100644 test/fixtures/docker-compose-with-buildkit/Dockerfile create mode 100755 test/fixtures/docker-compose-with-buildkit/bash_script.sh create mode 100644 test/fixtures/docker-compose-with-buildkit/docker-compose.yml diff --git a/modules/docker/docker_compose.go b/modules/docker/docker_compose.go index aaba564a9..2b48ffbcc 100644 --- a/modules/docker/docker_compose.go +++ b/modules/docker/docker_compose.go @@ -14,6 +14,10 @@ import ( type Options struct { WorkingDir string EnvVars map[string]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 + // Set a logger that should be used. See the logger package for more info. Logger *logger.Logger } @@ -46,6 +50,15 @@ func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args . result := icmd.RunCmd(dockerComposeVersionCmd) + if options.EnableBuildKit { + if options.EnvVars == nil { + options.EnvVars = make(map[string]string) + } + + options.EnvVars["DOCKER_BUILDKIT"] = "1" + options.EnvVars["COMPOSE_DOCKER_CLI_BUILD"] = "1" + } + if result.ExitCode == 0 { cmd = shell.Command{ Command: "docker", diff --git a/modules/docker/docker_compose_test.go b/modules/docker/docker_compose_test.go new file mode 100644 index 000000000..56e79e2fa --- /dev/null +++ b/modules/docker/docker_compose_test.go @@ -0,0 +1,26 @@ +package docker + +import ( + "github.com/stretchr/testify/require" + "testing" +) + +func TestDockerComposeWithBuildKit(t *testing.T) { + t.Parallel() + + testToken := "testToken" + dockerOptions := &Options{ + // Directory where docker-compose.yml lives + WorkingDir: "../../test/fixtures/docker-compose-with-buildkit", + + // Configure the port the web app will listen on and the text it will return using environment variables + EnvVars: map[string]string{ + "GITHUB_OAUTH_TOKEN": testToken, + }, + EnableBuildKit: true, + } + out := RunDockerCompose(t, dockerOptions, "build", "--no-cache") + out = RunDockerCompose(t, dockerOptions, "up") + + require.Contains(t, out, testToken) +} diff --git a/test/fixtures/docker-compose-with-buildkit/Dockerfile b/test/fixtures/docker-compose-with-buildkit/Dockerfile new file mode 100644 index 000000000..6f4648ad8 --- /dev/null +++ b/test/fixtures/docker-compose-with-buildkit/Dockerfile @@ -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 +COPY ./bash_script.sh /usr/local/bin/bash_script.sh \ No newline at end of file diff --git a/test/fixtures/docker-compose-with-buildkit/bash_script.sh b/test/fixtures/docker-compose-with-buildkit/bash_script.sh new file mode 100755 index 000000000..38e4b6b3c --- /dev/null +++ b/test/fixtures/docker-compose-with-buildkit/bash_script.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e + +cat text.txt diff --git a/test/fixtures/docker-compose-with-buildkit/docker-compose.yml b/test/fixtures/docker-compose-with-buildkit/docker-compose.yml new file mode 100644 index 000000000..6757d33fd --- /dev/null +++ b/test/fixtures/docker-compose-with-buildkit/docker-compose.yml @@ -0,0 +1,11 @@ +services: + test-docker-image: + build: + context: . + secrets: + - github-token + entrypoint: bash_script.sh + +secrets: + github-token: + environment: GITHUB_OAUTH_TOKEN \ No newline at end of file