github.com/client9/goreleaser@v0.17.4-0.20170511023544-27e4b028926d/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  	"log"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/goreleaser/goreleaser/client"
    11  	"github.com/goreleaser/goreleaser/context"
    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.Println("Skipped because --skip-publish is set")
    31  		return nil
    32  	}
    33  	log.Println("Creating or updating release", ctx.Git.CurrentTag, "on", ctx.Config.Release.GitHub.String())
    34  	body, err := describeBody(ctx)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	releaseID, err := client.CreateRelease(ctx, body.String())
    39  	if err != nil {
    40  		return err
    41  	}
    42  	var g errgroup.Group
    43  	for _, artifact := range ctx.Artifacts {
    44  		artifact := artifact
    45  		g.Go(func() error {
    46  			return upload(ctx, client, releaseID, artifact)
    47  		})
    48  	}
    49  	return g.Wait()
    50  }
    51  
    52  func upload(ctx *context.Context, client client.Client, releaseID int, artifact string) error {
    53  	var path = filepath.Join(ctx.Config.Dist, artifact)
    54  	file, err := os.Open(path)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	defer func() { _ = file.Close() }()
    59  	log.Println("Uploading", file.Name())
    60  	return client.Upload(ctx, releaseID, artifact, file)
    61  }