Browse Source

check buildx version before comparing it

Signed-off-by: yangfeiyu <[email protected]>
yangfeiyu 3 weeks ago
parent
commit
000a4a4b9f
1 changed files with 22 additions and 5 deletions
  1. 22 5
      pkg/compose/build_bake.go

+ 22 - 5
pkg/compose/build_bake.go

@@ -289,15 +289,11 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
 		_ = os.Remove(metadataFile)
 	}()
 
-	buildx, err := manager.GetPlugin("buildx", s.dockerCli, &cobra.Command{})
+	buildx, err := s.getBuildxPlugin()
 	if err != nil {
 		return nil, err
 	}
 
-	if versions.LessThan(buildx.Version[1:], "0.17.0") {
-		return nil, fmt.Errorf("compose build requires buildx 0.17 or later")
-	}
-
 	args := []string{"bake", "--file", "-", "--progress", "rawjson", "--metadata-file", metadataFile}
 	// FIXME we should prompt user about this, but this is a breaking change in UX
 	for _, path := range read {
@@ -414,6 +410,27 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
 	return results, nil
 }
 
+func (s *composeService) getBuildxPlugin() (*manager.Plugin, error) {
+	buildx, err := manager.GetPlugin("buildx", s.dockerCli, &cobra.Command{})
+	if err != nil {
+		return nil, err
+	}
+
+	if buildx.Err != nil {
+		return nil, buildx.Err
+	}
+
+	if buildx.Version == "" {
+		return nil, fmt.Errorf("failed to get version of buildx")
+	}
+
+	if versions.LessThan(buildx.Version[1:], "0.17.0") {
+		return nil, fmt.Errorf("compose build requires buildx 0.17 or later")
+	}
+
+	return buildx, nil
+}
+
 // makeConsole wraps the provided writer to match [containerd.File] interface if it is of type *streams.Out.
 // buildkit's NewDisplay doesn't actually require a [io.Reader], it only uses the [containerd.Console] type to
 // benefits from ANSI capabilities, but only does writes.