Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions pkg/buildx/buildx.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (

const DefaultTargetName = "default"

func BuildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]build.Options, progressMode, contextPathHash, metadataFile string, project string, token string, allowNoOutput bool) (imageID string, err error) {
func BuildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]build.Options, progressMode, contextPathHash, metadataFile string, project string, token string, allowNoOutput bool, buildPlatform string) (imageID string, err error) {
var buildErr error

ctx2, cancel := context.WithCancel(context.TODO())
Expand Down Expand Up @@ -74,7 +74,7 @@ func BuildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]bu
}
}()

dis, err := GetDrivers(ctx, dockerCli, contextPathHash, b.Msg.BuildId, token)
dis, err := GetDrivers(ctx, dockerCli, contextPathHash, b.Msg.BuildId, token, buildPlatform)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func BuildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]bu
return resp[DefaultTargetName].ExporterResponse["containerimage.digest"], err
}

func GetDrivers(ctx context.Context, dockerCli command.Cli, contextPathHash string, buildID string, token string) ([]build.DriverInfo, error) {
func GetDrivers(ctx context.Context, dockerCli command.Cli, contextPathHash string, buildID string, token string, buildPlatform string) ([]build.DriverInfo, error) {
imageopt, err := storeutil.GetImageConfig(dockerCli, nil)
if err != nil {
return nil, err
Expand Down Expand Up @@ -151,6 +151,14 @@ func GetDrivers(ctx context.Context, dockerCli command.Cli, contextPathHash stri
},
}

if buildPlatform == "linux/amd64" {
return []build.DriverInfo{amdDriverInfo}, nil
}

if buildPlatform == "linux/arm64" {
return []build.DriverInfo{armDriverInfo}, nil
}

if strings.HasPrefix(runtime.GOARCH, "arm") {
return []build.DriverInfo{armDriverInfo, amdDriverInfo}, nil
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/cmd/bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/depot/cli/pkg/buildx"
"github.com/depot/cli/pkg/config"
dockerclient "github.com/depot/cli/pkg/docker"
"github.com/depot/cli/pkg/helpers"
"github.com/depot/cli/pkg/project"
cliv1beta1 "github.com/depot/cli/pkg/proto/depot/cli/v1beta1"
"github.com/docker/buildx/bake"
Expand All @@ -27,8 +28,9 @@ import (
)

type bakeOptions struct {
project string
token string
project string
token string
buildPlatform string

files []string
overrides []string
Expand Down Expand Up @@ -129,7 +131,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions) (err error
}
}()

dis, err := buildx.GetDrivers(ctx, dockerCli, contextPathHash, b.Msg.BuildId, in.token)
dis, err := buildx.GetDrivers(ctx, dockerCli, contextPathHash, b.Msg.BuildId, in.token, in.buildPlatform)
if err != nil {
return err
}
Expand Down Expand Up @@ -238,6 +240,12 @@ func NewCmdBake() *cobra.Command {
return fmt.Errorf("missing API token, please run `depot login`")
}

buildPlatform, err := helpers.NormalizePlatform(options.buildPlatform)
if err != nil {
return err
}
options.buildPlatform = buildPlatform

dockerCli, err := dockerclient.NewDockerCLI()
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand All @@ -253,6 +261,7 @@ func NewCmdBake() *cobra.Command {
// Depot options
flags.StringVar(&options.project, "project", "", "Depot project ID")
flags.StringVar(&options.token, "token", "", "Depot API token")
flags.StringVar(&options.buildPlatform, "build-platform", "dynamic", `Run builds on this platform ("dynamic", "linux/amd64", "linux/arm64")`)

// docker buildx bake options
flags.StringArrayVarP(&options.files, "file", "f", []string{}, "Build definition file")
Expand Down
11 changes: 10 additions & 1 deletion pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
_ "github.com/depot/cli/pkg/buildxdriver"
"github.com/depot/cli/pkg/config"
"github.com/depot/cli/pkg/docker"
"github.com/depot/cli/pkg/helpers"
"github.com/depot/cli/pkg/project"
"github.com/docker/buildx/build"
"github.com/docker/buildx/util/buildflags"
Expand All @@ -32,6 +33,7 @@ type buildOptions struct {
project string
token string
allowNoOutput bool
buildPlatform string

contextPath string
dockerfileName string
Expand Down Expand Up @@ -232,7 +234,7 @@ func runBuild(dockerCli command.Cli, in buildOptions) (err error) {
contextPathHash = in.contextPath
}

imageID, err := buildx.BuildTargets(ctx, dockerCli, map[string]build.Options{buildx.DefaultTargetName: opts}, in.progress, contextPathHash, in.metadataFile, in.project, in.token, in.allowNoOutput)
imageID, err := buildx.BuildTargets(ctx, dockerCli, map[string]build.Options{buildx.DefaultTargetName: opts}, in.progress, contextPathHash, in.metadataFile, in.project, in.token, in.allowNoOutput, in.buildPlatform)
err = buildx.WrapBuildError(err, false)
if err != nil {
return err
Expand Down Expand Up @@ -287,6 +289,12 @@ func NewCmdBuild() *cobra.Command {
return errors.Errorf("load and no-load may not be both set")
}

buildPlatform, err := helpers.NormalizePlatform(options.buildPlatform)
if err != nil {
return err
}
options.buildPlatform = buildPlatform

options.contextPath = args[0]
return runBuild(dockerCli, options)
},
Expand All @@ -302,6 +310,7 @@ func NewCmdBuild() *cobra.Command {
// Depot options
flags.StringVar(&options.project, "project", "", "Depot project ID")
flags.StringVar(&options.token, "token", "", "Depot API token")
flags.StringVar(&options.buildPlatform, "build-platform", "dynamic", `Run builds on this platform ("dynamic", "linux/amd64", "linux/arm64")`)

allowNoOutput := false
if v := os.Getenv("DEPOT_SUPPRESS_NO_OUTPUT_WARNING"); v != "" {
Expand Down
22 changes: 22 additions & 0 deletions pkg/helpers/buildplatform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package helpers

import (
"fmt"
"os"
)

func NormalizePlatform(buildPlatform string) (string, error) {
if buildPlatform == "" {
buildPlatform = os.Getenv("DEPOT_BUILD_PLATFORM")
}

if buildPlatform == "" {
buildPlatform = "dynamic"
}

if buildPlatform != "linux/amd64" && buildPlatform != "linux/arm64" && buildPlatform != "dynamic" {
return "", fmt.Errorf("invalid build platform: %s (must be one of: dynamic, linux/amd64, linux/arm64)", buildPlatform)
}

return buildPlatform, nil
}