1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-30 04:50:45 +02:00

feat: Add cleanup pipeline

This commit is contained in:
Dominik Schulz 2018-03-28 15:31:09 +02:00 committed by Carlos Alexandro Becker
parent 7b45c9abbc
commit cbdd90ddad
5 changed files with 112 additions and 0 deletions

View File

@ -219,6 +219,11 @@ type Git struct {
ShortHash bool `yaml:"short_hash,omitempty"`
}
// Before config
type Before struct {
Hooks []string `yaml:",omitempty"`
}
// Project includes all project configuration
type Project struct {
ProjectName string `yaml:"project_name,omitempty"`
@ -239,6 +244,7 @@ type Project struct {
Sign Sign `yaml:",omitempty"`
EnvFiles EnvFiles `yaml:"env_files,omitempty"`
Git Git `yaml:",omitempty"`
Before Before `yaml:",omitempty"`
// this is a hack ¯\_(ツ)_/¯
SingleBuild Build `yaml:"build,omitempty"`

20
docs/045-hooks.md Normal file
View File

@ -0,0 +1,20 @@
---
title: Global Hooks
---
Some builds may need pre-build steps before building, e.g. `go generate`.
The `before` section allows for global hooks which will be executed before
the build is started.
The configuration is very simple, here is a complete example:
```yml
# .goreleaser.yml
before:
hooks:
- make clean
- go generate ./...
```
If any of the hooks fails the build process is aborted.

View File

@ -18,6 +18,7 @@ import (
"github.com/goreleaser/goreleaser/pipeline"
"github.com/goreleaser/goreleaser/pipeline/archive"
"github.com/goreleaser/goreleaser/pipeline/artifactory"
"github.com/goreleaser/goreleaser/pipeline/before"
"github.com/goreleaser/goreleaser/pipeline/brew"
"github.com/goreleaser/goreleaser/pipeline/build"
"github.com/goreleaser/goreleaser/pipeline/changelog"
@ -43,6 +44,7 @@ var (
var pipes = []Piper{
defaults.Pipe{}, // load default configs
before.Pipe{}, // run global hooks before build
dist.Pipe{}, // ensure ./dist is clean
git.Pipe{}, // get and validate git repo state
effectiveconfig.Pipe{}, // writes the actual config (with defaults et al set) to dist

38
pipeline/before/before.go Normal file
View File

@ -0,0 +1,38 @@
package before
import (
"fmt"
"os/exec"
"strings"
"github.com/goreleaser/goreleaser/context"
)
// Pipe is a global hook pipe
type Pipe struct{}
// String is the name of this pipe
func (Pipe) String() string {
return "Run global hooks before starting the relase process"
}
// Default initialized the default values
func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.Before.Hooks == nil {
ctx.Config.Before.Hooks = []string{}
}
return nil
}
// Run executes the hooks
func (Pipe) Run(ctx *context.Context) error {
/* #nosec */
for _, step := range ctx.Config.Before.Hooks {
args := strings.Fields(step)
cmd := exec.Command(args[0], args[1:]...)
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("hook failed: %s\n%v", step, string(out))
}
}
return nil
}

View File

@ -0,0 +1,46 @@
package before
import (
"testing"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/stretchr/testify/assert"
)
func TestDescription(t *testing.T) {
assert.NotEmpty(t, Pipe{}.String())
}
func TestRunPipe(t *testing.T) {
for _, tc := range [][]string{
nil,
[]string{},
[]string{"go version"},
[]string{"go version", "go list"},
} {
ctx := context.New(
config.Project{
Before: config.Before{
Hooks: tc,
},
},
)
assert.NoError(t, Pipe{}.Run(ctx))
}
}
func TestRunPipeFail(t *testing.T) {
for _, tc := range [][]string{
[]string{"go tool foobar"},
} {
ctx := context.New(
config.Project{
Before: config.Before{
Hooks: tc,
},
},
)
assert.Error(t, Pipe{}.Run(ctx))
}
}