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

Fix the target name issue for multi-stage build
Upstream-commit: 51a9119f6b817bbae21805ec05787d462c9492cd
Component: engine
This commit is contained in:
Sebastiaan van Stijn
2018-05-02 13:27:50 +02:00
committed by GitHub
3 changed files with 10 additions and 3 deletions
@@ -226,7 +226,7 @@ func (b *Builder) build(source builder.Source, dockerfile *parser.Result) (*buil
targetIx, found := instructions.HasStage(stages, b.options.Target)
if !found {
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]
}
@@ -390,7 +390,8 @@ func CurrentStage(s []Stage) (*Stage, error) {
// HasStage looks for the presence of a given stage name
func HasStage(s []Stage, name string) (int, bool) {
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
}
}
@@ -5965,10 +5965,16 @@ func (s *DockerSuite) TestBuildIntermediateTarget(c *check.C) {
cli.BuildCmd(c, "build1", build.WithExternalBuildContext(ctx),
cli.WithFlags("--target", "build-env"))
//res := inspectFieldJSON(c, "build1", "Config.Cmd")
res := cli.InspectCmd(c, "build1", cli.Format("json .Config.Cmd")).Combined()
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),
cli.WithFlags("--target", "nosuchtarget"))
result.Assert(c, icmd.Expected{