This repository has been archived by the owner on Feb 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
project_build_artifacts.go
193 lines (138 loc) · 4.78 KB
/
project_build_artifacts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright © 2016-present Thomas Rabaix <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package commands
import (
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/mitchellh/cli"
gitlab "gopkg.in/plouc/go-gitlab-client.v1"
helper "github.com/rande/gitlab-ci-helper"
)
type ProjectBuildArtifactCommand struct {
Ui cli.Ui
Verbose bool
ExtractPath string
ArtifactsFile string
BuildId string
Job string
Ref string
Project string
}
func (c *ProjectBuildArtifactCommand) Run(args []string) int {
flags := flag.NewFlagSet("project:builds:artifacts", flag.ContinueOnError)
flags.Usage = func() {
c.Ui.Output(c.Help())
}
flags.BoolVar(&c.Verbose, "verbose", false, "")
flags.StringVar(&c.ArtifactsFile, "file", "artifacts.zip", "Artifacts file name")
flags.StringVar(&c.ExtractPath, "path", "", "The path to extract the artifacts")
flags.StringVar(&c.BuildId, "build", "", "The build number to get the artifacts")
flags.StringVar(&c.Job, "job", "", "The job to search the artifacts")
flags.StringVar(&c.Ref, "ref", helper.GetEnv("CI_COMMIT_SHA", os.Getenv("CI_BUILD_REF")), "The reference (sha1) to search the artifacts")
flags.StringVar(&c.Project, "project", os.Getenv("CI_PROJECT_ID"), "The project reference")
if err := flags.Parse(args); err != nil {
return 1
}
args = flags.Args()
if len(args) != 0 {
flags.Usage()
c.Ui.Error(fmt.Sprintf("Error: %s", "Invalid number of arguments"))
return 1
}
config := helper.NewConfig()
client := gitlab.NewGitlab(config.Gitlab.Host, config.Gitlab.ApiPath, config.Gitlab.Token)
project, err := helper.GetProject(c.Project, client)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error: %s", err.Error()))
return 1
}
c.Ui.Output(fmt.Sprintf("Found project: %s/%s (id: %d)", project.Namespace.Name, project.Name, project.Id))
var build *gitlab.Build
if len(c.BuildId) > 0 {
build, err = helper.GetBuild(project, c.BuildId, client)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error: %s", err.Error()))
return 1
}
} else if len(c.Ref) > 0 {
builds, err := client.ProjectCommitBuilds(strconv.FormatInt(int64(project.Id), 10), c.Ref)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error: %s", err.Error()))
return 1
}
for _, b := range builds {
fmt.Printf("\nname: %s, job: %s\n", b.Name, c.Job)
if b.Name == c.Job {
build = b
break
}
}
}
if build == nil {
c.Ui.Error(fmt.Sprintf("Error: %s", "Unable to found the build"))
return 1
}
c.Ui.Output(fmt.Sprintf("Found build - stage:%s status:%s id:%d", build.Stage, build.Status, build.Id))
fp, err := os.Create(c.ArtifactsFile)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error: %s", err.Error()))
return 1
}
r, err := client.ProjectBuildArtifacts(strconv.FormatInt(int64(project.Id), 10), strconv.FormatInt(int64(build.Id), 10))
if err != nil {
c.Ui.Error(fmt.Sprintf("Error: %s", err.Error()))
return 1
}
c.Ui.Output(fmt.Sprintf("Downloading artifacts... (%s)", fp.Name()))
written, err := io.Copy(fp, r)
if written == 0 {
c.Ui.Error(fmt.Sprintf("Error: %s", "No byte downloaded"))
return 1
}
if err != nil {
c.Ui.Error(fmt.Sprintf("Error: %s", err.Error()))
return 1
}
if len(c.ExtractPath) > 0 {
c.Ui.Output(fmt.Sprintf("Extracting package... (%s)", c.ExtractPath))
err = helper.Unzip(c.ArtifactsFile, c.ExtractPath)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error: %s", err.Error()))
return 1
}
// delete artifacts.zip
os.Remove(c.ArtifactsFile)
}
c.Ui.Output(fmt.Sprintf("Done!"))
return 0
}
func (c *ProjectBuildArtifactCommand) Synopsis() string {
return "Download artifact from a job."
}
func (c *ProjectBuildArtifactCommand) Help() string {
helpText := `
Usage: gitlab-ci-helper project:builds:artifacts [options]
Download an artifacts and extract it if the 'path' option is provided
Options:
-project=XX The project reference (default: CI_PROJECT_ID)
-build=XX The build number used to retrieved the related artifact
-job=XX The job to search the build (must be used with -ref, default: package)
-ref=XX The sha1 linked to the build (must be used with -stage,
default 9.X: CI_COMMIT_SHA / 8.x: CI_BUILD_REF)
-file=artifacts.zip The path to the artifact file (default: artifacts.zip)
-path=./package The path to extract the command. If not set, the artifact will not
be extracted.
-verbose Add verbose information to the output
Credentials are retrieved from environment:
GITLAB_HOST The gitlab host
GITLAB_TOKEN The user's token
GITLAB_API_PATH (optional) the api path, default to: "/api/v4"
`
return strings.TrimSpace(helpText)
}