1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-01 13:07:49 +02:00

feat: --auto-snapshot on dirty git tree (#2286)

* feat: --auto-snapshot

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* docs: fix

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* fix: workflow

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos Alexandro Becker 2021-06-04 23:09:12 -03:00 committed by GitHub
parent 39e5792993
commit bad1132e78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 5 deletions

View File

@ -96,7 +96,7 @@ jobs:
TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }} TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
run: | run: |
if [[ $GITHUB_REF == refs/tags/v* ]]; then if [[ $GITHUB_REF == refs/tags/v* ]]; then
./goreleaser ./goreleaser --auto-snapshot
elif [[ $GITHUB_REF == refs/heads/master ]]; then elif [[ $GITHUB_REF == refs/heads/master ]]; then
./goreleaser --snapshot ./goreleaser --snapshot
fi fi

View File

@ -8,6 +8,7 @@ import (
"github.com/caarlos0/ctrlc" "github.com/caarlos0/ctrlc"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/goreleaser/goreleaser/internal/middleware" "github.com/goreleaser/goreleaser/internal/middleware"
"github.com/goreleaser/goreleaser/internal/pipe/git"
"github.com/goreleaser/goreleaser/internal/pipeline" "github.com/goreleaser/goreleaser/internal/pipeline"
"github.com/goreleaser/goreleaser/pkg/context" "github.com/goreleaser/goreleaser/pkg/context"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -26,6 +27,7 @@ type releaseOpts struct {
releaseHeaderTmpl string releaseHeaderTmpl string
releaseFooterFile string releaseFooterFile string
releaseFooterTmpl string releaseFooterTmpl string
autoSnapshot bool
snapshot bool snapshot bool
skipPublish bool skipPublish bool
skipSign bool skipSign bool
@ -73,6 +75,7 @@ func newReleaseCmd() *releaseCmd {
cmd.Flags().StringVar(&root.opts.releaseNotesTmpl, "release-notes-tmpl", "", "Load custom release notes from a templated markdown file (overrides --release-notes)") cmd.Flags().StringVar(&root.opts.releaseNotesTmpl, "release-notes-tmpl", "", "Load custom release notes from a templated markdown file (overrides --release-notes)")
cmd.Flags().StringVar(&root.opts.releaseHeaderTmpl, "release-header-tmpl", "", "Load custom release notes header from a templated markdown file (overrides --release-header)") cmd.Flags().StringVar(&root.opts.releaseHeaderTmpl, "release-header-tmpl", "", "Load custom release notes header from a templated markdown file (overrides --release-header)")
cmd.Flags().StringVar(&root.opts.releaseFooterTmpl, "release-footer-tmpl", "", "Load custom release notes footer from a templated markdown file (overrides --release-footer)") cmd.Flags().StringVar(&root.opts.releaseFooterTmpl, "release-footer-tmpl", "", "Load custom release notes footer from a templated markdown file (overrides --release-footer)")
cmd.Flags().BoolVar(&root.opts.autoSnapshot, "auto-snapshot", false, "Automatically sets --snapshot if the repo is dirty")
cmd.Flags().BoolVar(&root.opts.snapshot, "snapshot", false, "Generate an unversioned snapshot release, skipping all validations and without publishing any artifacts (implies --skip-publish, --skip-announce and --skip-validate)") cmd.Flags().BoolVar(&root.opts.snapshot, "snapshot", false, "Generate an unversioned snapshot release, skipping all validations and without publishing any artifacts (implies --skip-publish, --skip-announce and --skip-validate)")
cmd.Flags().BoolVar(&root.opts.skipPublish, "skip-publish", false, "Skips publishing artifacts") cmd.Flags().BoolVar(&root.opts.skipPublish, "skip-publish", false, "Skips publishing artifacts")
cmd.Flags().BoolVar(&root.opts.skipAnnounce, "skip-announce", false, "Skips announcing releases (implies --skip-validate)") cmd.Flags().BoolVar(&root.opts.skipAnnounce, "skip-announce", false, "Skips announcing releases (implies --skip-validate)")
@ -123,6 +126,10 @@ func setupReleaseContext(ctx *context.Context, options releaseOpts) *context.Con
ctx.ReleaseFooterFile = options.releaseFooterFile ctx.ReleaseFooterFile = options.releaseFooterFile
ctx.ReleaseFooterTmpl = options.releaseFooterTmpl ctx.ReleaseFooterTmpl = options.releaseFooterTmpl
ctx.Snapshot = options.snapshot ctx.Snapshot = options.snapshot
if options.autoSnapshot && git.CheckDirty() != nil {
log.Info("git repo is dirty and --auto-snapshot is set, implying --snapshot")
ctx.Snapshot = true
}
ctx.SkipPublish = ctx.Snapshot || options.skipPublish ctx.SkipPublish = ctx.Snapshot || options.skipPublish
ctx.SkipAnnounce = ctx.Snapshot || options.skipPublish || options.skipAnnounce ctx.SkipAnnounce = ctx.Snapshot || options.skipPublish || options.skipAnnounce
ctx.SkipValidate = ctx.Snapshot || options.skipValidate ctx.SkipValidate = ctx.Snapshot || options.skipValidate

View File

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"path/filepath"
"testing" "testing"
"github.com/goreleaser/goreleaser/pkg/config" "github.com/goreleaser/goreleaser/pkg/config"
@ -15,6 +16,27 @@ func TestRelease(t *testing.T) {
require.NoError(t, cmd.cmd.Execute()) require.NoError(t, cmd.cmd.Execute())
} }
func TestReleaseAutoSnapshot(t *testing.T) {
t.Run("clean", func(t *testing.T) {
setup(t)
cmd := newReleaseCmd()
cmd.cmd.SetArgs([]string{"--auto-snapshot", "--skip-publish"})
require.NoError(t, cmd.cmd.Execute())
require.FileExists(t, "dist/fake_0.0.2_checksums.txt", "should not have run with --snapshot")
})
t.Run("dirty", func(t *testing.T) {
setup(t)
createFile(t, "foo", "force dirty tree")
cmd := newReleaseCmd()
cmd.cmd.SetArgs([]string{"--auto-snapshot", "--skip-publish"})
require.NoError(t, cmd.cmd.Execute())
matches, err := filepath.Glob("./dist/fake_v0.0.2-SNAPSHOT-*_checksums.txt")
require.NoError(t, err)
require.Len(t, matches, 1, "should have implied --snapshot")
})
}
func TestReleaseInvalidConfig(t *testing.T) { func TestReleaseInvalidConfig(t *testing.T) {
setup(t) setup(t)
createFile(t, "goreleaser.yml", "foo: bar") createFile(t, "goreleaser.yml", "foo: bar")

View File

@ -119,11 +119,10 @@ func validate(ctx *context.Context) error {
if _, err := os.Stat(".git/shallow"); err == nil { if _, err := os.Stat(".git/shallow"); err == nil {
log.Warn("running against a shallow clone - check your CI documentation at https://goreleaser.com/ci") log.Warn("running against a shallow clone - check your CI documentation at https://goreleaser.com/ci")
} }
out, err := git.Run("status", "--porcelain") if err := CheckDirty(); err != nil {
if strings.TrimSpace(out) != "" || err != nil { return err
return ErrDirty{status: out}
} }
_, err = git.Clean(git.Run("describe", "--exact-match", "--tags", "--match", ctx.Git.CurrentTag)) _, err := git.Clean(git.Run("describe", "--exact-match", "--tags", "--match", ctx.Git.CurrentTag))
if err != nil { if err != nil {
return ErrWrongRef{ return ErrWrongRef{
commit: ctx.Git.Commit, commit: ctx.Git.Commit,
@ -133,6 +132,15 @@ func validate(ctx *context.Context) error {
return nil return nil
} }
// CheckDirty returns an error if the current git repository is dirty.
func CheckDirty() error {
out, err := git.Run("status", "--porcelain")
if strings.TrimSpace(out) != "" || err != nil {
return ErrDirty{status: out}
}
return nil
}
func getBranch() (string, error) { func getBranch() (string, error) {
return git.Clean(git.Run("rev-parse", "--abbrev-ref", "HEAD", "--quiet")) return git.Clean(git.Run("rev-parse", "--abbrev-ref", "HEAD", "--quiet"))
} }

View File

@ -9,6 +9,7 @@ goreleaser release [flags]
## Options ## Options
``` ```
--auto-snapshot Automatically sets --snapshot if the repo is dirty
-f, --config string Load configuration from file -f, --config string Load configuration from file
-h, --help help for release -h, --help help for release
-k, --key string GoReleaser Pro license key [$GORELEASER_KEY] -k, --key string GoReleaser Pro license key [$GORELEASER_KEY]