Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose versionFingerprint instead of versionId from packer template #12803

Merged
merged 6 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions hcl2template/types.packer_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -109,24 +109,29 @@ 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, `\`, `/`)),
}),
},
}

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
Expand Down
3 changes: 2 additions & 1 deletion internal/hcp/registry/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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}"
}
}
Expand Down
Loading