Skip to content

Add support for build arg files in yaml format. #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,29 @@ _(As a convention in the list below, all task parameters are specified with a
* `$BUILD_ARG_*`: params prefixed with `BUILD_ARG_` will be provided as build
args. For example `BUILD_ARG_foo=bar`, will set the `foo` build arg as `bar`.
* `$BUILD_ARGS_FILE` (default empty): path to a file containing build args in
the form `foo=bar`, one per line. Empty lines are skipped.
* `$BUILD_ARGS_FILE` (default empty): path to a file containing build args. By
default the task will assume each line is in the form `foo=bar`, one per
line. Empty lines are skipped. If the file ends in `yml` or `yaml` it will
be parsed as a YAML file. The YAML file can only contain string keys and
values.
Example file contents:
Example simple file contents:
```
EMAIL=me@yopmail.com
EMAIL=me@example.com
HOW_MANY_THINGS=1
DO_THING=false
```
Example YAML file contents:
```yaml
EMAIL: [email protected]
HOW_MANY_THINGS: "1"
DO_THING: "false"
MULTI_LINE_ARG: |
thing1
thing2
```

* `$BUILDKIT_SECRET_*`: files with extra secrets which are made available via
`--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.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ require (
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.25.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gopkg.in/yaml.v3 v3.0.1
)
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"
"os"
"os/exec"
Expand Down Expand Up @@ -373,13 +374,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
Loading