From 6aea8ead6a37ba086a1df85a3e977ed6a5dcb291 Mon Sep 17 00:00:00 2001 From: Adrienne Cohea Date: Sun, 17 May 2020 22:13:35 -0700 Subject: [PATCH] Support named builds in HCL2 templates. --- hcl2template/types.build.go | 6 ++++++ hcl2template/types.packer_config.go | 1 + packer/build.go | 4 ++++ 3 files changed, 11 insertions(+) diff --git a/hcl2template/types.build.go b/hcl2template/types.build.go index 09b663525..bf5802771 100644 --- a/hcl2template/types.build.go +++ b/hcl2template/types.build.go @@ -34,6 +34,9 @@ var buildSchema = &hcl.BodySchema{ // post-processor "" { ... } // } type BuildBlock struct { + // Name is a string representing the named build to show in the logs + Name string + // Sources is the list of sources that we want to start in this build block. Sources []SourceRef @@ -56,6 +59,7 @@ func (p *Parser) decodeBuildConfig(block *hcl.Block) (*BuildBlock, hcl.Diagnosti build := &BuildBlock{} var b struct { + Name string `hcl:"name,optional"` FromSources []string `hcl:"sources"` Config hcl.Body `hcl:",remain"` } @@ -64,6 +68,8 @@ func (p *Parser) decodeBuildConfig(block *hcl.Block) (*BuildBlock, hcl.Diagnosti return nil, diags } + build.Name = b.Name + for _, buildFrom := range b.FromSources { ref := sourceRefFromString(buildFrom) diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index 96293eb18..3477644f8 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -353,6 +353,7 @@ func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packer.Build } pcb := &packer.CoreBuild{ + BuildName: build.Name, Type: src.Type, Builder: builder, Provisioners: provisioners, diff --git a/packer/build.go b/packer/build.go index 007c6ed3b..b722cd8e2 100644 --- a/packer/build.go +++ b/packer/build.go @@ -86,6 +86,7 @@ type Build interface { // multiple files, of course, but it should be for only a single provider (such // as VirtualBox, EC2, etc.). type CoreBuild struct { + BuildName string Type string Builder Builder BuilderConfig interface{} @@ -128,6 +129,9 @@ type CoreBuildProvisioner struct { // Returns the name of the build. func (b *CoreBuild) Name() string { + if b.BuildName != "" { + return b.BuildName + } return b.Type }