github.com/marianogappa/goreleaser@v0.26.2-0.20170715090149-96acd0a9fc46/pipeline/release/release.go (about)

     1  // Package release implements Pipe and manages github releases and its
     2  // artifacts.
     3  package release
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/apex/log"
    10  	"github.com/goreleaser/goreleaser/context"
    11  	"github.com/goreleaser/goreleaser/internal/client"
    12  	"golang.org/x/sync/errgroup"
    13  )
    14  
    15  // Pipe for github release
    16  type Pipe struct{}
    17  
    18  // Description of the pipe
    19  func (Pipe) Description() string {
    20  	return "Releasing to GitHub"
    21  }
    22  
    23  // Run the pipe
    24  func (Pipe) Run(ctx *context.Context) error {
    25  	return doRun(ctx, client.NewGitHub(ctx))
    26  }
    27  
    28  func doRun(ctx *context.Context, client client.Client) error {
    29  	if !ctx.Publish {
    30  		log.Warn("skipped because --skip-publish is set")
    31  		return nil
    32  	}
    33  	log.WithField("tag", ctx.Git.CurrentTag).
    34  		WithField("repo", ctx.Config.Release.GitHub.String()).
    35  		Info("creating or updating release")
    36  	body, err := describeBody(ctx)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	releaseID, err := client.CreateRelease(ctx, body.String())
    41  	if err != nil {
    42  		return err
    43  	}
    44  	var g errgroup.Group
    45  	sem := make(chan bool, 4)
    46  	for _, artifact := range ctx.Artifacts {
    47  		sem <- true
    48  		artifact := artifact
    49  		g.Go(func() error {
    50  			defer func() {
    51  				<-sem
    52  			}()
    53  			return upload(ctx, client, releaseID, artifact)
    54  		})
    55  	}
    56  	return g.Wait()
    57  }
    58  
    59  func upload(ctx *context.Context, client client.Client, releaseID int, artifact string) error {
    60  	var path = filepath.Join(ctx.Config.Dist, artifact)
    61  	file, err := os.Open(path)
    62  	if err != nil {
    63  		return err
    64  	}
    65  	defer func() { _ = file.Close() }()
    66  	_, name := filepath.Split(path)
    67  	log.WithField("file", file.Name()).WithField("name", name).Info("uploading to release")
    68  	return client.Upload(ctx, releaseID, name, file)
    69  }