Skip to content

Commit 4203315

Browse files
authored
Merge pull request #123 from featheredtoast/add-yaml-build-args-file
Add support for build arg files in yaml format.
2 parents a7b2102 + f288da4 commit 4203315

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,29 @@ _(As a convention in the list below, all task parameters are specified with a
8080
* `$BUILD_ARG_*`: params prefixed with `BUILD_ARG_` will be provided as build
8181
args. For example `BUILD_ARG_foo=bar`, will set the `foo` build arg as `bar`.
8282
83-
* `$BUILD_ARGS_FILE` (default empty): path to a file containing build args in
84-
the form `foo=bar`, one per line. Empty lines are skipped.
83+
* `$BUILD_ARGS_FILE` (default empty): path to a file containing build args. By
84+
default the task will assume each line is in the form `foo=bar`, one per
85+
line. Empty lines are skipped. If the file ends in `yml` or `yaml` it will
86+
be parsed as a YAML file. The YAML file can only contain string keys and
87+
values.
8588
86-
Example file contents:
89+
Example simple file contents:
8790
8891
```
89-
EMAIL=me@yopmail.com
92+
EMAIL=me@example.com
9093
HOW_MANY_THINGS=1
9194
DO_THING=false
9295
```
96+
Example YAML file contents:
97+
98+
```yaml
99+
EMAIL: me@example.com
100+
HOW_MANY_THINGS: "1"
101+
DO_THING: "false"
102+
MULTI_LINE_ARG: |
103+
thing1
104+
thing2
105+
```
93106

94107
* `$BUILDKIT_SECRET_*`: files with extra secrets which are made available via
95108
`--mount=type=secret,id=...`. See [New Docker Build secret information](https://docs.docker.com/develop/develop-images/build_enhancements/#new-docker-build-secret-information) for more information on build secrets.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ require (
3838
golang.org/x/sync v0.8.0 // indirect
3939
golang.org/x/sys v0.26.0 // indirect
4040
golang.org/x/term v0.25.0 // indirect
41-
gopkg.in/yaml.v3 v3.0.1 // indirect
41+
gopkg.in/yaml.v3 v3.0.1
4242
)

task.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package task
33
import (
44
"encoding/json"
55
"fmt"
6+
"gopkg.in/yaml.v3"
67
"io"
78
"os"
89
"os/exec"
@@ -373,13 +374,24 @@ func sanitize(cfg *Config) error {
373374
return errors.Wrap(err, "read build args file")
374375
}
375376

376-
for _, arg := range strings.Split(string(buildArgs), "\n") {
377-
if len(arg) == 0 {
378-
// skip blank lines
379-
continue
377+
if strings.HasSuffix(cfg.BuildArgsFile, ".yml") || strings.HasSuffix(cfg.BuildArgsFile, ".yaml") {
378+
var buildArgsData map[string]string
379+
err = yaml.Unmarshal(buildArgs, &buildArgsData)
380+
if err != nil {
381+
return errors.Wrap(err, "read build args yaml file")
380382
}
383+
for key, arg := range buildArgsData {
384+
cfg.BuildArgs = append(cfg.BuildArgs, key + "=" + arg)
385+
}
386+
} else {
387+
for _, arg := range strings.Split(string(buildArgs), "\n") {
388+
if len(arg) == 0 {
389+
// skip blank lines
390+
continue
391+
}
381392

382-
cfg.BuildArgs = append(cfg.BuildArgs, arg)
393+
cfg.BuildArgs = append(cfg.BuildArgs, arg)
394+
}
383395
}
384396
}
385397

task_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ func (s *TaskSuite) TestBuildArgsFile() {
152152
s.NoError(err)
153153
}
154154

155+
func (s *TaskSuite) TestBuildArgsYamlFile() {
156+
s.req.Config.ContextDir = "testdata/build-args"
157+
s.req.Config.BuildArgsFile = "testdata/build-args/build_args_file.yaml"
158+
159+
// the Dockerfile itself asserts that the arg has been received
160+
_, err := s.build()
161+
s.NoError(err)
162+
}
163+
155164
func (s *TaskSuite) TestBuildArgsStaticAndFile() {
156165
s.req.Config.ContextDir = "testdata/build-args"
157166
s.req.Config.BuildArgs = []string{"some_arg=some_value"}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
some_arg: some_value
2+
some_other_arg: some_other_value

0 commit comments

Comments
 (0)