2016-12-29 02:23:39 +02:00
|
|
|
package release
|
|
|
|
|
2016-12-29 03:21:49 +02:00
|
|
|
import (
|
|
|
|
"os"
|
2020-02-11 21:10:41 +02:00
|
|
|
"path/filepath"
|
2018-12-30 00:14:05 +02:00
|
|
|
"time"
|
2017-12-17 20:59:54 +02:00
|
|
|
|
2017-06-22 05:09:14 +02:00
|
|
|
"github.com/apex/log"
|
2017-12-18 04:53:48 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
2017-05-13 23:06:15 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/client"
|
2018-09-12 19:18:01 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/pipe"
|
2018-07-10 06:38:00 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/semerrgroup"
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2020-02-11 21:10:41 +02:00
|
|
|
"github.com/mattn/go-zglob"
|
2018-11-29 20:42:14 +02:00
|
|
|
"github.com/pkg/errors"
|
2016-12-29 03:21:49 +02:00
|
|
|
)
|
2016-12-29 02:23:39 +02:00
|
|
|
|
2019-06-29 16:02:40 +02:00
|
|
|
// ErrMultipleReleases indicates that multiple releases are defined. ATM only one of them is allowed
|
|
|
|
// See https://github.com/goreleaser/goreleaser/pull/809
|
2019-08-26 09:31:38 +02:00
|
|
|
var ErrMultipleReleases = errors.New("multiple releases are defined. Only one is allowed")
|
2019-06-29 16:02:40 +02:00
|
|
|
|
2016-12-30 16:41:59 +02:00
|
|
|
// Pipe for github release
|
2016-12-30 13:27:35 +02:00
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-12-02 23:53:19 +02:00
|
|
|
func (Pipe) String() string {
|
2020-03-06 06:25:09 +02:00
|
|
|
return "github/gitlab/gitea releases"
|
2016-12-30 13:27:35 +02:00
|
|
|
}
|
|
|
|
|
2017-12-02 23:53:19 +02:00
|
|
|
// Default sets the pipe defaults
|
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
2019-08-26 09:31:38 +02:00
|
|
|
numOfReleases := 0
|
|
|
|
if ctx.Config.Release.GitHub.String() != "" {
|
|
|
|
numOfReleases++
|
|
|
|
}
|
|
|
|
if ctx.Config.Release.GitLab.String() != "" {
|
|
|
|
numOfReleases++
|
|
|
|
}
|
|
|
|
if ctx.Config.Release.Gitea.String() != "" {
|
|
|
|
numOfReleases++
|
|
|
|
}
|
|
|
|
if numOfReleases > 1 {
|
2019-06-29 16:02:40 +02:00
|
|
|
return ErrMultipleReleases
|
|
|
|
}
|
|
|
|
|
2017-12-02 23:53:19 +02:00
|
|
|
if ctx.Config.Release.NameTemplate == "" {
|
|
|
|
ctx.Config.Release.NameTemplate = "{{.Tag}}"
|
|
|
|
}
|
2019-06-29 16:02:40 +02:00
|
|
|
|
2019-08-26 09:31:38 +02:00
|
|
|
switch ctx.TokenType {
|
|
|
|
case context.TokenTypeGitLab:
|
|
|
|
{
|
|
|
|
if ctx.Config.Release.GitLab.Name == "" {
|
|
|
|
repo, err := remoteRepo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx.Config.Release.GitLab = repo
|
2019-06-29 16:02:40 +02:00
|
|
|
}
|
2019-08-26 09:31:38 +02:00
|
|
|
|
|
|
|
return nil
|
2019-06-29 16:02:40 +02:00
|
|
|
}
|
2019-08-26 09:31:38 +02:00
|
|
|
case context.TokenTypeGitea:
|
|
|
|
{
|
|
|
|
if ctx.Config.Release.Gitea.Name == "" {
|
|
|
|
repo, err := remoteRepo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx.Config.Release.Gitea = repo
|
|
|
|
}
|
2019-06-29 16:02:40 +02:00
|
|
|
|
2019-08-26 09:31:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-06-29 16:02:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We keep github as default for now
|
2019-01-10 21:09:46 +02:00
|
|
|
if ctx.Config.Release.GitHub.Name == "" {
|
|
|
|
repo, err := remoteRepo()
|
|
|
|
if err != nil && !ctx.Snapshot {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx.Config.Release.GitHub = repo
|
2017-12-02 23:53:19 +02:00
|
|
|
}
|
2018-11-29 20:42:14 +02:00
|
|
|
|
|
|
|
// Check if we have to check the git tag for an indicator to mark as pre release
|
|
|
|
switch ctx.Config.Release.Prerelease {
|
|
|
|
case "auto":
|
2019-01-19 20:57:58 +02:00
|
|
|
if ctx.Semver.Prerelease != "" {
|
2018-11-29 20:42:14 +02:00
|
|
|
ctx.PreRelease = true
|
|
|
|
}
|
2018-12-04 15:36:51 +02:00
|
|
|
log.Debugf("pre-release was detected for tag %s: %v", ctx.Git.CurrentTag, ctx.PreRelease)
|
2018-11-29 20:42:14 +02:00
|
|
|
case "true":
|
|
|
|
ctx.PreRelease = true
|
|
|
|
}
|
2019-01-10 20:06:13 +02:00
|
|
|
log.Debugf("pre-release for tag %s set to %v", ctx.Git.CurrentTag, ctx.PreRelease)
|
2018-11-29 20:42:14 +02:00
|
|
|
|
2017-12-02 23:53:19 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-17 01:39:10 +02:00
|
|
|
// Publish github release
|
|
|
|
func (Pipe) Publish(ctx *context.Context) error {
|
2019-06-29 16:02:40 +02:00
|
|
|
c, err := client.New(ctx)
|
2017-12-17 20:59:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-17 01:39:10 +02:00
|
|
|
return doPublish(ctx, c)
|
2017-12-17 20:59:54 +02:00
|
|
|
}
|
|
|
|
|
2019-06-29 16:02:40 +02:00
|
|
|
func doPublish(ctx *context.Context, client client.Client) error {
|
2018-04-25 05:36:05 +02:00
|
|
|
if ctx.Config.Release.Disable {
|
2018-09-12 19:18:01 +02:00
|
|
|
return pipe.Skip("release pipe is disabled")
|
2018-04-25 05:36:05 +02:00
|
|
|
}
|
2017-06-22 05:09:14 +02:00
|
|
|
log.WithField("tag", ctx.Git.CurrentTag).
|
|
|
|
WithField("repo", ctx.Config.Release.GitHub.String()).
|
2018-10-27 00:31:06 +02:00
|
|
|
Info("creating or updating release")
|
2017-04-19 22:12:12 +02:00
|
|
|
body, err := describeBody(ctx)
|
2017-04-19 21:59:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-29 16:02:40 +02:00
|
|
|
releaseID, err := client.CreateRelease(ctx, body.String())
|
2016-12-29 03:21:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-19 21:01:11 +02:00
|
|
|
|
2020-02-11 21:10:41 +02:00
|
|
|
extraFiles, err := findFiles(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, path := range extraFiles {
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
return errors.Wrapf(err, "failed to upload %s", name)
|
|
|
|
}
|
|
|
|
ctx.Artifacts.Add(&artifact.Artifact{
|
|
|
|
Name: name,
|
|
|
|
Path: path,
|
|
|
|
Type: artifact.UploadableFile,
|
|
|
|
})
|
2019-10-19 21:01:11 +02:00
|
|
|
}
|
|
|
|
|
2020-02-11 21:10:41 +02:00
|
|
|
var filters = artifact.Or(
|
|
|
|
artifact.ByType(artifact.UploadableArchive),
|
|
|
|
artifact.ByType(artifact.UploadableBinary),
|
|
|
|
artifact.ByType(artifact.Checksum),
|
|
|
|
artifact.ByType(artifact.Signature),
|
|
|
|
artifact.ByType(artifact.LinuxPackage),
|
|
|
|
)
|
|
|
|
|
2019-10-19 21:01:11 +02:00
|
|
|
if len(ctx.Config.Release.IDs) > 0 {
|
2020-02-11 21:10:41 +02:00
|
|
|
filters = artifact.And(filters, artifact.ByIDs(ctx.Config.Release.IDs...))
|
2019-10-19 21:01:11 +02:00
|
|
|
}
|
|
|
|
|
2020-02-11 21:10:41 +02:00
|
|
|
filters = artifact.Or(filters, artifact.ByType(artifact.UploadableFile))
|
|
|
|
|
2019-10-19 21:01:11 +02:00
|
|
|
var g = semerrgroup.New(ctx.Parallelism)
|
2020-02-11 21:10:41 +02:00
|
|
|
for _, artifact := range ctx.Artifacts.Filter(filters).List() {
|
2017-04-14 17:07:40 +02:00
|
|
|
artifact := artifact
|
2017-01-14 16:55:52 +02:00
|
|
|
g.Go(func() error {
|
2020-02-11 21:10:41 +02:00
|
|
|
return upload(ctx, client, releaseID, artifact)
|
2017-01-14 16:55:52 +02:00
|
|
|
})
|
2016-12-29 03:21:49 +02:00
|
|
|
}
|
2016-12-29 18:17:49 +02:00
|
|
|
return g.Wait()
|
2016-12-29 02:23:39 +02:00
|
|
|
}
|
2016-12-29 03:21:49 +02:00
|
|
|
|
2020-03-31 15:16:37 +02:00
|
|
|
func upload(ctx *context.Context, cli client.Client, releaseID string, artifact *artifact.Artifact) error {
|
|
|
|
var try int
|
|
|
|
tryUpload := func() error {
|
|
|
|
try++
|
2020-02-11 21:10:41 +02:00
|
|
|
file, err := os.Open(artifact.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close() // nolint: errcheck
|
|
|
|
log.WithField("file", file.Name()).WithField("name", artifact.Name).Info("uploading to release")
|
2020-03-31 15:16:37 +02:00
|
|
|
if err := cli.Upload(ctx, releaseID, artifact, file); err != nil {
|
2020-03-22 22:03:31 +02:00
|
|
|
log.WithField("try", try).
|
|
|
|
WithField("artifact", artifact.Name).
|
|
|
|
WithError(err).
|
|
|
|
Warnf("failed to upload artifact, will retry")
|
2020-02-11 21:10:41 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-31 15:16:37 +02:00
|
|
|
|
|
|
|
var err error
|
|
|
|
loop:
|
|
|
|
for try < 10 {
|
|
|
|
err = tryUpload()
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch err.(type) {
|
|
|
|
case client.RetriableError:
|
|
|
|
time.Sleep(time.Duration(try*50) * time.Millisecond)
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
break loop
|
|
|
|
}
|
2020-02-11 21:10:41 +02:00
|
|
|
}
|
2020-03-31 15:16:37 +02:00
|
|
|
|
|
|
|
return errors.Wrapf(err, "failed to upload %s after %d tries", artifact.Name, try)
|
2020-02-11 21:10:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func findFiles(ctx *context.Context) (map[string]string, error) {
|
|
|
|
var result = map[string]string{}
|
|
|
|
for _, extra := range ctx.Config.Release.ExtraFiles {
|
|
|
|
if extra.Glob != "" {
|
|
|
|
files, err := zglob.Glob(extra.Glob)
|
|
|
|
if err != nil {
|
|
|
|
return result, errors.Wrapf(err, "globbing failed for pattern %s", extra.Glob)
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
info, err := os.Stat(file)
|
|
|
|
if err == nil && info.IsDir() {
|
|
|
|
log.Debugf("ignoring directory %s", file)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var name = filepath.Base(file)
|
|
|
|
if old, ok := result[name]; ok {
|
|
|
|
log.Warnf("overriding %s with %s for name %s", old, file, name)
|
|
|
|
}
|
|
|
|
result[name] = file
|
|
|
|
}
|
|
|
|
}
|
2016-12-29 03:21:49 +02:00
|
|
|
}
|
2020-02-11 21:10:41 +02:00
|
|
|
return result, nil
|
2016-12-29 03:21:49 +02:00
|
|
|
}
|