1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

feat: log build hooks (#1752)

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos Alexandro Becker 2020-08-15 14:18:31 -03:00 committed by GitHub
parent e614bf4d1d
commit 6eae126445
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 5 deletions

View File

@ -3,7 +3,9 @@
package build
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
@ -11,6 +13,7 @@ import (
"github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/ids"
"github.com/goreleaser/goreleaser/internal/logext"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/tmpl"
builders "github.com/goreleaser/goreleaser/pkg/build"
@ -215,15 +218,18 @@ func extFor(target string, flags config.FlagArray) string {
func run(ctx *context.Context, dir string, command, env []string) error {
/* #nosec */
var cmd = exec.CommandContext(ctx, command[0], command[1:]...)
var log = log.WithField("env", env).WithField("cmd", command)
var entry = log.WithField("cmd", command)
cmd.Env = env
var b bytes.Buffer
cmd.Stderr = io.MultiWriter(logext.NewErrWriter(entry), &b)
cmd.Stdout = io.MultiWriter(logext.NewWriter(entry), &b)
if dir != "" {
cmd.Dir = dir
}
log.Debug("running")
if out, err := cmd.CombinedOutput(); err != nil {
log.WithError(err).Debug("failed")
return errors.Wrapf(err, "%q", string(out))
entry.WithField("env", env).Debug("running")
if err := cmd.Run(); err != nil {
entry.WithError(err).Debug("failed")
return errors.Wrapf(err, "%q", b.String())
}
return nil
}

View File

@ -731,3 +731,28 @@ func TestHookInvalidShelCommand(t *testing.T) {
},
}))
}
func TestRunHookFailWithLogs(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
var config = config.Project{
Dist: folder,
Builds: []config.Build{
{
Lang: "fakeFail",
Binary: "testing",
Flags: []string{"-v"},
Hooks: config.HookConfig{
Pre: []config.BuildHook{
{Cmd: "sh -c 'echo foo; exit 1'"},
},
},
Targets: []string{"whatever"},
},
},
}
var ctx = context.New(config)
ctx.Git.CurrentTag = "2.4.5"
assert.EqualError(t, Pipe{}.Run(ctx), "pre hook failed: \"foo\\n\": exit status 1")
assert.Empty(t, ctx.Artifacts.List())
}