hashicorp/packer · error

failed to add artifact for %q: %s

Error message

failed to add artifact for %q: %s

What it means

After successfully decoding an artifact's HCP images, doCompleteBuild uploads them via bucket.UpdateArtifactForBuild. If that HCP Packer API call fails, the completed-build flow aborts with this error naming the build, and the build is subsequently marked FAILED even though the local build itself succeeded.

Source

Thrown at internal/hcp/registry/types.bucket.go:792

				"failed to create decoder for HCP Packer artifact: %w",
				err)
		}

		state := art.State(packerSDKRegistry.ArtifactStateURI)
		if state == nil {
			log.Printf("[WARN] - artifact %q returned a nil value for the HCP state, ignoring", art.BuilderId())
			continue
		}

		err = decoder.Decode(state)
		if err != nil {
			log.Printf("[WARN] - artifact %q failed to be decoded to an HCP artifact, this is probably because it is not compatible: %s", art.BuilderId(), err)
			continue
		}

		err = bucket.UpdateArtifactForBuild(buildName, sdkImages...)
		if err != nil {
			return packerSDKArtifacts, fmt.Errorf("failed to add artifact for %q: %s", buildName, err)
		}
	}

	build, err := bucket.Version.Build(buildName)
	if err != nil {
		return packerSDKArtifacts, fmt.Errorf(
			"failed to get build %q from version being built. This is a Packer bug.",
			buildName)
	}
	if len(build.Artifacts) == 0 {
		return packerSDKArtifacts, &NotAHCPArtifactError{
			fmt.Errorf("No HCP Packer-compatible artifacts were found for the build"),
		}
	}

	for _, sbom := range build.CompressedSboms {
		err = bucket.uploadSbom(ctx, buildName, sbom)
		if err != nil {

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Re-run the build (or resume) ensuring stable connectivity to api.cloud.hashicorp.com
  2. Refresh HCP credentials or shorten build duration so tokens don't expire mid-run
  3. Check the wrapped inner error for the HTTP/gRPC status and address it (401 -> re-auth, 429 -> backoff, 5xx -> retry later)
  4. Verify the builder's artifact state (registry_image IDs) is valid and not oversized
Defensive patterns

Strategy: retry

Validate before calling

// Pre-flight: verify token validity and reachability before long builds
// curl -sS -o /dev/null -w '%{http_code}' -H "Authorization: Bearer $HCP_TOKEN" https://api.cloud.hashicorp.com/
// Token refresh: ensure HCP_CLIENT_ID/HCP_CLIENT_SECRET are set so Packer can renew tokens

Try / catch

// Go
if err := bucket.CompleteBuild(ctx, buildName, arts, ui, nil); err != nil {
	if strings.Contains(err.Error(), "failed to add artifact for") {
		// upload failed (auth/network/429/5xx): retry with backoff
		return retryWithBackoff(ctx, 3, func() error {
			_, e := bucket.CompleteBuild(ctx, buildName, arts, ui, nil)
			return e
		})
	}
	return err
}

Prevention

When it happens

Trigger: Bucket.CompleteBuild -> doCompleteBuild -> bucket.UpdateArtifactForBuild(buildName, sdkImages...) returns an error: network failure, authentication expiry, API 4xx/5xx, or invalid image payload rejected by the registry.

Common situations: HCP token expiring during a long build; transient network outage between build end and artifact upload; image metadata exceeding API limits; HCP Packer service incident; proxy intercepting the upload request.

Related errors


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/72348648948fe75c. Report an issue: GitHub.