gitee.com/mirrors_opencollective/goreleaser@v0.45.0/pipeline/release/release.go (about)

     1  package release
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/apex/log"
     7  	"golang.org/x/sync/errgroup"
     8  
     9  	"github.com/goreleaser/goreleaser/context"
    10  	"github.com/goreleaser/goreleaser/internal/artifact"
    11  	"github.com/goreleaser/goreleaser/internal/client"
    12  	"github.com/goreleaser/goreleaser/pipeline"
    13  )
    14  
    15  // Pipe for github release
    16  type Pipe struct{}
    17  
    18  func (Pipe) String() string {
    19  	return "releasing to GitHub"
    20  }
    21  
    22  // Default sets the pipe defaults
    23  func (Pipe) Default(ctx *context.Context) error {
    24  	if ctx.Config.Release.NameTemplate == "" {
    25  		ctx.Config.Release.NameTemplate = "{{.Tag}}"
    26  	}
    27  	if ctx.Config.Release.GitHub.Name != "" {
    28  		return nil
    29  	}
    30  	repo, err := remoteRepo()
    31  	if err != nil {
    32  		return err
    33  	}
    34  	ctx.Config.Release.GitHub = repo
    35  	return nil
    36  }
    37  
    38  // Run the pipe
    39  func (Pipe) Run(ctx *context.Context) error {
    40  	c, err := client.NewGitHub(ctx)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	return doRun(ctx, c)
    45  }
    46  
    47  func doRun(ctx *context.Context, c client.Client) error {
    48  	if !ctx.Publish {
    49  		return pipeline.ErrSkipPublish
    50  	}
    51  	log.WithField("tag", ctx.Git.CurrentTag).
    52  		WithField("repo", ctx.Config.Release.GitHub.String()).
    53  		Info("creating or updating release")
    54  	body, err := describeBody(ctx)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	releaseID, err := c.CreateRelease(ctx, body.String())
    59  	if err != nil {
    60  		return err
    61  	}
    62  	var g errgroup.Group
    63  	sem := make(chan bool, ctx.Parallelism)
    64  	for _, artifact := range ctx.Artifacts.Filter(
    65  		artifact.Or(
    66  			artifact.ByType(artifact.UploadableArchive),
    67  			artifact.ByType(artifact.UploadableBinary),
    68  			artifact.ByType(artifact.Checksum),
    69  			artifact.ByType(artifact.Signature),
    70  			artifact.ByType(artifact.LinuxPackage),
    71  		),
    72  	).List() {
    73  		sem <- true
    74  		artifact := artifact
    75  		g.Go(func() error {
    76  			defer func() {
    77  				<-sem
    78  			}()
    79  			return upload(ctx, c, releaseID, artifact)
    80  		})
    81  	}
    82  	return g.Wait()
    83  }
    84  
    85  func upload(ctx *context.Context, c client.Client, releaseID int, artifact artifact.Artifact) error {
    86  	file, err := os.Open(artifact.Path)
    87  	if err != nil {
    88  		return err
    89  	}
    90  	defer file.Close() // nolint: errcheck
    91  	log.WithField("file", file.Name()).WithField("name", artifact.Name).Info("uploading to release")
    92  	return c.Upload(ctx, releaseID, artifact.Name, file)
    93  }