mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-23 12:17:45 +08:00
Merge pull request #532 from tonistiigi/vars-in-func
bake: allow variables in user functions
This commit is contained in:
commit
ad95d6ba04
19
bake/hcl.go
19
bake/hcl.go
@ -167,15 +167,6 @@ func parseHCL(dt []byte, fn string) (_ *Config, err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
userFunctions, _, diags := userfunc.DecodeUserFunctions(file.Body, "function", func() *hcl.EvalContext {
|
|
||||||
return &hcl.EvalContext{
|
|
||||||
Functions: stdlibFunctions,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if diags.HasErrors() {
|
|
||||||
return nil, diags
|
|
||||||
}
|
|
||||||
|
|
||||||
var sc staticConfig
|
var sc staticConfig
|
||||||
|
|
||||||
// Decode only variable blocks without interpolation.
|
// Decode only variable blocks without interpolation.
|
||||||
@ -189,6 +180,16 @@ func parseHCL(dt []byte, fn string) (_ *Config, err error) {
|
|||||||
variables[variable.Name] = cty.StringVal(variable.Default)
|
variables[variable.Name] = cty.StringVal(variable.Default)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
userFunctions, _, diags := userfunc.DecodeUserFunctions(file.Body, "function", func() *hcl.EvalContext {
|
||||||
|
return &hcl.EvalContext{
|
||||||
|
Functions: stdlibFunctions,
|
||||||
|
Variables: variables,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if diags.HasErrors() {
|
||||||
|
return nil, diags
|
||||||
|
}
|
||||||
|
|
||||||
// Override default with values from environment.
|
// Override default with values from environment.
|
||||||
for _, env := range os.Environ() {
|
for _, env := range os.Environ() {
|
||||||
parts := strings.SplitN(env, "=", 2)
|
parts := strings.SplitN(env, "=", 2)
|
||||||
|
@ -250,4 +250,36 @@ func TestParseHCL(t *testing.T) {
|
|||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Contains(t, err.Error(), "docker-bake.hcl:7,17-37: Variables not allowed; Variables may not be used here.")
|
require.Contains(t, err.Error(), "docker-bake.hcl:7,17-37: Variables not allowed; Variables may not be used here.")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("WithVariablesInFunctions", func(t *testing.T) {
|
||||||
|
dt := []byte(`
|
||||||
|
variable "REPO" {
|
||||||
|
default = "user/repo"
|
||||||
|
}
|
||||||
|
function "tag" {
|
||||||
|
params = [tag]
|
||||||
|
result = ["${REPO}:${tag}"]
|
||||||
|
}
|
||||||
|
|
||||||
|
target "webapp" {
|
||||||
|
tags = tag("v1")
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
|
||||||
|
c, err := ParseHCL(dt, "docker-bake.hcl")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.Equal(t, 1, len(c.Targets))
|
||||||
|
require.Equal(t, c.Targets[0].Name, "webapp")
|
||||||
|
require.Equal(t, []string{"user/repo:v1"}, c.Targets[0].Tags)
|
||||||
|
|
||||||
|
os.Setenv("REPO", "docker/buildx")
|
||||||
|
|
||||||
|
c, err = ParseHCL(dt, "docker-bake.hcl")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.Equal(t, 1, len(c.Targets))
|
||||||
|
require.Equal(t, c.Targets[0].Name, "webapp")
|
||||||
|
require.Equal(t, []string{"docker/buildx:v1"}, c.Targets[0].Tags)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user