1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-09-16 09:26:52 +02:00

added hooks support

add support for pre and post hooks on build pipe
This commit is contained in:
Carlos Alexandro Becker
2017-01-21 20:02:51 -02:00
parent 9a2effeeec
commit 26a2471526
3 changed files with 36 additions and 7 deletions

View File

@@ -179,6 +179,13 @@ build:
# Defaults are 386 and amd64 # Defaults are 386 and amd64
goarch: goarch:
- amd64 - amd64
# Hooks can be used to customize the final binary, for example, to run
# generator or whatever you want.
# Default is both hooks empty.
hooks:
pre: touch somefile
post: ./script.sh
``` ```
### Archive customization ### Archive customization

View File

@@ -14,6 +14,12 @@ type Homebrew struct {
Dependencies []string Dependencies []string
} }
// Hooks define actions to run before and/or after something
type Hooks struct {
Pre string
Post string
}
// Build contains the build configuration section // Build contains the build configuration section
type Build struct { type Build struct {
Goos []string Goos []string
@@ -21,6 +27,7 @@ type Build struct {
Main string Main string
Ldflags string Ldflags string
BinaryName string `yaml:"binary_name"` BinaryName string `yaml:"binary_name"`
Hooks Hooks
} }
// Archive config used for the archive // Archive config used for the archive

View File

@@ -6,6 +6,7 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
"strings"
"github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/context"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
@@ -43,13 +44,27 @@ func build(name, goos, goarch string, ctx *context.Context) error {
ldflags := ctx.Config.Build.Ldflags + " -X main.version=" + ctx.Git.CurrentTag ldflags := ctx.Config.Build.Ldflags + " -X main.version=" + ctx.Git.CurrentTag
output := "dist/" + name + "/" + ctx.Config.Build.BinaryName + extFor(goos) output := "dist/" + name + "/" + ctx.Config.Build.BinaryName + extFor(goos)
log.Println("Building", output) log.Println("Building", output)
cmd := exec.Command( if ctx.Config.Build.Hooks.Pre != "" {
"go", cmd := strings.Split(ctx.Config.Build.Hooks.Pre, " ")
"build", if err := run(goos, goarch, cmd); err != nil {
"-ldflags="+ldflags, return err
"-o", output, }
ctx.Config.Build.Main, }
) cmd := []string{"go", "build", "-ldflags=" + ldflags, "-o", output, ctx.Config.Build.Main}
if err := run(goos, goarch, cmd); err != nil {
return err
}
if ctx.Config.Build.Hooks.Post != "" {
cmd := strings.Split(ctx.Config.Build.Hooks.Post, " ")
if err := run(goos, goarch, cmd); err != nil {
return err
}
}
return nil
}
func run(goos, goarch string, command []string) error {
cmd := exec.Command(command[0], command[1:]...)
cmd.Env = append( cmd.Env = append(
cmd.Env, cmd.Env,
"GOOS="+goos, "GOOS="+goos,