mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-04 10:03:42 +08:00 
			
		
		
		
	bake: allow variables in user functions
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
		
							
								
								
									
										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)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user