diff --git a/imagebuildah/build.go b/imagebuildah/build.go index d545c83d794..4014adfebbb 100644 --- a/imagebuildah/build.go +++ b/imagebuildah/build.go @@ -229,6 +229,17 @@ func BuildDockerfiles(ctx context.Context, store storage.Store, options define.B if platform.OS == "" && platform.Arch != "" { platform.OS = runtime.GOOS } + if platform.OS == "" && platform.Arch == "" { + if targetPlatform, ok := options.Args["TARGETPLATFORM"]; ok { + targetPlatform, err := platforms.Parse(targetPlatform) + if err != nil { + return "", nil, fmt.Errorf("parsing TARGETPLATFORM value %q: %w", targetPlatform, err) + } + platform.OS = targetPlatform.OS + platform.Arch = targetPlatform.Architecture + platform.Variant = targetPlatform.Variant + } + } platformSpec := internalUtil.NormalizePlatform(v1.Platform{ OS: platform.OS, Architecture: platform.Arch, diff --git a/tests/bud.bats b/tests/bud.bats index dea9ccb21a0..16a1e641ffb 100644 --- a/tests/bud.bats +++ b/tests/bud.bats @@ -6157,6 +6157,18 @@ _EOF assert "$manifests" = "amd64 arm64 ppc64le s390x" "arch list in manifest" } +@test "bud-targetplatform-as-build-arg" { + outputlist=localhost/testlist + for targetplatform in linux/arm64 linux/amd64 ; do + run_buildah build $WITH_POLICY_JSON \ + --build-arg SAFEIMAGE=$SAFEIMAGE \ + --build-arg TARGETPLATFORM=$targetplatform \ + -f $BUDFILES/multiarch/Dockerfile.built-in-args \ + $BUDFILES/multiarch + expect_output --substring "I'm compiling for $targetplatform" + done +} + # * Performs multi-stage build with label1=value1 and verifies # * Relabels build with label1=value2 and verifies # * Rebuild with label1=value1 and makes sure everything is used from cache