github.phpd.cn/goreleaser/goreleaser@v0.92.0/internal/pipe/release/release.go (about)

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