artifacts_uploader.go 1.79 KB
Newer Older
1
package helpers
2
3
4
5
6
7
8
9
10

import (
	"io"
	"os"
	"time"

	"github.com/Sirupsen/logrus"
	"github.com/codegangsta/cli"

Kamil Trzcinski's avatar
Kamil Trzcinski committed
11
	"errors"
12
13
14
15
16
17
18
19
	"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
	"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers/archives"
	"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers/formatter"
	"gitlab.com/gitlab-org/gitlab-ci-multi-runner/network"
)

type ArtifactsUploaderCommand struct {
	common.BuildCredentials
Kamil Trzcinski's avatar
Kamil Trzcinski committed
20
21
22
	fileArchiver
	retryHelper
	network common.Network
23
24
}

Kamil Trzcinski's avatar
Kamil Trzcinski committed
25
func (c *ArtifactsUploaderCommand) createAndUpload() (bool, error) {
26
27
28
29
30
31
32
33
34
35
	pr, pw := io.Pipe()
	defer pr.Close()

	// Create the archive
	go func() {
		err := archives.CreateZipArchive(pw, c.sortedFiles())
		pw.CloseWithError(err)
	}()

	// Upload the data
Kamil Trzcinski's avatar
Kamil Trzcinski committed
36
37
38
39
40
41
42
43
44
45
46
47
	switch c.network.UploadRawArtifacts(c.BuildCredentials, pr, "artifacts.zip") {
	case common.UploadSucceeded:
		return false, nil
	case common.UploadForbidden:
		return false, os.ErrPermission
	case common.UploadTooLarge:
		return false, errors.New("Too large")
	case common.UploadFailed:
		return true, os.ErrInvalid
	default:
		return false, os.ErrInvalid
	}
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
}

func (c *ArtifactsUploaderCommand) Execute(*cli.Context) {
	formatter.SetRunnerFormatter()

	if len(c.URL) == 0 || len(c.Token) == 0 {
		logrus.Fatalln("Missing runner credentials")
	}
	if c.ID <= 0 {
		logrus.Fatalln("Missing build ID")
	}

	// Enumerate files
	err := c.enumerate()
	if err != nil {
		logrus.Fatalln(err)
	}

	// If the upload fails, exit with a non-zero exit code to indicate an issue?
Kamil Trzcinski's avatar
Kamil Trzcinski committed
67
68
69
	err = c.doRetry(c.createAndUpload)
	if err != nil {
		logrus.Fatalln(err)
70
71
72
73
	}
}

func init() {
Kamil Trzcinski's avatar
Kamil Trzcinski committed
74
75
76
77
78
79
80
	common.RegisterCommand2("artifacts-uploader", "create and upload build artifacts (internal)", &ArtifactsUploaderCommand{
		network: &network.GitLabClient{},
		retryHelper: retryHelper{
			Retry:     2,
			RetryTime: time.Second,
		},
	})
81
}