implement packer_version variable in hcl

This commit is contained in:
Megan Marsh 2020-10-15 16:30:46 -07:00
parent 2cdc052d7c
commit 09c6d2880f
5 changed files with 56 additions and 7 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/version"
"github.com/zclconf/go-cty/cty"
)
@ -60,6 +61,7 @@ const (
pathVariablesAccessor = "path"
sourcesAccessor = "source"
buildAccessor = "build"
packerAccessor = "packer"
)
// EvalContext returns the *hcl.EvalContext that will be passed to an hcl
@ -78,6 +80,9 @@ func (cfg *PackerConfig) EvalContext(variables map[string]cty.Value) *hcl.EvalCo
"name": cty.UnknownVal(cty.String),
}),
buildAccessor: cty.UnknownVal(cty.EmptyObject),
packerAccessor: cty.ObjectVal(map[string]cty.Value{
"version": cty.StringVal(version.FormattedVersion()),
}),
pathVariablesAccessor: cty.ObjectVal(map[string]cty.Value{
"cwd": cty.StringVal(strings.ReplaceAll(cfg.Cwd, `\`, `/`)),
"root": cty.StringVal(strings.ReplaceAll(cfg.Basedir, `\`, `/`)),

View File

@ -13,7 +13,6 @@ description: |-
# Build Variables
Build variables will allow you to access connection information and basic instance state information for a builder.
All special build variables are stored in the `build` variable:
@ -72,3 +71,48 @@ For builder-specific builder variables, please also refer to the builder docs:
The HCL2 Special Build Variables is in beta; please report any issues or requests on the Packer
issue tracker on GitHub.
# Packer Version
This variable is set to the Packer version currently running.
```hcl
source "null" "first-example" {
communicator = "none"
}
build {
sources = ["null.first-example"]
provisioner "shell-local" {
inline = ["echo packer_version is '${packer.version}'"]
}
}
```
If you are running a development version of Packer, the version variable will
contain the released version number, dev flag, and current commit.
```shell-session
PACKER_LOG=0 packer build packer_version_demo.pkr.hcl
null.first-example: output will be in this color.
==> null.first-example: Running local shell script: /var/folders/8t/0yb5q0_x6mb2jldqq_vjn3lr0000gn/T/packer-shell083160352
null.first-example: packer_version is 1.6.5-dev (a69392129+CHANGES)
```
If you are running a released version of Packer, the version variable will
contain the released version number only:
```shell-session
PACKER_LOG=0 packer build packer_version_demo.pkr.hcl
null.first-example: output will be in this color.
==> null.first-example: Running local shell script: /var/folders/8t/0yb5q0_x6mb2jldqq_vjn3lr0000gn/T/packer-shell718995312
null.first-example: packer_version is 1.6.5
```
Make sure to wrap your variable in single quotes in order to escape the
string that is returned; if you are running a dev version of packer the
parenthesis may through off your shell escaping otherwise.

View File

@ -0,0 +1,6 @@
---
layout: docs
page_title: Contextual - Functions - Configuration Language
sidebar_title: Contextual Functions
description: Overview of available Contextual functions
---

View File

@ -1,6 +0,0 @@
---
layout: docs
page_title: vault - Functions - Configuration Language
sidebar_title: vault Functions
description: Overview of available vault functions
---