Skip to content

Commit

Permalink
Allow to use build variables in a post processor (#11323)
Browse files Browse the repository at this point in the history
* hcl: add test to verify that build.name can be used in a pp

* allow to use build vars in post processors
  • Loading branch information
azr authored Oct 11, 2021
1 parent e99e038 commit 254e020
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions command/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions command/test-fixtures/hcl/build-var-in-pp.pkr.hcl
Original file line number Diff line number Diff line change
@@ -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"]
}
}
3 changes: 3 additions & 0 deletions hcl2template/types.hcl_post-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 254e020

Please sign in to comment.