Add 'create_image' option to googlecompute

When building a CI/CD deployment, during the PR process it's nice to be
able to run the builder, but not create the image the build produces.

Closes #9965
This commit is contained in:
Bryan Hundven 2020-10-15 14:56:57 -07:00
parent 456214eb51
commit 476eefa26b
4 changed files with 26 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import (
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/mitchellh/mapstructure"
compute "google.golang.org/api/compute/v1"
)
@ -76,6 +77,8 @@ type Config struct {
EnableIntegrityMonitoring bool `mapstructure:"enable_integrity_monitoring" required:"false"`
// Whether to use an IAP proxy.
IAPConfig `mapstructure:",squash"`
// Create the image. Useful for setting to `false` during a build test stage. Defaults to `true`.
CreateImage bool `mapstructure:"create_image" required:"false"`
// The unique name of the resulting image. Defaults to
// `packer-{{timestamp}}`.
ImageName string `mapstructure:"image_name" required:"false"`
@ -288,7 +291,9 @@ type Config struct {
func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
c.ctx.Funcs = TemplateFuncs
var md mapstructure.Metadata
err := config.Decode(c, &config.DecodeOpts{
Metadata: &md,
Interpolate: true,
InterpolateContext: &c.ctx,
InterpolateFilter: &interpolate.RenderFilter{
@ -329,6 +334,18 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
}
}
hasCreateImage := false
for _, k := range md.Keys {
if k == "create_image" {
hasCreateImage = true
break
}
}
if !hasCreateImage {
c.CreateImage = true
}
if c.ImageDescription == "" {
c.ImageDescription = "Created by Packer"
}

View File

@ -81,6 +81,7 @@ type FlatConfig struct {
IAPHashBang *string `mapstructure:"iap_hashbang" required:"false" cty:"iap_hashbang" hcl:"iap_hashbang"`
IAPExt *string `mapstructure:"iap_ext" required:"false" cty:"iap_ext" hcl:"iap_ext"`
IAPTunnelLaunchWait *int `mapstructure:"iap_tunnel_launch_wait" required:"false" cty:"iap_tunnel_launch_wait" hcl:"iap_tunnel_launch_wait"`
CreateImage *bool `mapstructure:"create_image" required:"false" cty:"create_image" hcl:"create_image"`
ImageName *string `mapstructure:"image_name" required:"false" cty:"image_name" hcl:"image_name"`
ImageDescription *string `mapstructure:"image_description" required:"false" cty:"image_description" hcl:"image_description"`
ImageEncryptionKey *FlatCustomerEncryptionKey `mapstructure:"image_encryption_key" required:"false" cty:"image_encryption_key" hcl:"image_encryption_key"`
@ -200,6 +201,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
"iap_hashbang": &hcldec.AttrSpec{Name: "iap_hashbang", Type: cty.String, Required: false},
"iap_ext": &hcldec.AttrSpec{Name: "iap_ext", Type: cty.String, Required: false},
"iap_tunnel_launch_wait": &hcldec.AttrSpec{Name: "iap_tunnel_launch_wait", Type: cty.Number, Required: false},
"create_image": &hcldec.AttrSpec{Name: "create_image", Type: cty.Bool, Required: false},
"image_name": &hcldec.AttrSpec{Name: "image_name", Type: cty.String, Required: false},
"image_description": &hcldec.AttrSpec{Name: "image_description", Type: cty.String, Required: false},
"image_encryption_key": &hcldec.BlockSpec{TypeName: "image_encryption_key", Nested: hcldec.ObjectSpec((*FlatCustomerEncryptionKey)(nil).HCL2Spec())},

View File

@ -23,6 +23,11 @@ func (s *StepCreateImage) Run(ctx context.Context, state multistep.StateBag) mul
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
if !config.CreateImage {
ui.Say("Skipping image creation...")
return multistep.ActionContinue
}
if config.PackerForce && config.imageAlreadyExists {
ui.Say("Deleting previous image...")

View File

@ -42,6 +42,8 @@
state of your VM instances. Note: integrity monitoring relies on having
vTPM enabled. [Details](https://cloud.google.com/security/shielded-cloud/shielded-vm)
- `create_image` (bool) - Create the image. Useful for setting to `false` during a build test stage. Defaults to `true`.
- `image_name` (string) - The unique name of the resulting image. Defaults to
`packer-{{timestamp}}`.