1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-03 13:11:48 +02:00
Manuel Vogel eb7ba2a294 feat: add gitlab for releases (#1038)
* outlines gitlab client integration

* makes client parameter more explicit

* adds gitlab url to config

* changes releaseID to string to adapt to gitlab

* updates to latest gitlab client lib 0.18

* fixes copy paster in gitlab upload func

* fixes gitlab typo in config

* adds gitlab token to env and context

* release now uses the client factory method

* skips brew pipe if it is not a github release

* add github tokentype to publish tests

* skips scoop pipe if it is not a github release

* corrects brew skip msg

* adds gitlab token to main test

* adds gitlab to release docs

* validates config and errors accordingly

* adapt release pipe name to include gitlab

* fixes gitlab client after testing

* moves not-configured brew and scoop pipe checks as first check

* adds more debug to gitlab client

* adapts changelog generation for gitlab markdown

* adds debug log for gitlab changelog

* env needs to run before changelog pipe

* moves gitlab default download url to default pipe

* moves multiple releases check to from config to release pipe

* release differs now for github and gitlab

* adds debug gitlab release update msgs

* moves env pipe as second after before because it determines the token type other pipes depend on

* adaptes error check on gitlab release creation

* Revert "adaptes error check on gitlab release creation"

This reverts commit 032024571c76140f8e2207ee01cc08088f37594b.

* simplifies gitlab client logic. removes comments

* skips tls verification for gitlab client if specified in config

* updates the docs

* adds clarification that brew and scoop are not supported if it is a gitlab release

* fixes copy paster in release.md

* adds missing blob pipe in defaults and publish due to missing in merge

* updates comment in gitlab client
2019-06-29 11:02:40 -03:00

147 lines
4.0 KiB
Go

package release
import (
"os"
"time"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/kamilsk/retry/v4"
"github.com/kamilsk/retry/v4/backoff"
"github.com/kamilsk/retry/v4/strategy"
"github.com/pkg/errors"
)
// ErrMultipleReleases indicates that multiple releases are defined. ATM only one of them is allowed
// See https://github.com/goreleaser/goreleaser/pull/809
var ErrMultipleReleases = errors.New("both gitlab and github releases are defined. Only one is allowed")
// Pipe for github release
type Pipe struct{}
func (Pipe) String() string {
return "GitHub/GitLab Releases"
}
// Default sets the pipe defaults
func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.Release.GitHub.String() != "" && ctx.Config.Release.GitLab.String() != "" {
return ErrMultipleReleases
}
if ctx.Config.Release.NameTemplate == "" {
ctx.Config.Release.NameTemplate = "{{.Tag}}"
}
// Note: do a switch on ctx.TokenType later if more clients are added
if ctx.TokenType == context.TokenTypeGitLab {
if ctx.Config.Release.GitLab.Name == "" {
repo, err := remoteRepo()
if err != nil {
return err
}
ctx.Config.Release.GitLab = repo
}
return nil
}
// We keep github as default for now
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":
if ctx.Semver.Prerelease != "" {
ctx.PreRelease = true
}
log.Debugf("pre-release was detected for tag %s: %v", ctx.Git.CurrentTag, ctx.PreRelease)
case "true":
ctx.PreRelease = true
}
log.Debugf("pre-release for tag %s set to %v", ctx.Git.CurrentTag, ctx.PreRelease)
return nil
}
// Publish github release
func (Pipe) Publish(ctx *context.Context) error {
c, err := client.New(ctx)
if err != nil {
return err
}
return doPublish(ctx, c)
}
func doPublish(ctx *context.Context, client client.Client) error {
if ctx.Config.Release.Disable {
return pipe.Skip("release pipe is disabled")
}
log.WithField("tag", ctx.Git.CurrentTag).
WithField("repo", ctx.Config.Release.GitHub.String()).
Info("creating or updating release")
body, err := describeBody(ctx)
if err != nil {
return err
}
releaseID, err := client.CreateRelease(ctx, body.String())
if err != nil {
return err
}
var g = semerrgroup.New(ctx.Parallelism)
for _, artifact := range ctx.Artifacts.Filter(
artifact.Or(
artifact.ByType(artifact.UploadableArchive),
artifact.ByType(artifact.UploadableBinary),
artifact.ByType(artifact.Checksum),
artifact.ByType(artifact.Signature),
artifact.ByType(artifact.LinuxPackage),
),
).List() {
artifact := artifact
g.Go(func() error {
var repeats uint
what := func(try uint) error {
repeats = try + 1
if uploadErr := upload(ctx, client, releaseID, artifact); uploadErr != nil {
log.WithFields(log.Fields{
"try": try,
"artifact": artifact.Name,
}).Warnf("failed to upload artifact, will retry")
return uploadErr
}
return nil
}
how := []func(uint, error) bool{
strategy.Limit(10),
strategy.Backoff(backoff.Linear(50 * time.Millisecond)),
}
if err := retry.Try(ctx, what, how...); err != nil {
return errors.Wrapf(err, "failed to upload %s after %d retries", artifact.Name, repeats)
}
return nil
})
}
return g.Wait()
}
func upload(ctx *context.Context, client client.Client, releaseID string, artifact artifact.Artifact) error {
file, err := os.Open(artifact.Path)
if err != nil {
return err
}
defer file.Close() // nolint: errcheck
log.WithField("file", file.Name()).WithField("name", artifact.Name).Info("uploading to release")
return client.Upload(ctx, releaseID, artifact.Name, file)
}