2017-04-14 15:39:32 -03:00
|
|
|
// Package release implements Pipe and manages github releases and its
|
|
|
|
// artifacts.
|
2016-12-28 22:23:39 -02:00
|
|
|
package release
|
|
|
|
|
2016-12-28 23:21:49 -02:00
|
|
|
import (
|
|
|
|
"os"
|
2017-01-29 22:33:08 -02:00
|
|
|
"path/filepath"
|
2016-12-28 23:21:49 -02:00
|
|
|
|
2017-06-22 00:09:14 -03:00
|
|
|
"github.com/apex/log"
|
2017-01-14 20:01:32 -02:00
|
|
|
"github.com/goreleaser/goreleaser/context"
|
2017-05-13 18:06:15 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/client"
|
2017-08-20 16:35:46 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pipeline"
|
2016-12-29 14:17:49 -02:00
|
|
|
"golang.org/x/sync/errgroup"
|
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-01-14 19:41:32 +01:00
|
|
|
// Description of the pipe
|
2017-01-14 15:14:35 -02:00
|
|
|
func (Pipe) Description() string {
|
2017-01-19 10:04:41 +01:00
|
|
|
return "Releasing to GitHub"
|
2016-12-30 09:27:35 -02:00
|
|
|
}
|
|
|
|
|
2016-12-30 12:41:59 -02:00
|
|
|
// Run the pipe
|
2017-01-14 14:06:57 -02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
2017-09-26 18:52:37 -03:00
|
|
|
client, err := client.NewGitHub(ctx)
|
2017-09-23 19:42:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return doRun(ctx, client)
|
2017-03-26 15:56:35 -03:00
|
|
|
}
|
|
|
|
|
2017-04-14 16:07:27 -03:00
|
|
|
func doRun(ctx *context.Context, client client.Client) error {
|
2017-04-18 13:10:13 -03:00
|
|
|
if !ctx.Publish {
|
2017-08-20 16:35:46 -03:00
|
|
|
return pipeline.Skip("--skip-publish is set")
|
2017-04-18 13:10:13 -03:00
|
|
|
}
|
2017-06-22 00:09:14 -03:00
|
|
|
log.WithField("tag", ctx.Git.CurrentTag).
|
|
|
|
WithField("repo", ctx.Config.Release.GitHub.String()).
|
2017-06-22 10:47:34 -03: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
|
|
|
|
}
|
|
|
|
releaseID, err := client.CreateRelease(ctx, body.String())
|
2016-12-28 23:21:49 -02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-29 14:17:49 -02:00
|
|
|
var g errgroup.Group
|
2017-07-15 16:49:52 -03:00
|
|
|
sem := make(chan bool, ctx.Parallelism)
|
2017-04-14 12:07:40 -03:00
|
|
|
for _, artifact := range ctx.Artifacts {
|
2017-07-04 22:00:48 -03:00
|
|
|
sem <- true
|
2017-04-14 12:07:40 -03:00
|
|
|
artifact := artifact
|
2017-01-14 12:55:52 -02:00
|
|
|
g.Go(func() error {
|
2017-07-04 22:00:48 -03:00
|
|
|
defer func() {
|
|
|
|
<-sem
|
|
|
|
}()
|
2017-04-14 12:07:40 -03:00
|
|
|
return upload(ctx, client, releaseID, artifact)
|
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
|
|
|
|
2017-04-14 16:07:27 -03:00
|
|
|
func upload(ctx *context.Context, client client.Client, releaseID int, artifact string) error {
|
2017-04-14 12:07:40 -03:00
|
|
|
var path = filepath.Join(ctx.Config.Dist, artifact)
|
2017-01-29 22:33:08 -02:00
|
|
|
file, err := os.Open(path)
|
2016-12-28 23:21:49 -02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-14 12:55:52 -02:00
|
|
|
defer func() { _ = file.Close() }()
|
2017-07-04 21:28:21 -03:00
|
|
|
_, name := filepath.Split(path)
|
|
|
|
log.WithField("file", file.Name()).WithField("name", name).Info("uploading to release")
|
|
|
|
return client.Upload(ctx, releaseID, name, file)
|
2016-12-28 23:21:49 -02:00
|
|
|
}
|