diff --git a/hcl2template/testdata/build/named.pkr.hcl b/hcl2template/testdata/build/named.pkr.hcl new file mode 100644 index 000000000..ae6dca9b8 --- /dev/null +++ b/hcl2template/testdata/build/named.pkr.hcl @@ -0,0 +1,8 @@ +build { + name = "somebuild" + + sources = [ + "source.amazon-ebs.ubuntu-1604", + "source.virtualbox-iso.ubuntu-1204", + ] +} diff --git a/hcl2template/types.build.go b/hcl2template/types.build.go index 30705c55b..829038c85 100644 --- a/hcl2template/types.build.go +++ b/hcl2template/types.build.go @@ -35,6 +35,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 @@ -57,6 +60,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,optional"` Config hcl.Body `hcl:",remain"` } @@ -65,6 +69,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.build_test.go b/hcl2template/types.build_test.go index 7d45d022d..9b3c71161 100644 --- a/hcl2template/types.build_test.go +++ b/hcl2template/types.build_test.go @@ -100,6 +100,28 @@ func TestParse_build(t *testing.T) { []packer.Build{}, false, }, + {"named build", + defaultParser, + parseTestArgs{"testdata/build/named.pkr.hcl", nil, nil}, + &PackerConfig{ + Basedir: filepath.Join("testdata", "build"), + Builds: Builds{ + &BuildBlock{ + Name: "somebuild", + Sources: []SourceRef{ + { + Type: "amazon-ebs", + Name: "ubuntu-1604", + }, + refVBIsoUbuntu1204, + }, + }, + }, + }, + false, false, + []packer.Build{}, + true, + }, } testParse(t, tests) } diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index 2a6550b7f..81eb4bad8 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -355,6 +355,7 @@ func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packer.Build } pcb := &packer.CoreBuild{ + BuildName: build.Name, Type: src.Ref().String(), Builder: builder, Provisioners: provisioners, diff --git a/packer/build.go b/packer/build.go index 53c90490f..d3bb2e806 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 + "." + b.Type + } return b.Type } diff --git a/website/pages/docs/from-1.5/blocks/build/index.mdx b/website/pages/docs/from-1.5/blocks/build/index.mdx index 10e6b53ec..c05377e46 100644 --- a/website/pages/docs/from-1.5/blocks/build/index.mdx +++ b/website/pages/docs/from-1.5/blocks/build/index.mdx @@ -13,6 +13,13 @@ sidebar_title: build The `build` block defines what builders are started, how to `provision` them and if necessary what to do with their artifacts using `post-process`. +To use builders in a `build` block you can either: + +* Set the `sources` array of string with references to pre-defined sources. + +* Define [build-level `source` blocks](/docs/from-1.5/blocks/build/source) or +`sources` to use builders. This also allows you to set specific fields. + `@include 'from-1.5/builds/example-block.mdx'` @@ -20,12 +27,6 @@ Define [top-level `source` blocks](/docs/from-1.5/blocks/source) to configure your builders. The list of available builders can be found in the [builders](/docs/builders) section. -To use builders in a `build` block you can either: - -* Set the `sources` array of string with references to defined sources. - -* Define [build-level `source` blocks](/docs/from-1.5/blocks/build/source) or -`sources` to use builders. This also allows you to set specific fields. ## Related diff --git a/website/pages/partials/from-1.5/builds/example-block.mdx b/website/pages/partials/from-1.5/builds/example-block.mdx index 811ca559c..4ac0af5ef 100644 --- a/website/pages/partials/from-1.5/builds/example-block.mdx +++ b/website/pages/partials/from-1.5/builds/example-block.mdx @@ -1,16 +1,19 @@ ```hcl # build.pkr.hcl build { + # use the `name` field to name a build in the logs. + # For example this present config will display + # "buildname.amazon-ebs.example-1" and "buildname.amazon-ebs.example-2" + name = "buildname" sources = [ - # use the plural `sources` block to simply use sources + # use the optional plural `sources` list to simply use a `source` # without changing any field. - "source.amazon-ebs.example", + "source.amazon-ebs.example-1", ] - source "source.amazon-ebs.example" { - # Use the singular `source` block set - # specific fields. + source "source.amazon-ebs.example-2" { + # Use the singular `source` block set specific fields. # Note that fields cannot be overwritten, in other words, you cannot # set the 'output' field from the top-level source block and here. output = "different value" diff --git a/website/pages/partials/from-1.5/locals/example-block.mdx b/website/pages/partials/from-1.5/locals/example-block.mdx index c0cb5e663..b6adfa115 100644 --- a/website/pages/partials/from-1.5/locals/example-block.mdx +++ b/website/pages/partials/from-1.5/locals/example-block.mdx @@ -3,7 +3,7 @@ locals { # locals can be bare values like: wee = local.baz - # locals can also be set with input variables : - baz = "Foo is '${var.foo}'" + # locals can also be set with other variables : + baz = "Foo is '${var.foo}' but not '${local.wee}'" } ``` \ No newline at end of file diff --git a/website/pages/partials/from-1.5/sources/example-block.mdx b/website/pages/partials/from-1.5/sources/example-block.mdx index 05b2c5012..fcd948430 100644 --- a/website/pages/partials/from-1.5/sources/example-block.mdx +++ b/website/pages/partials/from-1.5/sources/example-block.mdx @@ -1,6 +1,6 @@ ```hcl # sources.pkr.hcl -source "amazon-ebs" "example" { +source "amazon-ebs" "example-1" { // ... } ``` \ No newline at end of file