Skip to content

Commit 32cf792

Browse files
committed
feat(server): add build log
Signed-off-by: Yaroslav Pershin <62902094+iapershin@users.noreply.github.com>
1 parent 5f76cb5 commit 32cf792

File tree

1 file changed

+15
-44
lines changed

1 file changed

+15
-44
lines changed

server/pkg/docker/build.go

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"os/exec"
1010
"path"
11-
"regexp"
1211

1312
"github.com/djherbis/buffer"
1413
"github.com/djherbis/nio/v3"
@@ -82,19 +81,20 @@ func BuildReleaseArtifacts(ctx context.Context, opts BuildReleaseArtifactsOpts,
8281
}
8382
}()
8483

85-
logboek.Context(ctx).Default().LogF("Building docker image with artifacts\n")
86-
logger.Debug("Building docker image with artifacts")
84+
logboek.Context(ctx).Default().LogLn("Building docker image with artifacts")
85+
logger.Info("Building docker image with artifacts")
8786

8887
args, err := setCliArgs(serviceDockerfilePathInContext, secrets)
8988
if err != nil {
9089
return fmt.Errorf("unable to set cli args: %w", err), nil
9190
}
9291

93-
if err := RunCliBuild(ctx, contextReader, opts.TarWriter, args...); err != nil {
92+
if err := RunCliBuild(ctx, logger, contextReader, opts.TarWriter, args...); err != nil {
9493
return fmt.Errorf("can't build artifacts: %w", err), nil
9594
}
9695

97-
logboek.Context(ctx).Default().LogF("Build is successful\n")
96+
logboek.Context(ctx).Default().LogLn("Build is successful")
97+
logger.Info("Build is successful")
9898

9999
cleanupFunc := func() error {
100100
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
@@ -107,21 +107,25 @@ func BuildReleaseArtifacts(ctx context.Context, opts BuildReleaseArtifactsOpts,
107107
return nil, cleanupFunc
108108
}
109109

110-
func RunCliBuild(ctx context.Context, contextReader *nio.PipeReader, tarWriter *nio.PipeWriter, args ...string) error {
110+
func RunCliBuild(ctx context.Context, logger hclog.Logger, contextReader *nio.PipeReader, tarWriter *nio.PipeWriter, args ...string) error {
111111
finalArgs := append([]string{"buildx", "build"}, args...)
112112
cmd := exec.CommandContext(ctx, "docker", finalArgs...)
113113
var stdErrBuf bytes.Buffer
114114
cmd.Stdout = tarWriter
115115
cmd.Stdin = contextReader
116116
cmd.Stderr = &stdErrBuf
117117

118-
if err := cmd.Run(); err != nil {
119-
if len := stdErrBuf.Len(); len > 0 {
120-
errSection, parseErr := extractRelevantLogs(&stdErrBuf)
121-
if parseErr == nil {
122-
return fmt.Errorf("build failed: %s %w", errSection.String(), err)
118+
defer func() {
119+
if stdErrBuf.Len() > 0 {
120+
scanner := bufio.NewScanner(&stdErrBuf)
121+
for scanner.Scan() {
122+
logger.Info(scanner.Text())
123+
logboek.Context(ctx).Default().LogLn(scanner.Text())
123124
}
124125
}
126+
}()
127+
128+
if err := cmd.Run(); err != nil {
125129
return fmt.Errorf("build failed: %w", err)
126130
}
127131

@@ -132,39 +136,6 @@ func RunCliBuild(ctx context.Context, contextReader *nio.PipeReader, tarWriter *
132136
return nil
133137
}
134138

135-
// this needs for parsing buildx logs due to the fact that buildx writes all to stderr
136-
// it will look for the first error section of the logs that contains the error
137-
// or if not found just lines starts with "error:" or "ERROR:"
138-
func extractRelevantLogs(stderr *bytes.Buffer) (bytes.Buffer, error) {
139-
scanner := bufio.NewScanner(stderr)
140-
141-
var errSection bytes.Buffer
142-
var foundSection bool
143-
reSectionStart := regexp.MustCompile(`^------$`)
144-
reError := regexp.MustCompile(`(?i)^error:`)
145-
146-
for scanner.Scan() {
147-
line := scanner.Text()
148-
149-
if reSectionStart.MatchString(line) {
150-
if foundSection {
151-
break
152-
}
153-
foundSection = true
154-
}
155-
156-
if foundSection || reError.MatchString(line) {
157-
errSection.WriteString(line + "\n")
158-
}
159-
}
160-
161-
if err := scanner.Err(); err != nil {
162-
return errSection, fmt.Errorf("error reading stderr: %w", err)
163-
}
164-
165-
return errSection, nil
166-
}
167-
168139
func setCliArgs(serviceDockerfilePathInContext string, secrets []secrets.Secret) ([]string, error) {
169140
args := []string{
170141
"--file", serviceDockerfilePathInContext,

0 commit comments

Comments
 (0)