2016-12-29 02:23:39 +02:00
|
|
|
package release
|
|
|
|
|
2016-12-29 03:21:49 +02:00
|
|
|
import (
|
|
|
|
"os"
|
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
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2017-01-15 00:01:32 +02:00
|
|
|
"github.com/goreleaser/goreleaser/context"
|
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"
|
2017-08-20 21:35:46 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pipeline"
|
2016-12-29 03:21:49 +02:00
|
|
|
)
|
2016-12-29 02:23:39 +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 {
|
|
|
|
return "releasing to GitHub"
|
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 {
|
|
|
|
if ctx.Config.Release.NameTemplate == "" {
|
|
|
|
ctx.Config.Release.NameTemplate = "{{.Tag}}"
|
|
|
|
}
|
|
|
|
if ctx.Config.Release.GitHub.Name != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
repo, err := remoteRepo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx.Config.Release.GitHub = repo
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-17 20:59:54 +02:00
|
|
|
// Run the pipe
|
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
|
|
|
c, err := client.NewGitHub(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return doRun(ctx, c)
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:47:29 +02:00
|
|
|
func doRun(ctx *context.Context, c client.Client) error {
|
2017-04-18 18:10:13 +02:00
|
|
|
if !ctx.Publish {
|
2018-01-10 23:22:37 +02:00
|
|
|
return pipeline.ErrSkipPublish
|
2017-04-18 18:10:13 +02:00
|
|
|
}
|
2017-06-22 05:09:14 +02:00
|
|
|
log.WithField("tag", ctx.Git.CurrentTag).
|
|
|
|
WithField("repo", ctx.Config.Release.GitHub.String()).
|
2017-06-22 15:47:34 +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
|
|
|
|
}
|
2017-10-05 15:47:29 +02:00
|
|
|
releaseID, err := c.CreateRelease(ctx, body.String())
|
2016-12-29 03:21:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-29 18:17:49 +02:00
|
|
|
var g errgroup.Group
|
2017-07-15 21:49:52 +02:00
|
|
|
sem := make(chan bool, ctx.Parallelism)
|
2017-12-17 20: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 23:16:25 +02:00
|
|
|
artifact.ByType(artifact.Signature),
|
2017-12-18 01:26:03 +02:00
|
|
|
artifact.ByType(artifact.LinuxPackage),
|
2017-12-17 20:59:54 +02:00
|
|
|
),
|
|
|
|
).List() {
|
2017-07-05 03:00:48 +02:00
|
|
|
sem <- true
|
2017-04-14 17:07:40 +02:00
|
|
|
artifact := artifact
|
2017-01-14 16:55:52 +02:00
|
|
|
g.Go(func() error {
|
2017-07-05 03:00:48 +02:00
|
|
|
defer func() {
|
|
|
|
<-sem
|
|
|
|
}()
|
2017-10-05 15:47:29 +02:00
|
|
|
return upload(ctx, c, 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
|
|
|
|
2017-12-17 20:59:54 +02:00
|
|
|
func upload(ctx *context.Context, c client.Client, releaseID int, artifact artifact.Artifact) error {
|
|
|
|
file, err := os.Open(artifact.Path)
|
2016-12-29 03:21:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-18 03:04:29 +02:00
|
|
|
defer file.Close() // nolint: errcheck
|
2017-12-17 20: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-29 03:21:49 +02:00
|
|
|
}
|