Skip to content

Commit

Permalink
feat: use CI environment variables to figure out tag (#1327)
Browse files Browse the repository at this point in the history
* fix(git): Use CI envronment variables to figure out tag

This patch detects CI environments and uses the available tag information when
collecting the git tag.

This resolves issues where one commit has multiple tags.

Closes #1163
Closes #1311

* Update www/content/release.md

Co-Authored-By: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* Update www/content/release.md

Co-Authored-By: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* Update www/content/build.md

Co-Authored-By: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* Update www/content/release.md

Co-Authored-By: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* feat(doc): Document git tag override in environment

Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
aeneasr and caarlos0 committed Jan 31, 2020
1 parent abd66bf commit ca3a63a
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 2 deletions.
5 changes: 5 additions & 0 deletions internal/pipe/changelog/changelog.go
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
Expand Down Expand Up @@ -189,6 +190,10 @@ func gitLog(refs ...string) (string, error) {
}

func previous(tag string) (result string, err error) {
if tag := os.Getenv("GORELEASER_PREVIOUS_TAG"); tag != "" {
return tag, nil
}

result, err = git.Clean(git.Run("describe", "--tags", "--abbrev=0", fmt.Sprintf("tags/%s^", tag)))
if err != nil {
result, err = git.Clean(git.Run("rev-list", "--max-parents=0", "HEAD"))
Expand Down
24 changes: 24 additions & 0 deletions internal/pipe/changelog/changelog_test.go
Expand Up @@ -109,6 +109,30 @@ func TestChangelog(t *testing.T) {
require.NotEmpty(t, string(bts))
}

func TestChangelogPreviousTagEnv(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
testlib.GitInit(t)
testlib.GitCommit(t, "first")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "second")
testlib.GitTag(t, "v0.0.2")
testlib.GitCommit(t, "third")
testlib.GitTag(t, "v0.0.3")
var ctx = context.New(config.Project{
Dist: folder,
Changelog: config.Changelog{Filters: config.Filters{}},
})
ctx.Git.CurrentTag = "v0.0.3"
require.NoError(t, os.Setenv("GORELEASER_PREVIOUS_TAG", "v0.0.1"))
require.NoError(t, Pipe{}.Run(ctx))
require.NoError(t, os.Setenv("GORELEASER_PREVIOUS_TAG", ""))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
require.NotContains(t, ctx.ReleaseNotes, "first")
require.Contains(t, ctx.ReleaseNotes, "second")
require.Contains(t, ctx.ReleaseNotes, "third")
}

func TestChangelogForGitlab(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
Expand Down
8 changes: 7 additions & 1 deletion internal/pipe/git/git.go
@@ -1,14 +1,16 @@
package git

import (
"os"
"os/exec"
"strings"

"github.com/apex/log"
"github.com/pkg/errors"

"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/pkg/errors"
)

// Pipe that sets up git state
Expand Down Expand Up @@ -122,6 +124,10 @@ func getFullCommit() (string, error) {
}

func getTag() (string, error) {
if tag := os.Getenv("GORELEASER_CURRENT_TAG"); tag != "" {
return tag, nil
}

return git.Clean(git.Run("describe", "--tags", "--abbrev=0"))
}

Expand Down
42 changes: 41 additions & 1 deletion internal/pipe/git/git_test.go
Expand Up @@ -6,10 +6,12 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/assert"
)

func TestDescription(t *testing.T) {
Expand Down Expand Up @@ -189,3 +191,41 @@ func TestGitNotInPath(t *testing.T) {
assert.NoError(t, os.Setenv("PATH", ""))
assert.EqualError(t, Pipe{}.Run(context.New(config.Project{})), ErrNoGit.Error())
}

func TestTagFromCI(t *testing.T) {
_, back := testlib.Mktmp(t)
defer back()
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "commit1")
testlib.GitTag(t, "v0.0.1")
testlib.GitTag(t, "v0.0.2")

for _, tc := range []struct {
envs map[string]string
expected string
}{
// It is not possible to concisely figure out the tag if a commit has more than one tags. Git always
// returns the tags in lexicographical order (ASC), which implies that we expect v0.0.1 here.
// More details: https://github.com/goreleaser/goreleaser/issues/1163
{expected: "v0.0.1"},
{
envs: map[string]string{"GORELEASER_CURRENT_TAG": "v0.0.2"},
expected: "v0.0.2",
},
} {
for name, value := range tc.envs {
require.NoError(t, os.Setenv(name, value))
}

var ctx = &context.Context{
Config: config.Project{},
}
assert.NoError(t, Pipe{}.Run(ctx))
assert.Equal(t, tc.expected, ctx.Git.CurrentTag)

for name := range tc.envs {
require.NoError(t, os.Setenv(name, ""))
}
}
}
6 changes: 6 additions & 0 deletions www/content/build.md
Expand Up @@ -149,3 +149,9 @@ GOVERSION=$(go version) goreleaser
```

[hook]: /hooks

## Define Build Tag

GoReleaser uses `git describe` to get the build tag. You can set
a different build tag using the environment variable `GORELEASER_CURRENT_TAG`.
This is useful in scenarios where two tags point to the same commit.
4 changes: 4 additions & 0 deletions www/content/environment.md
Expand Up @@ -117,3 +117,7 @@ func main() {
```

You can override this by changing the `ldflags` option in the `build` section.

## Overriding Git Tags

You can force the [build tag](/build#define-build-tag) and [previous changelog tag](/release#define-previous-tag) using environment variables. This is useful in cases where one git commit is referenced by multiple git tags.
6 changes: 6 additions & 0 deletions www/content/release.md
Expand Up @@ -137,6 +137,12 @@ changelog:
- (?i)foo
```

### Define Previous Tag

GoReleaser uses `git describe` to get the previous tag used for generating the Changelog.
You can set a different build tag using the environment variable `GORELEASER_PREVIOUS_TAG`.
This is useful in scenarios where two tags point to the same commit.

## Custom release notes

You can specify a file containing your custom release notes, and
Expand Down

0 comments on commit ca3a63a

Please sign in to comment.