github.com/szyn/goreleaser@v0.76.1-0.20180517112710-333da09a1297/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.Disable {
    25  		return nil
    26  	}
    27  	if ctx.Config.Release.NameTemplate == "" {
    28  		ctx.Config.Release.NameTemplate = "{{.Tag}}"
    29  	}
    30  	if ctx.Config.Release.GitHub.Name != "" {
    31  		return nil
    32  	}
    33  	repo, err := remoteRepo()
    34  	if err != nil && !ctx.Snapshot {
    35  		return err
    36  	}
    37  	ctx.Config.Release.GitHub = repo
    38  	return nil
    39  }
    40  
    41  // Run the pipe
    42  func (Pipe) Run(ctx *context.Context) error {
    43  	c, err := client.NewGitHub(ctx)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	return doRun(ctx, c)
    48  }
    49  
    50  func doRun(ctx *context.Context, c client.Client) error {
    51  	if ctx.Config.Release.Disable {
    52  		return pipeline.Skip("release pipe is disabled")
    53  	}
    54  	if ctx.SkipPublish {
    55  		return pipeline.ErrSkipPublishEnabled
    56  	}
    57  	log.WithField("tag", ctx.Git.CurrentTag).
    58  		WithField("repo", ctx.Config.Release.GitHub.String()).
    59  		Info("creating or updating release")
    60  	body, err := describeBody(ctx)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	releaseID, err := c.CreateRelease(ctx, body.String())
    65  	if err != nil {
    66  		return err
    67  	}
    68  	var g errgroup.Group
    69  	sem := make(chan bool, ctx.Parallelism)
    70  	for _, artifact := range ctx.Artifacts.Filter(
    71  		artifact.Or(
    72  			artifact.ByType(artifact.UploadableArchive),
    73  			artifact.ByType(artifact.UploadableBinary),
    74  			artifact.ByType(artifact.Checksum),
    75  			artifact.ByType(artifact.Signature),
    76  			artifact.ByType(artifact.LinuxPackage),
    77  		),
    78  	).List() {
    79  		sem <- true
    80  		artifact := artifact
    81  		g.Go(func() error {
    82  			defer func() {
    83  				<-sem
    84  			}()
    85  			return upload(ctx, c, releaseID, artifact)
    86  		})
    87  	}
    88  	return g.Wait()
    89  }
    90  
    91  func upload(ctx *context.Context, c client.Client, releaseID int64, artifact artifact.Artifact) error {
    92  	file, err := os.Open(artifact.Path)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	defer file.Close() // nolint: errcheck
    97  	log.WithField("file", file.Name()).WithField("name", artifact.Name).Info("uploading to release")
    98  	return c.Upload(ctx, releaseID, artifact.Name, file)
    99  }