Skip to content

Commit

Permalink
Improve documentation of docker buildArgs (#5871) (#5901)
Browse files Browse the repository at this point in the history
* 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.

* 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).
  • Loading branch information
torenware authored May 26, 2021
1 parent c788c2b commit 44cf6b5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/content/en/schemas/v2beta16.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
37 changes: 37 additions & 0 deletions pkg/skaffold/docker/build_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/schema/latest/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 44cf6b5

Please sign in to comment.