diff --git a/hcl2template/testdata/build/provisioner_packer_version_interpolation.pkr.hcl b/hcl2template/testdata/build/provisioner_packer_version_interpolation.pkr.hcl new file mode 100644 index 000000000..01d93fa6c --- /dev/null +++ b/hcl2template/testdata/build/provisioner_packer_version_interpolation.pkr.hcl @@ -0,0 +1,15 @@ + +// starts resources to provision them. +build { + sources = [ + "source.virtualbox-iso.ubuntu-1204", + ] + + provisioner "shell" { + slice_string = ["{{packer_version}}"] + } +} + +source "virtualbox-iso" "ubuntu-1204" { +} + diff --git a/hcl2template/types.build.post-processor.go b/hcl2template/types.build.post-processor.go index ce2bf1592..bbf64a01a 100644 --- a/hcl2template/types.build.post-processor.go +++ b/hcl2template/types.build.post-processor.go @@ -68,6 +68,7 @@ func (cfg *PackerConfig) startPostProcessor(source SourceUseBlock, pp *PostProce } builderVars := source.builderVariables() + builderVars["packer_core_version"] = cfg.CorePackerVersionString builderVars["packer_debug"] = strconv.FormatBool(cfg.debug) builderVars["packer_force"] = strconv.FormatBool(cfg.force) builderVars["packer_on_error"] = cfg.onError diff --git a/hcl2template/types.build.provisioners.go b/hcl2template/types.build.provisioners.go index e5d3420e1..374e10cbf 100644 --- a/hcl2template/types.build.provisioners.go +++ b/hcl2template/types.build.provisioners.go @@ -156,6 +156,7 @@ func (cfg *PackerConfig) startProvisioner(source SourceUseBlock, pb *Provisioner } builderVars := source.builderVariables() + builderVars["packer_core_version"] = cfg.CorePackerVersionString builderVars["packer_debug"] = strconv.FormatBool(cfg.debug) builderVars["packer_force"] = strconv.FormatBool(cfg.force) builderVars["packer_on_error"] = cfg.onError diff --git a/hcl2template/types.build_test.go b/hcl2template/types.build_test.go index b2833375c..6415cf98b 100644 --- a/hcl2template/types.build_test.go +++ b/hcl2template/types.build_test.go @@ -421,6 +421,57 @@ func TestParse_build(t *testing.T) { }, false, }, + {"provisioner with packer_version interpolation", + defaultParser, + parseTestArgs{"testdata/build/provisioner_packer_version_interpolation.pkr.hcl", nil, nil}, + &PackerConfig{ + CorePackerVersionString: lockedVersion, + Basedir: filepath.Join("testdata", "build"), + Sources: map[SourceRef]SourceBlock{ + refVBIsoUbuntu1204: {Type: "virtualbox-iso", Name: "ubuntu-1204"}, + }, + Builds: Builds{ + &BuildBlock{ + Sources: []SourceUseBlock{ + { + SourceRef: refVBIsoUbuntu1204, + }, + }, + ProvisionerBlocks: []*ProvisionerBlock{ + { + PType: "shell", + }, + }, + }, + }, + }, + false, false, + []packersdk.Build{ + &packer.CoreBuild{ + Type: "virtualbox-iso.ubuntu-1204", + Prepared: true, + Builder: emptyMockBuilder, + Provisioners: []packer.CoreBuildProvisioner{ + { + PType: "shell", + Provisioner: &HCL2Provisioner{ + Provisioner: &MockProvisioner{ + Config: MockConfig{ + NestedMockConfig: NestedMockConfig{ + Tags: []MockTag{}, + SliceString: []string{lockedVersion}, + }, + NestedSlice: []NestedMockConfig{}, + }, + }, + }, + }, + }, + PostProcessors: [][]packer.CoreBuildPostProcessor{}, + }, + }, + false, + }, } testParse(t, tests) } diff --git a/hcl2template/types.source.go b/hcl2template/types.source.go index c6afca46d..f877e7fad 100644 --- a/hcl2template/types.source.go +++ b/hcl2template/types.source.go @@ -128,6 +128,7 @@ func (cfg *PackerConfig) startBuilder(source SourceUseBlock, ectx *hcl.EvalConte // prepare at a later step, to make builds from different template types // easier to reason about. builderVars := source.builderVariables() + builderVars["packer_core_version"] = cfg.CorePackerVersionString builderVars["packer_debug"] = strconv.FormatBool(cfg.debug) builderVars["packer_force"] = strconv.FormatBool(cfg.force) builderVars["packer_on_error"] = cfg.onError diff --git a/packer/core.go b/packer/core.go index 87da34ec2..ab5e9efe5 100644 --- a/packer/core.go +++ b/packer/core.go @@ -21,6 +21,7 @@ import ( "github.com/hashicorp/packer-plugin-sdk/template/interpolate" packerregistry "github.com/hashicorp/packer/internal/packer_registry" "github.com/hashicorp/packer/internal/packer_registry/env" + packerversion "github.com/hashicorp/packer/version" ) // Core is the main executor of Packer. If Packer is being used as a @@ -453,8 +454,9 @@ func (c *Core) Build(n string) (packersdk.Build, error) { // Context returns an interpolation context. func (c *Core) Context() *interpolate.Context { return &interpolate.Context{ - TemplatePath: c.Template.Path, - UserVariables: c.variables, + TemplatePath: c.Template.Path, + UserVariables: c.variables, + CorePackerVersionString: packerversion.FormattedVersion(), } } diff --git a/packer/core_test.go b/packer/core_test.go index 5c54b00f4..5e43b5e6f 100644 --- a/packer/core_test.go +++ b/packer/core_test.go @@ -11,6 +11,7 @@ import ( packersdk "github.com/hashicorp/packer-plugin-sdk/packer" "github.com/hashicorp/packer-plugin-sdk/template" configHelper "github.com/hashicorp/packer-plugin-sdk/template/config" + "github.com/hashicorp/packer/version" ) func TestCoreBuildNames(t *testing.T) { @@ -845,3 +846,30 @@ func TestCoreBuild_provRetry(t *testing.T) { t.Fatal("provisioner should retry for max_retries integer value") } } + +func TestCoreBuild_packerVersion(t *testing.T) { + config := TestCoreConfig(t) + testCoreTemplate(t, config, fixtureDir("build-var-packer-version.json")) + b := TestBuilder(t, config, "test") + core := TestCore(t, config) + + expected := version.FormattedVersion() + build, err := core.Build("test") + if err != nil { + t.Fatalf("err: %s", err) + } + + if _, err := build.Prepare(); err != nil { + t.Fatalf("err: %s", err) + } + // Interpolate the config + var result map[string]interface{} + err = configHelper.Decode(&result, nil, b.PrepareConfig...) + if err != nil { + t.Fatalf("err: %s", err) + } + + if result["value"] != expected { + t.Fatalf("bad: %#v", result) + } +} diff --git a/packer/test-fixtures/build-var-packer-version.json b/packer/test-fixtures/build-var-packer-version.json new file mode 100644 index 000000000..89ffe3923 --- /dev/null +++ b/packer/test-fixtures/build-var-packer-version.json @@ -0,0 +1,9 @@ +{ + "variables": { + "CreatedBy": "{{packer_version}}" + }, + "builders": [{ + "type": "test", + "value": "{{user `CreatedBy`}}" + }] +}