Merge pull request #9245 from AdrienneCohea/master

Support named builds in HCL2 templates
This commit is contained in:
Megan Marsh 2020-06-02 11:42:14 -07:00 committed by GitHub
commit 19ae0ecf4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 59 additions and 14 deletions

View File

@ -0,0 +1,8 @@
build {
name = "somebuild"
sources = [
"source.amazon-ebs.ubuntu-1604",
"source.virtualbox-iso.ubuntu-1204",
]
}

View File

@ -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)

View File

@ -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)
}

View File

@ -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,

View File

@ -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
}

View File

@ -13,6 +13,13 @@ sidebar_title: <tt>build</tt>
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

View File

@ -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"

View File

@ -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}'"
}
```

View File

@ -1,6 +1,6 @@
```hcl
# sources.pkr.hcl
source "amazon-ebs" "example" {
source "amazon-ebs" "example-1" {
// ...
}
```