فهرست منبع

Merge pull request #36960 from arm64b/fix-multi-stage-name-issue

Fix the target name issue for multi-stage build
Sebastiaan van Stijn 7 سال پیش
والد
کامیت
51a9119f6b
3فایلهای تغییر یافته به همراه10 افزوده شده و 3 حذف شده
  1. 1 1
      builder/dockerfile/builder.go
  2. 2 1
      builder/dockerfile/instructions/commands.go
  3. 7 1
      integration-cli/docker_cli_build_test.go

+ 1 - 1
builder/dockerfile/builder.go

@@ -226,7 +226,7 @@ func (b *Builder) build(source builder.Source, dockerfile *parser.Result) (*buil
 		targetIx, found := instructions.HasStage(stages, b.options.Target)
 		targetIx, found := instructions.HasStage(stages, b.options.Target)
 		if !found {
 		if !found {
 			buildsFailed.WithValues(metricsBuildTargetNotReachableError).Inc()
 			buildsFailed.WithValues(metricsBuildTargetNotReachableError).Inc()
-			return nil, errors.Errorf("failed to reach build target %s in Dockerfile", b.options.Target)
+			return nil, errdefs.InvalidParameter(errors.Errorf("failed to reach build target %s in Dockerfile", b.options.Target))
 		}
 		}
 		stages = stages[:targetIx+1]
 		stages = stages[:targetIx+1]
 	}
 	}

+ 2 - 1
builder/dockerfile/instructions/commands.go

@@ -390,7 +390,8 @@ func CurrentStage(s []Stage) (*Stage, error) {
 // HasStage looks for the presence of a given stage name
 // HasStage looks for the presence of a given stage name
 func HasStage(s []Stage, name string) (int, bool) {
 func HasStage(s []Stage, name string) (int, bool) {
 	for i, stage := range s {
 	for i, stage := range s {
-		if stage.Name == name {
+		// Stage name is case-insensitive by design
+		if strings.EqualFold(stage.Name, name) {
 			return i, true
 			return i, true
 		}
 		}
 	}
 	}

+ 7 - 1
integration-cli/docker_cli_build_test.go

@@ -5965,10 +5965,16 @@ func (s *DockerSuite) TestBuildIntermediateTarget(c *check.C) {
 	cli.BuildCmd(c, "build1", build.WithExternalBuildContext(ctx),
 	cli.BuildCmd(c, "build1", build.WithExternalBuildContext(ctx),
 		cli.WithFlags("--target", "build-env"))
 		cli.WithFlags("--target", "build-env"))
 
 
-	//res := inspectFieldJSON(c, "build1", "Config.Cmd")
 	res := cli.InspectCmd(c, "build1", cli.Format("json .Config.Cmd")).Combined()
 	res := cli.InspectCmd(c, "build1", cli.Format("json .Config.Cmd")).Combined()
 	c.Assert(strings.TrimSpace(res), checker.Equals, `["/dev"]`)
 	c.Assert(strings.TrimSpace(res), checker.Equals, `["/dev"]`)
 
 
+	// Stage name is case-insensitive by design
+	cli.BuildCmd(c, "build1", build.WithExternalBuildContext(ctx),
+		cli.WithFlags("--target", "BUIld-EnV"))
+
+	res = cli.InspectCmd(c, "build1", cli.Format("json .Config.Cmd")).Combined()
+	c.Assert(strings.TrimSpace(res), checker.Equals, `["/dev"]`)
+
 	result := cli.Docker(cli.Build("build1"), build.WithExternalBuildContext(ctx),
 	result := cli.Docker(cli.Build("build1"), build.WithExternalBuildContext(ctx),
 		cli.WithFlags("--target", "nosuchtarget"))
 		cli.WithFlags("--target", "nosuchtarget"))
 	result.Assert(c, icmd.Expected{
 	result.Assert(c, icmd.Expected{