diff --git a/command/build_test.go b/command/build_test.go index 8aa30c720..21a032d6e 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -1318,6 +1318,27 @@ func TestBuildCmd(t *testing.T) { return fmt.Errorf("error: missing context for error message") } + return nil + }, + }, + { + name: "hcl - exclude post-processor, expect no warning", + args: []string{ + "-except", "manifest", + testFixture("hcl", "test_except_manifest.pkr.hcl"), + }, + expectedCode: 0, + outputCheck: func(out, err string) error { + for _, stream := range []string{out, err} { + if strings.Contains(stream, "Warning: an 'except' option was passed, but did not match any build") { + return fmt.Errorf("Unexpected warning for build no match with except") + } + + if strings.Contains(stream, "Running post-processor:") { + return fmt.Errorf("Should not run post-processors, but found one") + } + } + return nil }, }, diff --git a/command/test-fixtures/hcl/test_except_manifest.pkr.hcl b/command/test-fixtures/hcl/test_except_manifest.pkr.hcl new file mode 100644 index 000000000..bf9f7ad29 --- /dev/null +++ b/command/test-fixtures/hcl/test_except_manifest.pkr.hcl @@ -0,0 +1,9 @@ +source "null" "test" { + communicator = "none" +} + +build { + sources = ["null.test"] + + post-processor "manifest" {} +} diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index ef67f33e7..3ac4b0c51 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -514,7 +514,7 @@ func (cfg *PackerConfig) getCoreBuildProvisioner(source SourceUseBlock, pb *Prov // getCoreBuildProvisioners takes a list of post processor block, starts // according provisioners and sends parsed HCL2 over to it. -func (cfg *PackerConfig) getCoreBuildPostProcessors(source SourceUseBlock, blocksList [][]*PostProcessorBlock, ectx *hcl.EvalContext) ([][]packer.CoreBuildPostProcessor, hcl.Diagnostics) { +func (cfg *PackerConfig) getCoreBuildPostProcessors(source SourceUseBlock, blocksList [][]*PostProcessorBlock, ectx *hcl.EvalContext, exceptMatches *int) ([][]packer.CoreBuildPostProcessor, hcl.Diagnostics) { var diags hcl.Diagnostics res := [][]packer.CoreBuildPostProcessor{} for _, blocks := range blocksList { @@ -533,6 +533,7 @@ func (cfg *PackerConfig) getCoreBuildPostProcessors(source SourceUseBlock, block for _, exceptGlob := range cfg.except { if exceptGlob.Match(name) { exclude = true + *exceptMatches = *exceptMatches + 1 break } } @@ -673,21 +674,20 @@ func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packersdk.Bu if moreDiags.HasErrors() { continue } - pps, moreDiags := cfg.getCoreBuildPostProcessors(srcUsage, build.PostProcessorsLists, cfg.EvalContext(BuildContext, variables)) + pps, moreDiags := cfg.getCoreBuildPostProcessors(srcUsage, build.PostProcessorsLists, cfg.EvalContext(BuildContext, variables), &opts.ExceptMatches) diags = append(diags, moreDiags...) if moreDiags.HasErrors() { continue } - if build.ErrorCleanupProvisionerBlock != nil { - if !build.ErrorCleanupProvisionerBlock.OnlyExcept.Skip(srcUsage.String()) { - errorCleanupProv, moreDiags := cfg.getCoreBuildProvisioner(srcUsage, build.ErrorCleanupProvisionerBlock, cfg.EvalContext(BuildContext, variables)) - diags = append(diags, moreDiags...) - if moreDiags.HasErrors() { - continue - } - pcb.CleanupProvisioner = errorCleanupProv + if build.ErrorCleanupProvisionerBlock != nil && + !build.ErrorCleanupProvisionerBlock.OnlyExcept.Skip(srcUsage.String()) { + errorCleanupProv, moreDiags := cfg.getCoreBuildProvisioner(srcUsage, build.ErrorCleanupProvisionerBlock, cfg.EvalContext(BuildContext, variables)) + diags = append(diags, moreDiags...) + if moreDiags.HasErrors() { + continue } + pcb.CleanupProvisioner = errorCleanupProv } pcb.Builder = builder