Skip to content

Commit

Permalink
build: fall back to parsing a TARGETPLATFORM build-arg
Browse files Browse the repository at this point in the history
If we're not given an explicit platform or arch or os to target for a
build, but someone defined TARGETPLATFORM as a build argument, parse it.

Signed-off-by: Nalin Dahyabhai <[email protected]>
  • Loading branch information
nalind committed Sep 11, 2024
1 parent 19e7088 commit dedb2e1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions imagebuildah/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dedb2e1

Please sign in to comment.