2016-12-28 22:23:39 -02:00
|
|
|
package release
|
|
|
|
|
2016-12-28 23:21:49 -02:00
|
|
|
import (
|
|
|
|
"os"
|
2018-12-30 01:14:05 +03:00
|
|
|
"time"
|
2017-12-17 16:59:54 -02:00
|
|
|
|
2017-06-22 00:09:14 -03:00
|
|
|
"github.com/apex/log"
|
2017-12-18 00:53:48 -02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
2017-05-13 18:06:15 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/client"
|
2018-09-12 14:18:01 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/pipe"
|
2018-07-09 21:38:00 -07:00
|
|
|
"github.com/goreleaser/goreleaser/internal/semerrgroup"
|
2018-08-14 23:50:20 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2018-12-30 01:14:05 +03:00
|
|
|
"github.com/kamilsk/retry"
|
|
|
|
"github.com/kamilsk/retry/backoff"
|
|
|
|
"github.com/kamilsk/retry/strategy"
|
2018-11-29 19:42:14 +01:00
|
|
|
"github.com/pkg/errors"
|
2016-12-28 23:21:49 -02:00
|
|
|
)
|
2016-12-28 22:23:39 -02:00
|
|
|
|
2016-12-30 12:41:59 -02:00
|
|
|
// Pipe for github release
|
2016-12-30 09:27:35 -02:00
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-12-02 19:53:19 -02:00
|
|
|
func (Pipe) String() string {
|
2018-11-03 15:25:01 -03:00
|
|
|
return "GitHub Releases"
|
2016-12-30 09:27:35 -02:00
|
|
|
}
|
|
|
|
|
2017-12-02 19:53:19 -02:00
|
|
|
// Default sets the pipe defaults
|
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
|
|
|
if ctx.Config.Release.NameTemplate == "" {
|
|
|
|
ctx.Config.Release.NameTemplate = "{{.Tag}}"
|
|
|
|
}
|
2019-01-10 17: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 19:53:19 -02:00
|
|
|
}
|
2018-11-29 19:42:14 +01: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 16:57:58 -02:00
|
|
|
if ctx.Semver.Prerelease != "" {
|
2018-11-29 19:42:14 +01:00
|
|
|
ctx.PreRelease = true
|
|
|
|
}
|
2018-12-04 11:36:51 -02:00
|
|
|
log.Debugf("pre-release was detected for tag %s: %v", ctx.Git.CurrentTag, ctx.PreRelease)
|
2018-11-29 19:42:14 +01:00
|
|
|
case "true":
|
|
|
|
ctx.PreRelease = true
|
|
|
|
}
|
2019-01-10 16:06:13 -02:00
|
|
|
log.Debugf("pre-release for tag %s set to %v", ctx.Git.CurrentTag, ctx.PreRelease)
|
2018-11-29 19:42:14 +01:00
|
|
|
|
2017-12-02 19:53:19 -02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-16 20:39:10 -03:00
|
|
|
// Publish github release
|
|
|
|
func (Pipe) Publish(ctx *context.Context) error {
|
2017-12-17 16:59:54 -02:00
|
|
|
c, err := client.NewGitHub(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-16 20:39:10 -03:00
|
|
|
return doPublish(ctx, c)
|
2017-12-17 16:59:54 -02:00
|
|
|
}
|
|
|
|
|
2018-10-16 20:39:10 -03:00
|
|
|
func doPublish(ctx *context.Context, c client.Client) error {
|
2018-04-24 20:36:05 -07:00
|
|
|
if ctx.Config.Release.Disable {
|
2018-09-12 14:18:01 -03:00
|
|
|
return pipe.Skip("release pipe is disabled")
|
2018-04-24 20:36:05 -07:00
|
|
|
}
|
2017-06-22 00:09:14 -03:00
|
|
|
log.WithField("tag", ctx.Git.CurrentTag).
|
|
|
|
WithField("repo", ctx.Config.Release.GitHub.String()).
|
2018-10-26 16:31:06 -06:00
|
|
|
Info("creating or updating release")
|
2017-04-19 17:12:12 -03:00
|
|
|
body, err := describeBody(ctx)
|
2017-04-19 16:59:26 -03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-05 15:47:29 +02:00
|
|
|
releaseID, err := c.CreateRelease(ctx, body.String())
|
2016-12-28 23:21:49 -02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-09 21:38:00 -07:00
|
|
|
var g = semerrgroup.New(ctx.Parallelism)
|
2017-12-17 16:59:54 -02:00
|
|
|
for _, artifact := range ctx.Artifacts.Filter(
|
|
|
|
artifact.Or(
|
|
|
|
artifact.ByType(artifact.UploadableArchive),
|
|
|
|
artifact.ByType(artifact.UploadableBinary),
|
|
|
|
artifact.ByType(artifact.Checksum),
|
2017-12-17 19:16:25 -02:00
|
|
|
artifact.ByType(artifact.Signature),
|
2017-12-17 21:26:03 -02:00
|
|
|
artifact.ByType(artifact.LinuxPackage),
|
2017-12-17 16:59:54 -02:00
|
|
|
),
|
|
|
|
).List() {
|
2017-04-14 12:07:40 -03:00
|
|
|
artifact := artifact
|
2017-01-14 12:55:52 -02:00
|
|
|
g.Go(func() error {
|
2018-12-30 01:14:05 +03:00
|
|
|
var repeats uint
|
|
|
|
action := func(try uint) error {
|
|
|
|
repeats = try + 1
|
|
|
|
if uploadErr := upload(ctx, c, releaseID, artifact); uploadErr != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"try": try,
|
|
|
|
"artifact": artifact.Name,
|
|
|
|
}).Warnf("failed to upload artifact, will retry")
|
|
|
|
return uploadErr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
strategies := []strategy.Strategy{
|
|
|
|
strategy.Limit(10),
|
|
|
|
strategy.Backoff(backoff.Linear(50 * time.Millisecond)),
|
|
|
|
}
|
|
|
|
if retryErr := retry.Retry(ctx.Done(), action, strategies...); retryErr != nil {
|
|
|
|
return errors.Wrapf(retryErr, "failed to upload %s after %d retries", artifact.Name, repeats)
|
|
|
|
}
|
|
|
|
return nil
|
2017-01-14 12:55:52 -02:00
|
|
|
})
|
2016-12-28 23:21:49 -02:00
|
|
|
}
|
2016-12-29 14:17:49 -02:00
|
|
|
return g.Wait()
|
2016-12-28 22:23:39 -02:00
|
|
|
}
|
2016-12-28 23:21:49 -02:00
|
|
|
|
2018-12-30 01:14:05 +03:00
|
|
|
func upload(ctx *context.Context, c client.Client, releaseID int64, artifact artifact.Artifact) error {
|
2017-12-17 16:59:54 -02:00
|
|
|
file, err := os.Open(artifact.Path)
|
2016-12-28 23:21:49 -02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-17 23:04:29 -02:00
|
|
|
defer file.Close() // nolint: errcheck
|
2017-12-17 16:59:54 -02:00
|
|
|
log.WithField("file", file.Name()).WithField("name", artifact.Name).Info("uploading to release")
|
2018-12-30 01:14:05 +03:00
|
|
|
return c.Upload(ctx, releaseID, artifact.Name, file)
|
2016-12-28 23:21:49 -02:00
|
|
|
}
|