Skip to content

Commit

Permalink
fix: Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvey committed Jan 7, 2022
1 parent 87ec681 commit fef7d44
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
5 changes: 4 additions & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ func doBuild(opts buildOpts) (*dag.DAG, error) {
}
}

DAG.Rebuild(currentVersion, opts.forceRebuild, opts.runTests, opts.localOnly)
if err := DAG.Rebuild(currentVersion, opts.forceRebuild, opts.runTests, opts.localOnly); err != nil {
return nil, err
}

if opts.retagLatest {
logrus.Info("--retag-latest is set to true, latest tag will now use current image versions")
if err := DAG.RetagLatest(currentVersion); err != nil {
Expand Down
8 changes: 7 additions & 1 deletion dag/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,22 @@ func (dag *DAG) RetagLatest(tag string) error {
return nil
}

func (dag *DAG) Rebuild(newTag string, forceRebuild, runTests, localOnly bool) {
func (dag *DAG) Rebuild(newTag string, forceRebuild, runTests, localOnly bool) error {
errs := make(chan error, 1)
for _, img := range dag.Images {
go img.Rebuild(newTag, forceRebuild, runTests, localOnly, errs)
}
var hasError bool
for i := 0; i < len(dag.Images); i++ {
err := <-errs
if err != nil {
hasError = true
logrus.Errorf("Error building image: %v", err)
}
}
close(errs)
if hasError {
return fmt.Errorf("one of the image build failed, see logs for more details")
}
return nil
}
12 changes: 9 additions & 3 deletions dag/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,18 @@ func (img *Image) Rebuild(newTag string, forceRebuild, runTests, localOnly bool,
for _, child := range img.Children {
go child.Rebuild(newTag, forceRebuild, runTests, localOnly, errs)
}
var hasError bool
for i := 0; i < len(img.Children); i++ {
err := <-errs
if err != nil {
hasError = true
logrus.Errorf("Error building image: %v", err)
}
}
close(errs)
if hasError {
errChan <- fmt.Errorf("one of the image build failed, see logs for more details")
}
errChan <- nil
}

Expand All @@ -102,7 +108,7 @@ func (img *Image) doRebuild(newTag string, localOnly bool) error {
logrus.Infof("Building \"%s:%s\" in context \"%s\"", img.Name, newTag, img.Dockerfile.ContextPath)

if err := dockerfile.ReplaceFromTag(*img.Dockerfile, newTag); err != nil {
return err
return fmt.Errorf("failed to replace tag in dockerfile %s: %w", img.Dockerfile.ContextPath, err)
}

now := time.Now()
Expand All @@ -124,11 +130,11 @@ func (img *Image) doRebuild(newTag string, localOnly bool) error {

err := img.Builder.Build(opts)
if err != nil {
return err
return fmt.Errorf("building image %s failed: %w", img.ShortName, err)
}

if err := dockerfile.ResetFromTag(*img.Dockerfile, newTag); err != nil {
return err
return fmt.Errorf("failed to reset tag in dockerfile %s: %w", img.Dockerfile.ContextPath, err)
}

return nil
Expand Down

0 comments on commit fef7d44

Please sign in to comment.