hcl work on only/except (#9454)

* HCL2: allow to skip a named build block too

* test that excepting a build block works

* test only on a named build block

* add/update docs
pull/9461/head
Adrien Delorme 6 years ago committed by GitHub
parent b2320ca911
commit fb337f8867
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -207,6 +207,31 @@ func TestBuild(t *testing.T) {
},
},
},
// only / except HCL2
{
name: "hcl - 'except' a build block",
args: []string{
"-except=my_build.*",
testFixture("hcl-only-except"),
},
fileCheck: fileCheck{
expected: []string{"cherry.txt"},
notExpected: []string{"chocolate.txt", "vanilla.txt"},
},
},
{
name: "hcl - 'only' a build block",
args: []string{
"-only=my_build.*",
testFixture("hcl-only-except"),
},
fileCheck: fileCheck{
notExpected: []string{"cherry.txt"},
expected: []string{"chocolate.txt", "vanilla.txt"},
},
},
}
for _, tt := range tc {
@ -490,15 +515,20 @@ func TestBuildCommand_HCLOnlyExceptOptions(t *testing.T) {
[]string{"chocolate.txt", "vanilla.txt"},
},
{
[]string{"-only=file.chocolate"},
[]string{"-only=my_build.file.chocolate"},
[]string{"chocolate.txt"},
[]string{"vanilla.txt", "cherry.txt"},
},
{
[]string{"-except=file.chocolate"},
[]string{"-except=my_build.file.chocolate"},
[]string{"vanilla.txt", "cherry.txt"},
[]string{"chocolate.txt"},
},
{
[]string{"-only=file.cherry"},
[]string{"cherry.txt"},
[]string{"vanilla.txt", "chocolate.txt"},
},
}
for _, tt := range tests {

@ -14,9 +14,15 @@ source "file" "cherry" {
}
build {
source "file.cherry" {
}
}
build {
name = "my_build"
sources = [
"file.chocolate",
"file.vanilla",
"file.cherry",
]
}

@ -301,9 +301,13 @@ func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packer.Build
}
src.addition = from.addition
// Apply the -only and -except command-line options to exclude matching builds.
buildName := fmt.Sprintf("%s.%s", src.Type, src.Name)
pcb := &packer.CoreBuild{
BuildName: build.Name,
Type: src.Ref().String(),
}
// Apply the -only and -except command-line options to exclude matching builds.
buildName := pcb.Name()
// -only
if len(opts.Only) > 0 {
onlyGlobs, diags := convertFilterOption(opts.Only, "only")
@ -383,14 +387,11 @@ func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packer.Build
continue
}
pcb := &packer.CoreBuild{
BuildName: build.Name,
Type: src.Ref().String(),
Builder: builder,
Provisioners: provisioners,
PostProcessors: pps,
Prepared: true,
}
pcb.Builder = builder
pcb.Provisioners = provisioners
pcb.PostProcessors = pps
pcb.Prepared = true
// Prepare just sets the "prepareCalled" flag on CoreBuild, since
// we did all the prep here.
_, err := pcb.Prepare()

@ -11,12 +11,12 @@ export default [
content: [
{
category: 'blocks',
content: [
content: [
{
category: 'build',
content: [ 'source', 'provisioner', 'post-processor' ],
content: ['source', 'provisioner', 'post-processor'],
},
'locals', 'source', 'variable' ],
'locals', 'source', 'variable'],
},
{
category: 'functions',
@ -141,6 +141,7 @@ export default [
'variables',
'locals',
'syntax',
'onlyexcept',
'expressions',
'syntax-json',
],

@ -26,13 +26,7 @@ artifacts that are created will be outputted at the end of the build.
will stop between each step, waiting for keyboard input before continuing.
This will allow the user to inspect state and so on.
- `-except=foo,bar,baz` - Run all the builds and post-processors except those
with the given comma-separated names. Build and post-processor names by
default are their type, unless a specific `name` attribute is specified
within the configuration. Any post-processor following a skipped
post-processor will not run. Because post-processors can be nested in
arrays a different post-processor chain can still run. A post-processor
with an empty name will be ignored.
`@include 'commands/except.mdx'`
- `-force` - Forces a builder to run when artifacts from a previous build
prevent a build from running. The exact behavior of a forced build is left
@ -54,6 +48,8 @@ artifacts that are created will be outputted at the end of the build.
attribute is specified within the configuration. `-only` does not apply to
post-processors.
`@include 'commands/only.mdx'`
- `-parallel-builds=N` - Limit the number of builds to run in parallel, 0
means no limit (defaults to 0).

@ -0,0 +1,65 @@
---
layout: docs
page_title: Only Except - HCL Configuration Language
sidebar_title: Only Except
description: >-
Only and Except can be used as a command line argument to selectively run
builds. Only and Except can also be used in a provisioner to not run it for a
source.
---
# Only and Except
`only` and `except` are keywords used to filter what runs in your Packer build,
they can be seen as a command line argument:
`@include 'commands/except.mdx'`
`@include 'commands/only.mdx'`
They can also be seen in a template to run or skip provisioners and/or
post-processors for a specific source:
```hcl
build {
name = "my_build"
sources [
"source.amazon-ebs.first-example",
"source.amazon-ebs.second-example",
]
provisioner "shell-local" {
only = ["source.amazon-ebs.second-example"]
inline = ["echo I will only run for the second example source"]
}
provisioner "shell-local" {
except = ["source.amazon-ebs.second-example"]
inline = ["echo I will never run for the second example source"]
}
}
build {
sources [
"source.amazon-ebs.third-example",
]
}
# this file will result in Packer creating three builds named:
# my_build.amazon-ebs.first-example
# my_build.amazon-ebs.second-example
# amazon-ebs.third-example
```
Note that cli arguments can be used with a glob operator, using the previous
configuration:
* `packer build -only 'my_build.*' dir`: will only run the builds in blocks
named `my_build`.
* `packer build -only '*.amazon-ebs.*' dir`: will only run the builds with a
source of type `amazon-ebs`.
-> Note: In the cli `only` and `except` will match agains **build names** (for
example:`my_build.amazon-ebs.first-example`) but in a provisioner they will
match on the **source type** (for example:`source.amazon-ebs.third-example`).

@ -0,0 +1,7 @@
- `-except=foo,bar,baz` - Run all the builds and post-processors except those
with the given comma-separated names. Build and post-processor names by
default are their type, unless a specific `name` attribute is specified
within the configuration. Any post-processor following a skipped
post-processor will not run. Because post-processors can be nested in
arrays a different post-processor chain can still run. A post-processor
with an empty name will be ignored.

@ -0,0 +1,4 @@
- `-only=foo,bar,baz` - Only run the builds with the given comma-separated
names. Build names by default are their type, unless a specific `name`
attribute is specified within the configuration. `-only` does not apply to
post-processors.
Loading…
Cancel
Save