1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/pipeline/build/build.go
Carlos Alexandro Becker e76474d16d
simplified env for build
2017-01-23 09:27:28 -02:00

63 lines
1.3 KiB
Go

package build
import (
"bytes"
"errors"
"log"
"os"
"os/exec"
"github.com/goreleaser/goreleaser/context"
"golang.org/x/sync/errgroup"
)
// Pipe for build
type Pipe struct{}
// Description of the pipe
func (Pipe) Description() string {
return "Building binaries"
}
// Run the pipe
func (Pipe) Run(ctx *context.Context) error {
var g errgroup.Group
for _, goos := range ctx.Config.Build.Goos {
for _, goarch := range ctx.Config.Build.Goarch {
goos := goos
goarch := goarch
name, err := nameFor(ctx, goos, goarch)
if err != nil {
return err
}
ctx.Archives[goos+goarch] = name
g.Go(func() error {
return build(name, goos, goarch, ctx)
})
}
}
return g.Wait()
}
func build(name, goos, goarch string, ctx *context.Context) error {
ldflags := ctx.Config.Build.Ldflags + " -X main.version=" + ctx.Git.CurrentTag
output := "dist/" + name + "/" + ctx.Config.Build.BinaryName + extFor(goos)
log.Println("Building", output)
cmd := exec.Command(
"go",
"build",
"-ldflags="+ldflags,
"-o", output,
ctx.Config.Build.Main,
)
cmd.Env = append(cmd.Env, "GOOS="+goos, "GOARCH="+goarch)
cmd.Env = append(cmd.Env, os.Environ()...)
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stdout
if err := cmd.Run(); err != nil {
return errors.New(stdout.String())
}
return nil
}