Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CI environment variables to figure out tag #1327

Merged
merged 6 commits into from Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can describe both environments on the release page... or maybe on environment.md 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add it to environment.md but I would keep them separate because they impact different features. If there is a known issues / workarounds file, I would put it there also.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, yeah, makes sense... lets then add both to environment as well?

we can just link to the relevant topics 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whichever you prefer :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets do it then 🙏


Goreleaser uses `git describe` to get the build tag. You can set
aeneasr marked this conversation as resolved.
Show resolved Hide resolved
a different build tag using the environment variable `GORELEASER_CURRENT_TAG`.
This is useful in scenarios where two tags point to the same commit.
6 changes: 6 additions & 0 deletions www/content/release.md
Expand Up @@ -137,6 +137,12 @@ changelog:
- (?i)foo
```

### Define Previos Tag
aeneasr marked this conversation as resolved.
Show resolved Hide resolved

Goreleaser uses `git describe` to get the previous tag used for generating the Changelog.
aeneasr marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would document both environment variables here only.

. You can set a different build tag using the environment variable `GORELEASER_PREVIOUS_TAG`.
aeneasr marked this conversation as resolved.
Show resolved Hide resolved
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