diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index 10963226160..3ead0b6b5f1 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -9,7 +9,7 @@ import ( "strings" "github.com/gobwas/glob" - hcl "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/hcl/v2/hclsyntax" packersdk "github.com/hashicorp/packer-plugin-sdk/packer" @@ -109,10 +109,6 @@ func (cfg *PackerConfig) EvalContext(ctx BlockContext, variables map[string]cty. "name": cty.UnknownVal(cty.String), }), buildAccessor: cty.UnknownVal(cty.EmptyObject), - packerAccessor: cty.ObjectVal(map[string]cty.Value{ - "version": cty.StringVal(cfg.CorePackerVersionString), - "iterationID": cty.UnknownVal(cty.String), - }), pathVariablesAccessor: cty.ObjectVal(map[string]cty.Value{ "cwd": cty.StringVal(strings.ReplaceAll(cfg.Cwd, `\`, `/`)), "root": cty.StringVal(strings.ReplaceAll(cfg.Basedir, `\`, `/`)), @@ -120,13 +116,22 @@ func (cfg *PackerConfig) EvalContext(ctx BlockContext, variables map[string]cty. }, } + packerVars := map[string]cty.Value{ + "version": cty.StringVal(cfg.CorePackerVersionString), + "iterationID": cty.UnknownVal(cty.String), + "versionFingerprint": cty.UnknownVal(cty.String), + } + iterID, ok := cfg.HCPVars["iterationID"] if ok { - ectx.Variables[packerAccessor] = cty.ObjectVal(map[string]cty.Value{ - "version": cty.StringVal(cfg.CorePackerVersionString), - "iterationID": iterID, - }) + packerVars["iterationID"] = iterID } + versionFP, ok := cfg.HCPVars["versionFingerprint"] + if ok { + packerVars["versionFingerprint"] = versionFP + } + + ectx.Variables[packerAccessor] = cty.ObjectVal(packerVars) // In the future we'd like to load and execute HCL blocks using a graph // dependency tree, so that any block can use any block whatever the diff --git a/internal/hcp/registry/hcl.go b/internal/hcp/registry/hcl.go index e0af421ba38..23c5af7ccd9 100644 --- a/internal/hcp/registry/hcl.go +++ b/internal/hcp/registry/hcl.go @@ -47,10 +47,11 @@ func (h *HCLRegistry) PopulateVersion(ctx context.Context) error { } versionID := h.bucket.Version.ID + versionFingerprint := h.bucket.Version.Fingerprint // FIXME: Remove h.configuration.HCPVars["iterationID"] = cty.StringVal(versionID) - h.configuration.HCPVars["versionID"] = cty.StringVal(versionID) + h.configuration.HCPVars["versionFingerprint"] = cty.StringVal(versionFingerprint) sha, err := getGitSHA(h.configuration.Basedir) if err != nil { diff --git a/website/content/docs/templates/hcl_templates/contextual-variables.mdx b/website/content/docs/templates/hcl_templates/contextual-variables.mdx index b755e1082b7..03c04d82c86 100644 --- a/website/content/docs/templates/hcl_templates/contextual-variables.mdx +++ b/website/content/docs/templates/hcl_templates/contextual-variables.mdx @@ -132,23 +132,92 @@ parenthesis may through off your shell escaping otherwise. # HCP Packer Iteration ID +~> **Note**: The `packer.iterationID` variable is now deprecated and will be removed in a future version of Packer. HCP Packer Versions should be accessed with their fingerprint instead. The `packer.versionFingerprint` variable is now exposed to be used in its stead with the new HCP Packer data sources. + If your build is pushing metadata to the HCP Packer registry, this variable is set to the value of the Iteration ID associated with this run. ```hcl -source "amazon-ebs" "cannonical-ubuntu-server" { - ami_name = "packer-example" - // ... - run_volume_tags = { - hcp_iteration_id = packer.iterationID +source "null" "example" { + communicator = "none" +} + +data "hcp-packer-version" "hardened-source" { + bucket_name = "example" + channel_name = "latest" +} + +data "hcp-packer-artifact" "file" { + bucket_name = "example" + version_fingerprint = "${data.hcp-packer-version.hardened-source.fingerprint}" + platform = "aws" + region = "us-east-1" +} + +build { + hcp_packer_registry { + bucket_name = "simple" + } + sources = [ + "source.null.example" + ] + + provisioner "shell-local" { + inline = [ + "echo data is ${packer.iterationID}" + ] + } +} + +``` + +```shell-session +==> mybuild.null.example: Running local shell script: /var/folders/cz/q3cr3tld2457gtlgw7qs1kqc0000gq/T/packer-shell842419427 + mybuild.null.example: data is 01HN3KCRPVKR5PBQ28TS6B12W0 +``` + +# HCP Packer Version Fingerprint + +If your build is pushing metadata to the HCP Packer registry, this variable is +set to the value of the Version Fingerprint associated with this run. + +```hcl +source "null" "example" { + communicator = "none" +} + +data "hcp-packer-version" "hardened-source" { + bucket_name = "example" + channel_name = "latest" +} + +data "hcp-packer-artifact" "file" { + bucket_name = "example" + version_fingerprint = "${data.hcp-packer-version.hardened-source.fingerprint}" + platform = "aws" + region = "us-east-1" +} + +build { + hcp_packer_registry { + bucket_name = "simple" + } + sources = [ + "source.null.example" + ] + + provisioner "shell-local" { + inline = [ + "echo data is ${packer.versionFingerprint}" + ] } } + ``` ```shell-session -==> vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tags to source instance - vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tag: "Name": "Packer Builder" - vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tag: "hcp_iteration_id": "01FHGF3M2AK4TS6PCZES4VX5E7" +==> mybuild.null.example: Running local shell script: /var/folders/cz/q3cr3tld2457gtlgw7qs1kqc0000gq/T/packer-shell842419427 + mybuild.null.example: data is 01HN3KCRPVKR5PBQ28TS6B12W0 ``` You can also add this value to post-processors, for example to add to a manifest file: @@ -158,6 +227,7 @@ You can also add this value to post-processors, for example to add to a manifest output = "manifest.json" strip_path = true custom_data = { + version_fingerprint = "${packer.versionFingerprint}" iteration = "${packer.iterationID}" } }