Merge pull request #46 from sosedoff/capture-build-error
Handle docker image build failures
This commit is contained in:
commit
b18239ff77
6 changed files with 47 additions and 21 deletions
3
Makefile
3
Makefile
|
@ -17,6 +17,9 @@ export GITHUB_TOKEN = $(shell cat ~/.config/github/token)
|
|||
|
||||
default: check
|
||||
|
||||
test:
|
||||
go test -cover -short ./...
|
||||
|
||||
check:
|
||||
$(ACT) -ra check
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ func TestRunEvent(t *testing.T) {
|
|||
{"basic.workflow", "push", ""},
|
||||
{"pipe.workflow", "push", ""},
|
||||
{"fail.workflow", "push", "exit with `FAILURE`: 1"},
|
||||
{"buildfail.workflow", "push", "COPY failed"},
|
||||
{"regex.workflow", "push", "exit with `NEUTRAL`: 78"},
|
||||
{"gitref.workflow", "push", ""},
|
||||
{"env.workflow", "push", ""},
|
||||
|
@ -42,7 +43,7 @@ func TestRunEvent(t *testing.T) {
|
|||
if table.errorMessage == "" {
|
||||
assert.NilError(t, err, table.workflowPath)
|
||||
} else {
|
||||
assert.Error(t, err, table.errorMessage)
|
||||
assert.ErrorContains(t, err, table.errorMessage)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
2
actions/testdata/buildfail-action/Dockerfile
vendored
Normal file
2
actions/testdata/buildfail-action/Dockerfile
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
FROM alpine:3.8
|
||||
COPY foobar /foo/bar
|
8
actions/testdata/buildfail.workflow
vendored
Normal file
8
actions/testdata/buildfail.workflow
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
workflow "test" {
|
||||
on = "push"
|
||||
resolves = ["test-action"]
|
||||
}
|
||||
|
||||
action "test-action" {
|
||||
uses = "./buildfail-action"
|
||||
}
|
|
@ -51,7 +51,8 @@ func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor {
|
|||
|
||||
input.Logger.Debugf("Creating image from context dir '%s' with tag '%s'", input.ContextDir, input.ImageTag)
|
||||
resp, err := cli.ImageBuild(input.Ctx, buildContext, options)
|
||||
input.logDockerResponse(resp.Body, err != nil)
|
||||
|
||||
err = input.logDockerResponse(resp.Body, err != nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
@ -64,26 +65,38 @@ func (i *DockerExecutorInput) writeLog(isError bool, format string, args ...inte
|
|||
|
||||
}
|
||||
|
||||
func (i *DockerExecutorInput) logDockerResponse(dockerResponse io.ReadCloser, isError bool) {
|
||||
func (i *DockerExecutorInput) logDockerResponse(dockerResponse io.ReadCloser, isError bool) error {
|
||||
if dockerResponse == nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
defer dockerResponse.Close()
|
||||
|
||||
scanner := bufio.NewScanner(dockerResponse)
|
||||
msg := dockerMessage{}
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Bytes()
|
||||
|
||||
msg.ID = ""
|
||||
msg.Stream = ""
|
||||
msg.Error = ""
|
||||
msg.ErrorDetail.Message = ""
|
||||
msg.Status = ""
|
||||
msg.Progress = ""
|
||||
if err := json.Unmarshal(line, &msg); err == nil {
|
||||
|
||||
if err := json.Unmarshal(line, &msg); err != nil {
|
||||
i.writeLog(false, "Unable to unmarshal line [%s] ==> %v", string(line), err)
|
||||
continue
|
||||
}
|
||||
|
||||
if msg.Error != "" {
|
||||
i.writeLog(isError, "%s", msg.Error)
|
||||
return
|
||||
return errors.New(msg.Error)
|
||||
}
|
||||
|
||||
if msg.ErrorDetail.Message != "" {
|
||||
i.writeLog(isError, "%s", msg.ErrorDetail.Message)
|
||||
return errors.New(msg.Error)
|
||||
}
|
||||
|
||||
if msg.Status != "" {
|
||||
|
@ -97,9 +110,7 @@ func (i *DockerExecutorInput) logDockerResponse(dockerResponse io.ReadCloser, is
|
|||
} else {
|
||||
i.writeLog(false, "Unable to handle line: %s", string(line))
|
||||
}
|
||||
} else {
|
||||
i.writeLog(false, "Unable to unmarshal line [%s] ==> %v", string(line), err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue