浏览代码

Merge pull request #24978 from yongtang/24912-build-with-progress

Add hint of progress to the output of `docker build`
Doug Davis 9 年之前
父节点
当前提交
282b0aff08

+ 4 - 2
builder/dockerfile/builder.go

@@ -233,6 +233,7 @@ func (b *Builder) build(stdout io.Writer, stderr io.Writer, out io.Writer) (stri
 	}
 
 	var shortImgID string
+	total := len(b.dockerfile.Children)
 	for i, n := range b.dockerfile.Children {
 		select {
 		case <-b.clientCtx.Done():
@@ -242,7 +243,7 @@ func (b *Builder) build(stdout io.Writer, stderr io.Writer, out io.Writer) (stri
 		default:
 			// Not cancelled yet, keep going...
 		}
-		if err := b.dispatch(i, n); err != nil {
+		if err := b.dispatch(i, total, n); err != nil {
 			if b.options.ForceRemove {
 				b.clearTmp()
 			}
@@ -320,8 +321,9 @@ func BuildFromConfig(config *container.Config, changes []string) (*container.Con
 	b.Stderr = ioutil.Discard
 	b.disableCommit = true
 
+	total := len(ast.Children)
 	for i, n := range ast.Children {
-		if err := b.dispatch(i, n); err != nil {
+		if err := b.dispatch(i, total, n); err != nil {
 			return nil, err
 		}
 	}

+ 2 - 2
builder/dockerfile/evaluator.go

@@ -93,7 +93,7 @@ func init() {
 // such as `RUN` in ONBUILD RUN foo. There is special case logic in here to
 // deal with that, at least until it becomes more of a general concern with new
 // features.
-func (b *Builder) dispatch(stepN int, ast *parser.Node) error {
+func (b *Builder) dispatch(stepN int, stepTotal int, ast *parser.Node) error {
 	cmd := ast.Value
 	upperCasedCmd := strings.ToUpper(cmd)
 
@@ -107,7 +107,7 @@ func (b *Builder) dispatch(stepN int, ast *parser.Node) error {
 	original := ast.Original
 	flags := ast.Flags
 	strList := []string{}
-	msg := fmt.Sprintf("Step %d : %s", stepN+1, upperCasedCmd)
+	msg := fmt.Sprintf("Step %d/%d : %s", stepN+1, stepTotal, upperCasedCmd)
 
 	if len(ast.Flags) > 0 {
 		msg += " " + strings.Join(ast.Flags, " ")

+ 1 - 1
builder/dockerfile/evaluator_test.go

@@ -184,7 +184,7 @@ func executeTestCase(t *testing.T, testCase dispatchTestCase) {
 
 	b := &Builder{runConfig: config, options: options, Stdout: ioutil.Discard, context: context}
 
-	err = b.dispatch(0, n.Children[0])
+	err = b.dispatch(0, len(n.Children), n.Children[0])
 
 	if err == nil {
 		t.Fatalf("No error when executing test %s", testCase.name)

+ 2 - 1
builder/dockerfile/internals.go

@@ -434,6 +434,7 @@ func (b *Builder) processImageFrom(img builder.Image) error {
 			return err
 		}
 
+		total := len(ast.Children)
 		for i, n := range ast.Children {
 			switch strings.ToUpper(n.Value) {
 			case "ONBUILD":
@@ -442,7 +443,7 @@ func (b *Builder) processImageFrom(img builder.Image) error {
 				return fmt.Errorf("%s isn't allowed as an ONBUILD trigger", n.Value)
 			}
 
-			if err := b.dispatch(i, n); err != nil {
+			if err := b.dispatch(i, total, n); err != nil {
 				return err
 			}
 		}

+ 15 - 2
integration-cli/docker_cli_build_test.go

@@ -5417,7 +5417,7 @@ func (s *DockerSuite) TestBuildNoDupOutput(c *check.C) {
 		c.Fatalf("Build should have worked: %q", err)
 	}
 
-	exp := "\nStep 2 : RUN env\n"
+	exp := "\nStep 2/2 : RUN env\n"
 	if !strings.Contains(out, exp) {
 		c.Fatalf("Bad output\nGot:%s\n\nExpected to contain:%s\n", out, exp)
 	}
@@ -5434,7 +5434,7 @@ func (s *DockerSuite) TestBuildStartsFromOne(c *check.C) {
 		c.Fatalf("Build should have worked: %q", err)
 	}
 
-	exp := "\nStep 1 : FROM busybox\n"
+	exp := "\nStep 1/1 : FROM busybox\n"
 	if !strings.Contains(out, exp) {
 		c.Fatalf("Bad output\nGot:%s\n\nExpected to contain:%s\n", out, exp)
 	}
@@ -6981,3 +6981,16 @@ func (s *DockerSuite) TestBuildCmdShellArgsEscaped(c *check.C) {
 		c.Fatalf("CMD was not escaped Config.Cmd: got %v", res)
 	}
 }
+
+// Test case for #24912.
+func (s *DockerSuite) TestBuildStepsWithProgress(c *check.C) {
+	name := "testbuildstepswithprogress"
+
+	totalRun := 5
+	_, out, err := buildImageWithOut(name, "FROM busybox\n"+strings.Repeat("RUN echo foo\n", totalRun), true)
+	c.Assert(err, checker.IsNil)
+	c.Assert(out, checker.Contains, fmt.Sprintf("Step 1/%d : FROM busybox", 1+totalRun))
+	for i := 2; i <= 1+totalRun; i++ {
+		c.Assert(out, checker.Contains, fmt.Sprintf("Step %d/%d : RUN echo foo", i, 1+totalRun))
+	}
+}