diff --git a/bake/hcl.go b/bake/hcl.go index 714fbb2f..c279b6a9 100644 --- a/bake/hcl.go +++ b/bake/hcl.go @@ -1,10 +1,27 @@ package bake -import "github.com/hashicorp/hcl/v2/hclsimple" +import ( + "os" + "strings" + + hcl "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/hcl/v2/hclsimple" + "github.com/zclconf/go-cty/cty" +) func ParseHCL(dt []byte, fn string) (*Config, error) { + variables := make(map[string]cty.Value) + for _, env := range os.Environ() { + parts := strings.SplitN(env, "=", 2) + variables[parts[0]] = cty.StringVal(parts[1]) + } + + ctx := &hcl.EvalContext{ + Variables: variables, + } + var c Config - if err := hclsimple.Decode(fn, dt, nil, &c); err != nil { + if err := hclsimple.Decode(fn, dt, ctx, &c); err != nil { return nil, err } return &c, nil diff --git a/bake/hcl_test.go b/bake/hcl_test.go index ec6e2bc3..e34ca3fb 100644 --- a/bake/hcl_test.go +++ b/bake/hcl_test.go @@ -1,12 +1,15 @@ package bake import ( + "os" "testing" "github.com/stretchr/testify/require" ) func TestParseHCL(t *testing.T) { + os.Setenv("IAMCROSS", "true") + var dt = []byte(` group "default" { targets = ["db", "webapp"] @@ -35,7 +38,7 @@ func TestParseHCL(t *testing.T) { target "webapp-plus" { inherits = ["webapp", "cross"] args = { - IAMCROSS = "true" + IAMCROSS = "${IAMCROSS}" } } `) @@ -58,4 +61,8 @@ func TestParseHCL(t *testing.T) { require.Equal(t, c.Targets[2].Name, "cross") require.Equal(t, 2, len(c.Targets[2].Platforms)) require.Equal(t, []string{"linux/amd64", "linux/arm64"}, c.Targets[2].Platforms) + + require.Equal(t, c.Targets[3].Name, "webapp-plus") + require.Equal(t, 1, len(c.Targets[3].Args)) + require.Equal(t, map[string]string{"IAMCROSS": "true"}, c.Targets[3].Args) } diff --git a/go.mod b/go.mod index 3d985282..e562b021 100644 --- a/go.mod +++ b/go.mod @@ -55,6 +55,7 @@ require ( github.com/theupdateframework/notary v0.6.1 // indirect github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea github.com/xlab/handysort v0.0.0-20150421192137-fb3537ed64a1 // indirect + github.com/zclconf/go-cty v1.2.0 golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e gopkg.in/dancannon/gorethink.v3 v3.0.5 // indirect gopkg.in/fatih/pool.v2 v2.0.0 // indirect