1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/pipeline/release/release.go

95 lines
2.2 KiB
Go
Raw Normal View History

2017-04-14 20:39:32 +02:00
// Package release implements Pipe and manages github releases and its
// artifacts.
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
"github.com/goreleaser/goreleaser/internal/artifact"
2016-12-29 03:21:49 +02:00
2017-06-22 05:09:14 +02:00
"github.com/apex/log"
2017-01-15 00:01:32 +02:00
"github.com/goreleaser/goreleaser/context"
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 18:17:49 +02:00
"golang.org/x/sync/errgroup"
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{}
func (Pipe) String() string {
return "releasing to GitHub"
2016-12-30 13: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 != "" {
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)
}
func doRun(ctx *context.Context, c client.Client) error {
if !ctx.Publish {
2017-08-20 21:35:46 +02:00
return pipeline.Skip("--skip-publish is set")
}
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
}
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-17 20:59:54 +02:00
),
).List() {
2017-07-05 03:00:48 +02:00
sem <- true
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
}()
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-01-14 16:55:52 +02:00
defer func() { _ = file.Close() }()
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
}