github.com/alloyci/alloy-runner@v1.0.1-0.20180222164613-925503ccafd6/commands/helpers/artifacts_uploader.go (about) 1 package helpers 2 3 import ( 4 "errors" 5 "io" 6 "os" 7 "path" 8 "time" 9 10 "github.com/Sirupsen/logrus" 11 "github.com/urfave/cli" 12 13 "gitlab.com/gitlab-org/gitlab-runner/common" 14 "gitlab.com/gitlab-org/gitlab-runner/helpers/archives" 15 "gitlab.com/gitlab-org/gitlab-runner/helpers/formatter" 16 "gitlab.com/gitlab-org/gitlab-runner/network" 17 ) 18 19 type ArtifactsUploaderCommand struct { 20 common.JobCredentials 21 fileArchiver 22 retryHelper 23 network common.Network 24 25 Name string `long:"name" description:"The name of the archive"` 26 ExpireIn string `long:"expire-in" description:"When to expire artifacts"` 27 } 28 29 func (c *ArtifactsUploaderCommand) createAndUpload() (bool, error) { 30 pr, pw := io.Pipe() 31 defer pr.Close() 32 33 // Create the archive 34 go func() { 35 err := archives.CreateZipArchive(pw, c.sortedFiles()) 36 pw.CloseWithError(err) 37 }() 38 39 artifactsName := path.Base(c.Name) + ".zip" 40 41 // Upload the data 42 switch c.network.UploadRawArtifacts(c.JobCredentials, pr, artifactsName, c.ExpireIn) { 43 case common.UploadSucceeded: 44 return false, nil 45 case common.UploadForbidden: 46 return false, os.ErrPermission 47 case common.UploadTooLarge: 48 return false, errors.New("Too large") 49 case common.UploadFailed: 50 return true, os.ErrInvalid 51 default: 52 return false, os.ErrInvalid 53 } 54 } 55 56 func (c *ArtifactsUploaderCommand) Execute(*cli.Context) { 57 formatter.SetRunnerFormatter() 58 59 if len(c.URL) == 0 || len(c.Token) == 0 { 60 logrus.Fatalln("Missing runner credentials") 61 } 62 if c.ID <= 0 { 63 logrus.Fatalln("Missing build ID") 64 } 65 66 // Enumerate files 67 err := c.enumerate() 68 if err != nil { 69 logrus.Fatalln(err) 70 } 71 72 // If the upload fails, exit with a non-zero exit code to indicate an issue? 73 err = c.doRetry(c.createAndUpload) 74 if err != nil { 75 logrus.Fatalln(err) 76 } 77 } 78 79 func init() { 80 common.RegisterCommand2("artifacts-uploader", "create and upload build artifacts (internal)", &ArtifactsUploaderCommand{ 81 network: network.NewGitLabClient(), 82 retryHelper: retryHelper{ 83 Retry: 2, 84 RetryTime: time.Second, 85 }, 86 Name: "artifacts", 87 }) 88 }