From d5c7af1db95e3d139ff95290d42c75feb26047c9 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sun, 3 Dec 2017 16:19:57 -0200 Subject: [PATCH] feat: support environment variables on ldflags Supports passing environment variables to ldflags by using .Env.VARNAME. Closes #426 --- docs/000-introduction.md | 4 ++-- docs/040-project.md | 1 - docs/050-build.md | 16 ++++++++++++++++ pipeline/build/ldflags.go | 13 +++++++++++++ pipeline/build/ldflags_test.go | 6 +++++- 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/docs/000-introduction.md b/docs/000-introduction.md index 196db00f6..55c808f97 100644 --- a/docs/000-introduction.md +++ b/docs/000-introduction.md @@ -22,5 +22,5 @@ that and rewrote the whole thing in Go. There are three ways to get going. 1. Install Goreleaser via go get (`goreleaser` command will be globally available) `go get github.com/goreleaser/goreleaser` -2. On a Mac use [Homebrew](https://github.com/goreleaser/homebrew-tap). -3. Install directly [from the binaries](https://github.com/goreleaser/goreleaser/releases/latest). +1. On a Mac use [Homebrew](https://github.com/goreleaser/homebrew-tap). +1. Install directly [from the binaries](https://github.com/goreleaser/goreleaser/releases/latest). diff --git a/docs/040-project.md b/docs/040-project.md index 18fe0f9da..fa09c551f 100644 --- a/docs/040-project.md +++ b/docs/040-project.md @@ -5,7 +5,6 @@ title: Project Name The project name is used in the name of the Brew formula, archives, etc. If none is given, it will be inferred from the name of the Git project. - ```yaml # .goreleaser.yml project_name: myproject diff --git a/docs/050-build.md b/docs/050-build.md index fa5324eed..946ff834d 100644 --- a/docs/050-build.md +++ b/docs/050-build.md @@ -78,3 +78,19 @@ builds: pre: rice embed-go post: ./script.sh ``` + +## Passing environment variables to ldflags + +You can do that by using `{{ .Env.VARIABLE_NAME }}` in the template, for +example: + +```yaml +builds: + - ldflags: -s -w -X "main.goversion={{.Env.GOVERSION}}" +``` + +Then you can run: + +```console +GOVERSION=$(go version) goreleaser +``` diff --git a/pipeline/build/ldflags.go b/pipeline/build/ldflags.go index cf6633616..527e96c6a 100644 --- a/pipeline/build/ldflags.go +++ b/pipeline/build/ldflags.go @@ -2,6 +2,8 @@ package build import ( "bytes" + "os" + "strings" "text/template" "time" @@ -14,6 +16,7 @@ type ldflagsData struct { Tag string Commit string Version string + Env map[string]string } func ldflags(ctx *context.Context, build config.Build) (string, error) { @@ -22,6 +25,7 @@ func ldflags(ctx *context.Context, build config.Build) (string, error) { Tag: ctx.Git.CurrentTag, Version: ctx.Version, Date: time.Now().UTC().Format(time.RFC3339), + Env: loadEnvs(), } var out bytes.Buffer t, err := template.New("ldflags").Parse(build.Ldflags) @@ -31,3 +35,12 @@ func ldflags(ctx *context.Context, build config.Build) (string, error) { err = t.Execute(&out, data) return out.String(), err } + +func loadEnvs() map[string]string { + r := map[string]string{} + for _, e := range os.Environ() { + env := strings.Split(e, "=") + r[env[0]] = env[1] + } + return r +} diff --git a/pipeline/build/ldflags_test.go b/pipeline/build/ldflags_test.go index ae3ea4250..2ed361302 100644 --- a/pipeline/build/ldflags_test.go +++ b/pipeline/build/ldflags_test.go @@ -1,6 +1,7 @@ package build import ( + "os" "testing" "github.com/goreleaser/goreleaser/config" @@ -12,10 +13,12 @@ func TestLdFlagsFullTemplate(t *testing.T) { var config = config.Project{ Builds: []config.Build{ { - Ldflags: "-s -w -X main.version={{.Version}} -X main.tag={{.Tag}} -X main.date={{.Date}} -X main.commit={{.Commit}}", + Ldflags: `-s -w -X main.version={{.Version}} -X main.tag={{.Tag}} -X main.date={{.Date}} -X main.commit={{.Commit}} -X "main.foo={{.Env.FOO}}"`, }, }, } + os.Setenv("FOO", "123") + defer os.Unsetenv("FOO") var ctx = &context.Context{ Git: context.GitInfo{ CurrentTag: "v1.2.3", @@ -31,6 +34,7 @@ func TestLdFlagsFullTemplate(t *testing.T) { assert.Contains(t, flags, "-X main.tag=v1.2.3") assert.Contains(t, flags, "-X main.commit=123") assert.Contains(t, flags, "-X main.date=") + assert.Contains(t, flags, `-X "main.foo=123"`) } func TestInvalidTemplate(t *testing.T) {