Skip to content

Commit

Permalink
fix(build): throw error if building process fails
Browse files Browse the repository at this point in the history
  • Loading branch information
tunahanertekin committed May 22, 2023
1 parent 6e9d7b4 commit 57479d2
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions pkg/docker/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package docker

import (
"archive/tar"
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"io"
"os"
Expand All @@ -16,6 +18,15 @@ import (
"github.com/robolaunch/cosmodrome/pkg/api"
)

type ErrorLine struct {
Error string `json:"error"`
ErrorDetail ErrorDetail `json:"errorDetail"`
}

type ErrorDetail struct {
Message string `json:"message"`
}

func Build(ctx context.Context, dfName, dfPath, buildContext string, step api.Step, lc api.LaunchConfig) error {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
Expand Down Expand Up @@ -60,7 +71,7 @@ func Build(ctx context.Context, dfName, dfPath, buildContext string, step api.St
defer imageBuildResponse.Body.Close()

if lc.Verbose {
_, err = io.Copy(os.Stdout, imageBuildResponse.Body)
err = printBuildLogs(os.Stdout, imageBuildResponse.Body)
if err != nil {
return err
}
Expand All @@ -82,7 +93,7 @@ func Build(ctx context.Context, dfName, dfPath, buildContext string, step api.St
}
}

_, err = io.Copy(f, imageBuildResponse.Body)
err = printBuildLogs(f, imageBuildResponse.Body)
if err != nil {
return err
}
Expand All @@ -91,6 +102,28 @@ func Build(ctx context.Context, dfName, dfPath, buildContext string, step api.St
return nil
}

func printBuildLogs(dst io.Writer, rd io.Reader) error {
var lastLine string

scanner := bufio.NewScanner(rd)
for scanner.Scan() {
lastLine = scanner.Text()
io.Copy(dst, rd)
}

errLine := &ErrorLine{}
json.Unmarshal([]byte(lastLine), errLine)
if errLine.Error != "" {
return errors.New(errLine.Error)
}

if err := scanner.Err(); err != nil {
return err
}

return nil
}

func BuildMultiplatform(ctx context.Context, dfName, dfPath, buildContext, baseImage string, step api.Step, lc api.LaunchConfig) error {

// docker buildx rm multiarch_builder || true
Expand Down

0 comments on commit 57479d2

Please sign in to comment.