From bbd0e777b2d4884a16cc2cb0096feccb2739caa9 Mon Sep 17 00:00:00 2001 From: Rob Thorne Date: Mon, 24 May 2021 17:20:57 -0700 Subject: [PATCH 1/2] Improve documentation of docker buildArgs (#5871) - Fixes documentation issue where yaml syntax was not described and templating not mentioned. - Adds a unit test to verify that docker buildArgs support golang templating from the environment. --- docs/content/en/schemas/v2beta16.json | 2 +- pkg/skaffold/build/gcb/docker.go | 1 + pkg/skaffold/docker/build_args_test.go | 37 +++++++++++++++++++++++++ pkg/skaffold/schema/latest/v1/config.go | 2 +- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/docs/content/en/schemas/v2beta16.json b/docs/content/en/schemas/v2beta16.json index 283f0081f80..dd09e4f682a 100755 --- a/docs/content/en/schemas/v2beta16.json +++ b/docs/content/en/schemas/v2beta16.json @@ -1111,7 +1111,7 @@ "x-intellij-html-description": "arguments passed to the docker build.", "default": "{}", "examples": [ - "{\"key1\": \"value1\", \"key2\": \"value2\"}" + "{\"key1\": \"value1\", \"key2\": \"{{ .ENV_VAR }}\"}" ] }, "cacheFrom": { diff --git a/pkg/skaffold/build/gcb/docker.go b/pkg/skaffold/build/gcb/docker.go index ecaffb48c47..38b436fbf89 100644 --- a/pkg/skaffold/build/gcb/docker.go +++ b/pkg/skaffold/build/gcb/docker.go @@ -68,6 +68,7 @@ func (b *Builder) dockerBuildArgs(a *latestV1.Artifact, tag string, deps []*late return nil, errors.New("docker build options, secrets and ssh, are not currently supported in GCB builds") } requiredImages := docker.ResolveDependencyImages(deps, b.artifactStore, true) + buildArgs, err := docker.EvalBuildArgs(b.cfg.Mode(), a.Workspace, d.DockerfilePath, d.BuildArgs, requiredImages) if err != nil { return nil, fmt.Errorf("unable to evaluate build args: %w", err) diff --git a/pkg/skaffold/docker/build_args_test.go b/pkg/skaffold/docker/build_args_test.go index aee2d1291b0..120d5b9df0e 100644 --- a/pkg/skaffold/docker/build_args_test.go +++ b/pkg/skaffold/docker/build_args_test.go @@ -245,6 +245,43 @@ func TestCreateBuildArgsFromArtifacts(t *testing.T) { } } +func TestBuildArgTemplating(t *testing.T) { + envs := map[string]string{ + "MY_KEY": "abc123", + "SO_SECRET": "do not say it", + } + + args := map[string]*string{ + "MY_KEY": util.StringPtr("{{ .MY_KEY}}"), + "SO_SECRET": util.StringPtr("{{ .SO_SECRET}}"), + } + + dockerFile := ` + ARG MY_KEY + ARG SO_SECRET + ARG foo3 + ARG SKAFFOLD_GO_GCFLAGS + FROM bar1` + + testutil.Run(t, "test_templating_from_env_variables", func(t *testutil.T) { + t.SetEnvs(envs) + artifact := &latestV1.DockerArtifact{ + DockerfilePath: "Dockerfile", + BuildArgs: args, + } + + tmpDir := t.NewTempDir() + tmpDir.Write("./Dockerfile", dockerFile) + workspace := tmpDir.Path(".") + + filledMap, err := EvalBuildArgs(config.RunModes.Dev, workspace, artifact.DockerfilePath, artifact.BuildArgs, nil) + + t.CheckNil(err) + t.CheckMatches(*filledMap["MY_KEY"], "abc123") + t.CheckMatches(*filledMap["SO_SECRET"], "do not say it") + }) +} + type mockArtifactResolver struct { m map[string]string } diff --git a/pkg/skaffold/schema/latest/v1/config.go b/pkg/skaffold/schema/latest/v1/config.go index 173816ec41e..a71b68f5706 100644 --- a/pkg/skaffold/schema/latest/v1/config.go +++ b/pkg/skaffold/schema/latest/v1/config.go @@ -1205,7 +1205,7 @@ type DockerArtifact struct { Target string `yaml:"target,omitempty"` // BuildArgs are arguments passed to the docker build. - // For example: `{"key1": "value1", "key2": "value2"}`. + // For example: `{"key1": "value1", "key2": "{{ .ENV_VAR }}"}`. BuildArgs map[string]*string `yaml:"buildArgs,omitempty"` // NetworkMode is passed through to docker and overrides the From 23fcbbfb2561660e32f35d9de0a6e5811d52b07b Mon Sep 17 00:00:00 2001 From: Rob Thorne Date: Mon, 24 May 2021 18:49:25 -0700 Subject: [PATCH 2/2] Improve documentation of docker buildArgs (#5871) - Fixes documentation issue where yaml syntax was not described and templating not mentioned. - Adds a unit test to verify that docker buildArgs support golang templating from the environment. (revised: removed a blank line in an otherwise unchanged file). --- pkg/skaffold/build/gcb/docker.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/skaffold/build/gcb/docker.go b/pkg/skaffold/build/gcb/docker.go index 38b436fbf89..ecaffb48c47 100644 --- a/pkg/skaffold/build/gcb/docker.go +++ b/pkg/skaffold/build/gcb/docker.go @@ -68,7 +68,6 @@ func (b *Builder) dockerBuildArgs(a *latestV1.Artifact, tag string, deps []*late return nil, errors.New("docker build options, secrets and ssh, are not currently supported in GCB builds") } requiredImages := docker.ResolveDependencyImages(deps, b.artifactStore, true) - buildArgs, err := docker.EvalBuildArgs(b.cfg.Mode(), a.Workspace, d.DockerfilePath, d.BuildArgs, requiredImages) if err != nil { return nil, fmt.Errorf("unable to evaluate build args: %w", err)