From 9d66a9a8924ab789192415cc981af699eabfd798 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Mon, 11 Oct 2021 14:30:14 +0200 Subject: [PATCH 1/2] hcl: add test to verify that build.name can be used in a pp --- command/build_test.go | 11 +++++++++++ command/test-fixtures/hcl/build-var-in-pp.pkr.hcl | 12 ++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 command/test-fixtures/hcl/build-var-in-pp.pkr.hcl 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"] + } +} From a861a5cd99275ea7f2c9c9f2ccd5ed6eed995561 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Mon, 11 Oct 2021 14:31:40 +0200 Subject: [PATCH 2/2] allow to use build vars in post processors --- hcl2template/types.hcl_post-processor.go | 3 +++ 1 file changed, 3 insertions(+) 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 {