mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-18 03:56:52 +02:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
7255eb7461
@ -89,18 +89,31 @@ func doPublish(ctx *context.Context, c client.Client) error {
|
||||
).List() {
|
||||
artifact := artifact
|
||||
g.Go(func() error {
|
||||
return upload(ctx, c, releaseID, artifact)
|
||||
return upload(ctx, c, releaseID, artifact, 1)
|
||||
})
|
||||
}
|
||||
return g.Wait()
|
||||
}
|
||||
|
||||
func upload(ctx *context.Context, c client.Client, releaseID int64, artifact artifact.Artifact) error {
|
||||
const maxTries = 10
|
||||
|
||||
func upload(ctx *context.Context, c client.Client, releaseID int64, artifact artifact.Artifact, try int) error {
|
||||
file, err := os.Open(artifact.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close() // nolint: errcheck
|
||||
log.WithField("file", file.Name()).WithField("name", artifact.Name).Info("uploading to release")
|
||||
return c.Upload(ctx, releaseID, artifact.Name, file)
|
||||
err = c.Upload(ctx, releaseID, artifact.Name, file)
|
||||
if err != nil {
|
||||
if try == maxTries {
|
||||
return errors.Wrapf(err, "failed to upload %s after %d retries", artifact.Name, try)
|
||||
}
|
||||
log.WithFields(log.Fields{
|
||||
"try": try,
|
||||
"artifact": artifact.Name,
|
||||
}).Warnf("failed to upload artifact, will retry")
|
||||
return upload(ctx, c, releaseID, artifact, try+1)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package release
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -13,6 +12,7 @@ import (
|
||||
"github.com/goreleaser/goreleaser/internal/testlib"
|
||||
"github.com/goreleaser/goreleaser/pkg/config"
|
||||
"github.com/goreleaser/goreleaser/pkg/context"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -120,11 +120,39 @@ func TestRunPipeUploadFailure(t *testing.T) {
|
||||
client := &DummyClient{
|
||||
FailToUpload: true,
|
||||
}
|
||||
assert.Error(t, doPublish(ctx, client))
|
||||
assert.EqualError(t, doPublish(ctx, client), "failed to upload bin.tar.gz after 10 retries: upload failed")
|
||||
assert.True(t, client.CreatedRelease)
|
||||
assert.False(t, client.UploadedFile)
|
||||
}
|
||||
|
||||
func TestRunPipeUploadRetry(t *testing.T) {
|
||||
folder, err := ioutil.TempDir("", "goreleasertest")
|
||||
assert.NoError(t, err)
|
||||
tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
|
||||
assert.NoError(t, err)
|
||||
var config = config.Project{
|
||||
Release: config.Release{
|
||||
GitHub: config.Repo{
|
||||
Owner: "test",
|
||||
Name: "test",
|
||||
},
|
||||
},
|
||||
}
|
||||
var ctx = context.New(config)
|
||||
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
|
||||
ctx.Artifacts.Add(artifact.Artifact{
|
||||
Type: artifact.UploadableArchive,
|
||||
Name: "bin.tar.gz",
|
||||
Path: tarfile.Name(),
|
||||
})
|
||||
client := &DummyClient{
|
||||
FailFirstUpload: true,
|
||||
}
|
||||
assert.NoError(t, doPublish(ctx, client))
|
||||
assert.True(t, client.CreatedRelease)
|
||||
assert.True(t, client.UploadedFile)
|
||||
}
|
||||
|
||||
func TestPipeDisabled(t *testing.T) {
|
||||
var ctx = context.New(config.Project{
|
||||
Release: config.Release{
|
||||
@ -263,6 +291,7 @@ type DummyClient struct {
|
||||
CreatedRelease bool
|
||||
UploadedFile bool
|
||||
UploadedFileNames []string
|
||||
FailFirstUpload bool
|
||||
Lock sync.Mutex
|
||||
}
|
||||
|
||||
@ -278,13 +307,22 @@ func (client *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.
|
||||
return
|
||||
}
|
||||
|
||||
func (client *DummyClient) Upload(ctx *context.Context, releaseID int64, name string, file *os.File) (err error) {
|
||||
func (client *DummyClient) Upload(ctx *context.Context, releaseID int64, name string, file *os.File) error {
|
||||
client.Lock.Lock()
|
||||
defer client.Lock.Unlock()
|
||||
// ensure file is read to better mimic real behavior
|
||||
_, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unexpected error")
|
||||
}
|
||||
if client.FailToUpload {
|
||||
return errors.New("upload failed")
|
||||
}
|
||||
if client.FailFirstUpload {
|
||||
client.FailFirstUpload = false
|
||||
return errors.New("upload failed, should retry")
|
||||
}
|
||||
client.UploadedFile = true
|
||||
client.UploadedFileNames = append(client.UploadedFileNames, name)
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user