artifacts_uploader.go 2.01 KB
Newer Older
1
package helpers
2
3

import (
4
	"errors"
5
6
	"io"
	"os"
7
	"path"
8
9
10
	"time"

	"github.com/Sirupsen/logrus"
11
	"github.com/urfave/cli"
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 {
20
	common.JobCredentials
Kamil Trzcinski's avatar
Kamil Trzcinski committed
21
22
23
	fileArchiver
	retryHelper
	network common.Network
24

25
26
	Name     string `long:"name" description:"The name of the archive"`
	ExpireIn string `long:"expire-in" description:"When to expire artifacts"`
27
28
}

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

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

39
40
	artifactsName := path.Base(c.Name) + ".zip"

41
	// Upload the data
42
	switch c.network.UploadRawArtifacts(c.JobCredentials, pr, artifactsName, c.ExpireIn) {
Kamil Trzcinski's avatar
Kamil Trzcinski committed
43
44
45
46
47
48
49
50
51
52
53
	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
	}
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
}

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
73
74
75
	err = c.doRetry(c.createAndUpload)
	if err != nil {
		logrus.Fatalln(err)
76
77
78
79
	}
}

func init() {
Kamil Trzcinski's avatar
Kamil Trzcinski committed
80
	common.RegisterCommand2("artifacts-uploader", "create and upload build artifacts (internal)", &ArtifactsUploaderCommand{
81
		network: network.NewGitLabClient(),
Kamil Trzcinski's avatar
Kamil Trzcinski committed
82
83
84
85
		retryHelper: retryHelper{
			Retry:     2,
			RetryTime: time.Second,
		},
86
		Name: "artifacts",
Kamil Trzcinski's avatar
Kamil Trzcinski committed
87
	})
88
}