diff --git a/command/build_test.go b/command/build_test.go index ecda6358eae..00229c64b16 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -415,6 +415,17 @@ func TestBuild(t *testing.T) { }, }, }, + { + name: "hcl - using build variables in post-processor", + args: []string{ + testFixture("hcl", "build-var-in-pp.pkr.hcl"), + }, + fileCheck: fileCheck{ + expectedContent: map[string]string{ + "example.2.txt": two, + }, + }, + }, } for _, tt := range tc { diff --git a/command/test-fixtures/hcl/build-var-in-pp.pkr.hcl b/command/test-fixtures/hcl/build-var-in-pp.pkr.hcl new file mode 100644 index 00000000000..96c36e77e64 --- /dev/null +++ b/command/test-fixtures/hcl/build-var-in-pp.pkr.hcl @@ -0,0 +1,12 @@ +source "null" "example" { + communicator = "none" +} + +build { + name = "example" + sources = ["source.null.example"] + + post-processor "shell-local" { + inline = ["echo 2 > ${build.name}.2.txt"] + } +} diff --git a/hcl2template/types.hcl_post-processor.go b/hcl2template/types.hcl_post-processor.go index 249e1e7c50b..21627e4d993 100644 --- a/hcl2template/types.hcl_post-processor.go +++ b/hcl2template/types.hcl_post-processor.go @@ -31,6 +31,9 @@ func (p *HCL2PostProcessor) HCL2Prepare(buildVars map[string]interface{}) error if len(buildVars) > 0 { ectx = p.evalContext.NewChild() buildValues := map[string]cty.Value{} + if !p.evalContext.Variables[buildAccessor].IsNull() { + buildValues = p.evalContext.Variables[buildAccessor].AsValueMap() + } for k, v := range buildVars { val, err := ConvertPluginConfigValueToHCLValue(v) if err != nil {