Skip to content

Commit

Permalink
Add support for build arg files in yaml format.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
featheredtoast committed Sep 13, 2024
1 parent 6763cba commit 82e0c08
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ require (
github.com/vbauerster/mpb v3.4.0+incompatible
github.com/vrischmann/envconfig v1.3.0
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
22 changes: 17 additions & 5 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package task
import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v3"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -374,13 +375,24 @@ func sanitize(cfg *Config) error {
return errors.Wrap(err, "read build args file")
}

for _, arg := range strings.Split(string(buildArgs), "\n") {
if len(arg) == 0 {
// skip blank lines
continue
if strings.HasSuffix(cfg.BuildArgsFile, ".yml") || strings.HasSuffix(cfg.BuildArgsFile, ".yaml") {
var buildArgsData map[string]string
err = yaml.Unmarshal(buildArgs, &buildArgsData)
if err != nil {
return errors.Wrap(err, "read build args yaml file")
}
for key, arg := range buildArgsData {
cfg.BuildArgs = append(cfg.BuildArgs, key + "=" + arg)
}
} else {
for _, arg := range strings.Split(string(buildArgs), "\n") {
if len(arg) == 0 {
// skip blank lines
continue
}

cfg.BuildArgs = append(cfg.BuildArgs, arg)
cfg.BuildArgs = append(cfg.BuildArgs, arg)
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ func (s *TaskSuite) TestBuildArgsFile() {
s.NoError(err)
}

func (s *TaskSuite) TestBuildArgsYamlFile() {
s.req.Config.ContextDir = "testdata/build-args"
s.req.Config.BuildArgsFile = "testdata/build-args/build_args_file.yaml"

// the Dockerfile itself asserts that the arg has been received
_, err := s.build()
s.NoError(err)
}

func (s *TaskSuite) TestBuildArgsStaticAndFile() {
s.req.Config.ContextDir = "testdata/build-args"
s.req.Config.BuildArgs = []string{"some_arg=some_value"}
Expand Down
2 changes: 2 additions & 0 deletions testdata/build-args/build_args_file.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
some_arg: some_value
some_other_arg: some_other_value

0 comments on commit 82e0c08

Please sign in to comment.