hcp: support for multiple build block with hcl2

prepend the build block name to the component name before making any
request to hcp server
hcp-multiple-build-block
Martin Grogan 1 year ago
parent af6dbb2280
commit 4cba382a0a

@ -69,7 +69,8 @@ func (h *HCLRegistry) StartBuild(ctx context.Context, build sdkpacker.Build) err
name := build.Name()
cb, ok := build.(*packer.CoreBuild)
if ok {
name = cb.Type
// We prepend type with builder block name to prevent conflict
name = fmt.Sprintf("%s.%s", cb.BuildName, cb.Type)
}
return h.bucket.startBuild(ctx, name)
@ -85,7 +86,8 @@ func (h *HCLRegistry) CompleteBuild(
buildName := build.Name()
cb, ok := build.(*packer.CoreBuild)
if ok {
buildName = cb.Type
// We prepend type with builder block name to prevent conflict
buildName = fmt.Sprintf("%s.%s", cb.BuildName, cb.Type)
}
buildMetadata, envMetadata := cb.GetMetadata(), h.metadata
@ -103,18 +105,6 @@ func (h *HCLRegistry) VersionStatusSummary() {
func NewHCLRegistry(config *hcl2template.PackerConfig, ui sdkpacker.Ui) (*HCLRegistry, hcl.Diagnostics) {
var diags hcl.Diagnostics
if len(config.Builds) > 1 {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Multiple " + buildLabel + " blocks",
Detail: fmt.Sprintf("For HCP Packer Registry enabled builds, only one " + buildLabel +
" block can be defined. Please remove any additional " + buildLabel +
" block(s). If this " + buildLabel + " is not meant for the HCP Packer registry please " +
"clear any HCP_PACKER_* environment variables."),
})
return nil, diags
}
withHCLBucketConfiguration := func(bb *hcl2template.BuildBlock) bucketConfigurationOpts {
return func(bucket *Bucket) hcl.Diagnostics {
@ -155,8 +145,13 @@ func NewHCLRegistry(config *hcl2template.PackerConfig, ui sdkpacker.Ui) (*HCLReg
return nil, diags
}
for _, source := range build.Sources {
bucket.RegisterBuildForComponent(source.String())
for _, build := range config.Builds {
for _, source := range build.Sources {
// We prepend each source with builder block name to prevent conflict
buildName := fmt.Sprintf("%s.%s", build.Name, source.String())
bucket.RegisterBuildForComponent(buildName)
}
}
ui.Say(fmt.Sprintf("Tracking build on HCP Packer with fingerprint %q", bucket.Version.Fingerprint))

@ -132,16 +132,15 @@ func (bucket *Bucket) Initialize(
return bucket.initializeVersion(ctx, templateType)
}
func (bucket *Bucket) RegisterBuildForComponent(sourceName string) {
func (bucket *Bucket) RegisterBuildForComponent(buildName string) {
if bucket == nil {
return
}
if ok := bucket.Version.HasBuild(sourceName); ok {
if ok := bucket.Version.HasBuild(buildName); ok {
return
}
bucket.Version.expectedBuilds = append(bucket.Version.expectedBuilds, sourceName)
bucket.Version.expectedBuilds = append(bucket.Version.expectedBuilds, buildName)
}
// CreateInitialBuildForVersion will create a build entry on the HCP Packer Registry for the named componentType.
@ -658,7 +657,7 @@ func (bucket *Bucket) doCompleteBuild(
ctx context.Context,
buildName string,
packerSDKArtifacts []packerSDK.Artifact,
buildErr error,
_ error,
) ([]packerSDK.Artifact, error) {
for _, art := range packerSDKArtifacts {
var sdkImages []packerSDKRegistry.Image

Loading…
Cancel
Save