forked from jenkins-x/jx
/
step_collect.go
290 lines (265 loc) · 7.26 KB
/
step_collect.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package cmd
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
jenkinsv1 "github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
// StepCollect contains the command line flags
type StepCollectOptions struct {
StepOptions
Provider string
GitHubPagesStepCollectOptions
HttpStepCollectOptions
Pattern []string
Classifier string
}
type GitHubPagesStepCollectOptions struct {
}
type HttpStepCollectOptions struct {
Destination string
}
type CollectProviderKind string
const (
GitHubPagesCollectProviderKind CollectProviderKind = "GitHub"
HttpCollectProviderKind CollectProviderKind = "Http"
)
var CollectProvidersKinds = []string{
string(GitHubPagesCollectProviderKind),
string(HttpCollectProviderKind),
}
var (
StepCollectLong = templates.LongDesc(`
This pipeline step collects the specified files that need storing from the build
`)
StepCollectExample = templates.Examples(`
jx step collect TODO
`)
)
func NewCmdStepCollect(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := StepCollectOptions{
StepOptions: StepOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "collect",
Short: "Collects the specified files that need storing from the build",
Long: StepCollectLong,
Example: StepCollectExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Provider, "provider", "", "", fmt.Sprintf("Specify the storage provider to use. Supported options are: %s", CollectProvidersKinds))
cmd.Flags().StringArrayVarP(&options.Pattern, "pattern", "", make([]string, 0), fmt.Sprintf("Specify the pattern to use to look for files"))
cmd.Flags().StringVarP(&options.HttpStepCollectOptions.Destination, "destination", "", "", fmt.Sprintf("Specify the HTTP endpoint to send each file to"))
cmd.Flags().StringVarP(&options.Classifier, "classifier", "", "", "A name which classifies this type of file e.g. test-reports, coverage")
return cmd
}
func (o *StepCollectOptions) Run() error {
if o.Provider == "" {
return errors.New("Must specify a provider using --provider")
}
if strings.ToLower(o.Provider) == strings.ToLower(string(GitHubPagesCollectProviderKind)) {
err := o.GitHubPagesStepCollectOptions.collect(*o)
return err
} else if strings.ToLower(o.Provider) == strings.ToLower(string(HttpCollectProviderKind)) {
return o.HttpStepCollectOptions.collect()
} else {
return errors.New(fmt.Sprintf("Unrecognized provider %s", o.Provider))
}
return nil
}
func (o *GitHubPagesStepCollectOptions) collect(options StepCollectOptions) (err error) {
gitRepoInfo, err := options.FindGitInfo("")
if err != nil {
return err
}
if !gitRepoInfo.IsGitHub() {
return errors.New(fmt.Sprintf("Git repo must be GitHub to use GitHub Pages but it is %s", gitRepoInfo))
}
// First copy files out of the way, as we're going to checkout a different git branch
collectedDir, err := ioutil.TempDir("", "jenkins-x-collect")
if err != nil {
return err
}
for _, p := range options.Pattern {
_, err = exec.Command("cp", "-r", p, collectedDir).Output()
if err != nil {
return err
}
}
gitClient := options.Git()
cwb, err := gitClient.Branch("")
// TODO duplicated below
defer func() {
if gitClient.Checkout("", cwb) != nil {
log.Errorf("Error checking out original banch %s\n", cwb)
}
}()
if err != nil {
return err
}
err = gitClient.FetchBranch("", "origin", "gh-pages:gh-pages")
if err != nil {
// swallow the error
log.Infof("No existing gh-pages branch")
}
remotes, err := gitClient.RemoteBranchNames("", "")
if err != nil {
return err
}
buildNo := options.getBuildNumber()
if options.Classifier == "" {
return errors.New("You must pass --classfier")
}
if !contains(remotes, "gh-pages") {
// branch doesn't exist, so we create it following the process on https://help.github.com/articles/creating-project-pages-using-the-command-line/
err = gitClient.CheckoutOrphan("", "gh-pages")
if err != nil {
return err
}
err = gitClient.RemoveForce("", ".")
if err != nil {
return err
}
err = gitClient.Remove("", ".gitignore")
if err != nil {
return err
}
}
err = gitClient.Checkout("", "gh-pages")
if err != nil {
return err
}
repoDir := filepath.Join("jenkins-x", buildNo, options.Classifier)
err = os.MkdirAll(repoDir, 0755)
if err != nil {
return err
}
urls := make([]string, 0)
err = filepath.Walk(collectedDir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
rPath := strings.TrimPrefix(strings.TrimPrefix(path, collectedDir), "/")
rDir := filepath.Dir(rPath)
err = os.MkdirAll(filepath.Join(repoDir, rDir), 0755)
if err != nil {
return err
}
if rPath != "" {
cmd := util.Command{
Name: "cp",
Args: []string{path, fmt.Sprintf("%s/%s", repoDir, rPath)},
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return err
}
url := fmt.Sprintf("https://%s.github.com/%s/%s/%s/%s", gitRepoInfo.Organisation, gitRepoInfo.Name, cwb, repoDir, rPath)
log.Infof("Publishing %s\n", util.ColorInfo(url))
urls = append(urls, url)
}
return nil
})
if err != nil {
return err
}
err = gitClient.Add("", repoDir)
if err != nil {
return err
}
err = gitClient.CommitDir("", fmt.Sprintf("Collecting files for build %s", buildNo))
if err != nil {
fmt.Println(err)
return err
}
err = gitClient.Push("")
if err != nil {
return err
}
err = gitClient.Checkout("", cwb)
if err != nil {
return err
}
f := options.Factory
client, ns, err := f.CreateJXClient()
if err != nil {
return errors.Wrap(err, "cannot create the JX client")
}
apisClient, err := options.CreateApiExtensionsClient()
if err != nil {
return err
}
err = kube.RegisterPipelineActivityCRD(apisClient)
if err != nil {
return err
}
activities := client.JenkinsV1().PipelineActivities(ns)
if err != nil {
return err
}
appName := ""
if gitRepoInfo != nil {
appName = gitRepoInfo.Name
}
pipeline := ""
build := options.getBuildNumber()
pipeline, build = options.getPipelineName(gitRepoInfo, pipeline, build, appName)
if pipeline != "" && build != "" {
name := kube.ToValidName(pipeline + "-" + build)
key := &kube.PromoteStepActivityKey{
PipelineActivityKey: kube.PipelineActivityKey{
Name: name,
Pipeline: pipeline,
Build: build,
},
}
a, _, err := key.GetOrCreate(activities)
if err != nil {
return err
}
a.Spec.Attachments = append(a.Spec.Attachments, jenkinsv1.Attachment{
Name: options.Classifier,
URLs: urls,
})
_, err = client.JenkinsV1().PipelineActivities(ns).Update(a)
if err != nil {
return err
}
}
return nil
}
func (o *GitHubPagesStepCollectOptions) contains(strings []string, str string) bool {
for _, s := range strings {
if str == s {
return true
}
}
return false
}
func (o *HttpStepCollectOptions) collect() error {
return nil
}