Skip to content

Commit 82e0c08

Browse files
Add support for build arg files in yaml format.
Adds key/value yaml files as possible input for build arg file. This allows for multiline args support in files. Signed-off-by: Jeff Wong <[email protected]>
1 parent 6763cba commit 82e0c08

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ require (
1616
github.com/vbauerster/mpb v3.4.0+incompatible
1717
github.com/vrischmann/envconfig v1.3.0
1818
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
19+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
1920
)

task.go

+17-5
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
"io/ioutil"
89
"os"
@@ -374,13 +375,24 @@ func sanitize(cfg *Config) error {
374375
return errors.Wrap(err, "read build args file")
375376
}
376377

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

383-
cfg.BuildArgs = append(cfg.BuildArgs, arg)
394+
cfg.BuildArgs = append(cfg.BuildArgs, arg)
395+
}
384396
}
385397
}
386398

task_test.go

+9
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"}
+2
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)