From 254e020b0802d31c49bb5b8a532f6bca5c72b635 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Mon, 11 Oct 2021 16:17:34 +0200 Subject: [PATCH] Allow to use build variables in a post processor (#11323) * hcl: add test to verify that build.name can be used in a pp * allow to use build vars in post processors --- command/build_test.go | 11 +++++++++++ command/test-fixtures/hcl/build-var-in-pp.pkr.hcl | 12 ++++++++++++ hcl2template/types.hcl_post-processor.go | 3 +++ 3 files changed, 26 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 49902744b..18158173a 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 000000000..96c36e77e --- /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 249e1e7c5..21627e4d9 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 {