diff --git a/README.md b/README.md index 00ce28782..75537c46b 100644 --- a/README.md +++ b/README.md @@ -190,9 +190,10 @@ build: # Custom ldflags template. # This is parsed with Golang template engine and the following variables # are available: - # - Version # - Date # - Commit + # - Tag + # - Version (Tag with the `v` prefix stripped) # The default is `-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}` # Date format is `2006-01-02_15:04:05` ldflags: -s -w -X main.build={{.Version}} diff --git a/pipeline/build/ldflags.go b/pipeline/build/ldflags.go index 51921b98e..2dacfb3ed 100644 --- a/pipeline/build/ldflags.go +++ b/pipeline/build/ldflags.go @@ -10,6 +10,7 @@ import ( type ldflagsData struct { Date string + Tag string Commit string Version string } @@ -17,7 +18,8 @@ type ldflagsData struct { func ldflags(ctx *context.Context) (string, error) { var data = ldflagsData{ Commit: ctx.Git.Commit, - Version: ctx.Git.CurrentTag, + Tag: ctx.Git.CurrentTag, + Version: ctx.Version, Date: time.Now().UTC().Format(time.RFC3339), } var out bytes.Buffer diff --git a/pipeline/build/ldflags_test.go b/pipeline/build/ldflags_test.go index bfb049bd8..4db97cc7a 100644 --- a/pipeline/build/ldflags_test.go +++ b/pipeline/build/ldflags_test.go @@ -12,7 +12,7 @@ func TestLdFlagsFullTemplate(t *testing.T) { assert := assert.New(t) var config = config.Project{ Build: config.Build{ - Ldflags: "-s -w -X main.version={{.Version}} -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}}", }, } var ctx = &context.Context{ @@ -20,12 +20,14 @@ func TestLdFlagsFullTemplate(t *testing.T) { CurrentTag: "v1.2.3", Commit: "123", }, - Config: config, + Version: "1.2.3", + Config: config, } flags, err := ldflags(ctx) assert.NoError(err) assert.Contains(flags, "-s -w") - assert.Contains(flags, "-X main.version=v1.2.3") + assert.Contains(flags, "-X main.version=1.2.3") + assert.Contains(flags, "-X main.tag=v1.2.3") assert.Contains(flags, "-X main.commit=123") assert.Contains(flags, "-X main.date=") }