2016-12-29 02:23:39 +02:00
|
|
|
package release
|
|
|
|
|
2016-12-29 03:21:49 +02:00
|
|
|
import (
|
2016-12-30 13:27:35 +02:00
|
|
|
"log"
|
2016-12-29 03:21:49 +02:00
|
|
|
"os"
|
2017-01-30 02:33:08 +02:00
|
|
|
"path/filepath"
|
2016-12-29 03:21:49 +02:00
|
|
|
|
2017-01-15 00:01:32 +02:00
|
|
|
"github.com/goreleaser/goreleaser/clients"
|
|
|
|
"github.com/goreleaser/goreleaser/context"
|
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{}
|
|
|
|
|
2017-01-14 20:41:32 +02:00
|
|
|
// Description of the pipe
|
2017-01-14 19:14:35 +02:00
|
|
|
func (Pipe) Description() string {
|
2017-01-19 11:04:41 +02:00
|
|
|
return "Releasing to GitHub"
|
2016-12-30 13:27:35 +02:00
|
|
|
}
|
|
|
|
|
2016-12-30 16:41:59 +02:00
|
|
|
// Run the pipe
|
2017-01-14 18:06:57 +02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
2017-03-26 19:46:25 +02:00
|
|
|
client := clients.NewGitHubClient(ctx)
|
|
|
|
log.Println("Creating or updating release", ctx.Git.CurrentTag, "on", ctx.Config.Release.GitHub.String())
|
|
|
|
releaseID, err := client.CreateRelease(ctx)
|
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-01-14 18:06:57 +02:00
|
|
|
for _, archive := range ctx.Archives {
|
2017-01-14 16:55:52 +02:00
|
|
|
archive := archive
|
|
|
|
g.Go(func() error {
|
2017-03-26 19:46:25 +02:00
|
|
|
return upload(ctx, client, releaseID, archive, ctx.Config.Archive.Format)
|
2017-01-14 16:55:52 +02:00
|
|
|
})
|
2017-01-30 02:33:08 +02:00
|
|
|
for _, format := range ctx.Config.FPM.Formats {
|
|
|
|
format := format
|
2017-01-30 01:55:32 +02:00
|
|
|
g.Go(func() error {
|
2017-03-26 19:46:25 +02:00
|
|
|
return upload(ctx, client, releaseID, archive, format)
|
2017-01-30 01:55:32 +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-03-26 19:46:25 +02:00
|
|
|
func upload(ctx *context.Context, client clients.Client, releaseID int, archive, format string) error {
|
2017-01-30 01:55:32 +02:00
|
|
|
archive = archive + "." + format
|
2017-03-26 02:31:16 +02:00
|
|
|
var path = filepath.Join(ctx.Config.Dist, archive)
|
2017-01-30 14:01:29 +02:00
|
|
|
// In case the file doesn't exist, we just ignore it.
|
|
|
|
// We do this because we can get invalid combinations of archive+format here,
|
|
|
|
// like darwinamd64 + deb or something like that.
|
|
|
|
// It's assumed that the archive pipe would fail the entire thing in case it fails to
|
|
|
|
// generate some archive, as well fpm pipe is expected to fail if something wrong happens.
|
|
|
|
// So, here, we just assume IsNotExist as an expected error.
|
|
|
|
// TODO: maybe add a list of files to upload in the context so we don't have to do this.
|
2017-01-30 02:33:08 +02:00
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
file, err := os.Open(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-01-19 11:04:41 +02:00
|
|
|
log.Println("Uploading", file.Name())
|
2017-03-26 19:46:25 +02:00
|
|
|
return client.Upload(ctx, releaseID, archive, file)
|
2016-12-29 03:21:49 +02:00
|
|
|
}
|