2016-12-29 02:23:39 +02:00
|
|
|
package release
|
|
|
|
|
2016-12-29 03:21:49 +02:00
|
|
|
import (
|
2020-09-21 19:47:51 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-12-29 03:21:49 +02:00
|
|
|
"os"
|
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"
|
2020-05-25 20:07:40 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/extrafiles"
|
2020-07-09 22:40:37 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/git"
|
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"
|
2016-12-29 03:21:49 +02:00
|
|
|
)
|
2016-12-29 02:23:39 +02:00
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// ErrMultipleReleases indicates that multiple releases are defined. ATM only one of them is allowed.
|
2019-06-29 16:02:40 +02:00
|
|
|
// 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
|
|
|
|
2020-05-26 05:48:10 +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
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Default sets the pipe defaults.
|
2017-12-02 23:53:19 +02:00
|
|
|
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
|
|
|
|
2020-07-06 22:30:00 +02:00
|
|
|
// nolint: exhaustive
|
2019-08-26 09:31:38 +02:00
|
|
|
switch ctx.TokenType {
|
|
|
|
case context.TokenTypeGitLab:
|
|
|
|
{
|
|
|
|
if ctx.Config.Release.GitLab.Name == "" {
|
2020-07-09 22:40:37 +02:00
|
|
|
repo, err := git.ExtractRepoFromConfig()
|
2019-08-26 09:31:38 +02:00
|
|
|
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 == "" {
|
2020-07-09 22:40:37 +02:00
|
|
|
repo, err := git.ExtractRepoFromConfig()
|
2019-08-26 09:31:38 +02:00
|
|
|
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 == "" {
|
2020-07-09 22:40:37 +02:00
|
|
|
repo, err := git.ExtractRepoFromConfig()
|
2019-01-10 21:09:46 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Publish the release.
|
2018-10-17 01:39:10 +02:00
|
|
|
func (Pipe) Publish(ctx *context.Context) error {
|
2020-04-29 20:09:00 +02:00
|
|
|
if ctx.SkipPublish {
|
|
|
|
return pipe.ErrSkipPublishEnabled
|
|
|
|
}
|
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-05-25 20:07:40 +02:00
|
|
|
extraFiles, err := extrafiles.Find(ctx.Config.Release.ExtraFiles)
|
2020-02-11 21:10:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, path := range extraFiles {
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
2020-09-21 19:47:51 +02:00
|
|
|
return fmt.Errorf("failed to upload %s: %w", name, err)
|
2020-02-11 21:10:41 +02:00
|
|
|
}
|
|
|
|
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),
|
2020-04-12 16:47:46 +02:00
|
|
|
artifact.ByType(artifact.UploadableSourceArchive),
|
2020-02-11 21:10:41 +02:00
|
|
|
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
|
|
|
|
}
|
2020-05-26 05:48:10 +02:00
|
|
|
defer file.Close()
|
2020-02-11 21:10:41 +02:00
|
|
|
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
|
|
|
|
for try < 10 {
|
|
|
|
err = tryUpload()
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-10 16:15:03 +02:00
|
|
|
if errors.As(err, &client.RetriableError{}) {
|
2020-03-31 15:16:37 +02:00
|
|
|
time.Sleep(time.Duration(try*50) * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
2020-11-10 16:15:03 +02:00
|
|
|
break
|
2020-02-11 21:10:41 +02:00
|
|
|
}
|
2020-03-31 15:16:37 +02:00
|
|
|
|
2020-09-21 19:47:51 +02:00
|
|
|
return fmt.Errorf("failed to upload %s after %d tries: %w", artifact.Name, try, err)
|
2020-02-11 21:10:41 +02:00
|
|
|
}
|