1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00

124 lines
3.3 KiB
Go
Raw Normal View History

2016-12-28 22:23:39 -02:00
package release
2016-12-28 23:21:49 -02:00
import (
"os"
"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"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/kamilsk/retry"
"github.com/kamilsk/retry/backoff"
"github.com/kamilsk/retry/strategy"
"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{}
func (Pipe) String() string {
2018-11-03 15:25:01 -03:00
return "GitHub Releases"
2016-12-30 09:27:35 -02:00
}
// Default sets the pipe defaults
func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.Release.NameTemplate == "" {
ctx.Config.Release.NameTemplate = "{{.Tag}}"
}
if ctx.Config.Release.GitHub.Name == "" {
repo, err := remoteRepo()
if err != nil && !ctx.Snapshot {
return err
}
ctx.Config.Release.GitHub = repo
}
// 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 != "" {
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)
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)
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 {
if ctx.Config.Release.Disable {
2018-09-12 14:18:01 -03:00
return pipe.Skip("release pipe is disabled")
}
2017-06-22 00:09:14 -03:00
log.WithField("tag", ctx.Git.CurrentTag).
WithField("repo", ctx.Config.Release.GitHub.String()).
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
}
releaseID, err := c.CreateRelease(ctx, body.String())
2016-12-28 23:21:49 -02:00
if err != nil {
return err
}
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() {
artifact := artifact
2017-01-14 12:55:52 -02:00
g.Go(func() error {
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
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")
return c.Upload(ctx, releaseID, artifact.Name, file)
2016-12-28 23:21:49 -02:00
}