-
Notifications
You must be signed in to change notification settings - Fork 787
/
start_pipeline.go
287 lines (250 loc) · 6.19 KB
/
start_pipeline.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
package cmd
import (
"errors"
"fmt"
"io"
"net/url"
"sort"
"strings"
"time"
gojenkins "github.com/jenkins-x/golang-jenkins"
"github.com/jenkins-x/jx/pkg/prow"
v1 "k8s.io/api/core/v1"
"k8s.io/test-infra/prow/pjutil"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
build "github.com/knative/build/pkg/apis/build/v1alpha1"
prowjobv1 "k8s.io/test-infra/prow/apis/prowjobs/v1"
)
const (
repoOwnerEnv = "REPO_OWNER"
repoNameEnv = "REPO_NAME"
jmbrBranchName = "BRANCH_NAME"
jmbrSourceURL = "SOURCE_URL"
)
// StartPipelineOptions contains the command line options
type StartPipelineOptions struct {
GetOptions
Tail bool
Filter string
Jobs map[string]gojenkins.Job
ProwOptions prow.Options
}
var (
start_pipeline_long = templates.LongDesc(`
Starts the pipeline build.
`)
start_pipeline_example = templates.Examples(`
# Start a pipeline
jx start pipeline foo
# Select the pipeline to start
jx start pipeline
# Select the pipeline to start and tail the log
jx start pipeline -t
`)
)
// NewCmdStartPipeline creates the command
func NewCmdStartPipeline(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &StartPipelineOptions{
GetOptions: GetOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "pipeline [flags]",
Short: "Starts one or more pipelines",
Long: start_pipeline_long,
Example: start_pipeline_example,
Aliases: []string{"pipe", "pipeline", "build", "run"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().BoolVarP(&options.Tail, "tail", "t", false, "Tails the build log to the current terminal")
cmd.Flags().StringVarP(&options.Filter, "filter", "f", "", "Filters all the available jobs by those that contain the given text")
return cmd
}
// Run implements this command
func (o *StartPipelineOptions) Run() error {
kubeClient, err := o.KubeClient()
if err != nil {
return err
}
_, _, err = o.JXClient()
if err != nil {
return err
}
isProw, err := o.isProw()
if err != nil {
return err
}
args := o.Args
names := []string{}
o.ProwOptions = prow.Options{
KubeClient: kubeClient,
NS: o.currentNamespace,
}
if len(args) == 0 {
if isProw {
names, err = o.ProwOptions.GetReleaseJobs()
if err != nil {
return err
}
names = util.StringsContaining(names, o.Filter)
} else {
jobMap, err := o.getJobMap(o.Filter)
if err != nil {
return err
}
o.Jobs = jobMap
for k, _ := range o.Jobs {
names = append(names, k)
}
}
if len(names) == 0 {
return errors.New("no jobs found to trigger")
}
sort.Strings(names)
defaultName := ""
for _, n := range names {
if strings.HasSuffix(n, "/master") {
defaultName = n
break
}
}
name, err := util.PickNameWithDefault(names, "Which pipeline do you want to start: ", defaultName, "", o.In, o.Out, o.Err)
if err != nil {
return err
}
args = []string{name}
}
for _, a := range args {
if isProw {
err = o.createProwJob(a)
if err != nil {
return err
}
} else {
err = o.startJenkinsJob(a)
if err != nil {
return err
}
}
}
return nil
}
func (o *StartPipelineOptions) createProwJob(jobname string) error {
parts := strings.Split(jobname, "/")
if len(parts) != 3 {
return fmt.Errorf("job name [%s] does not match org/repo/branch format", jobname)
}
org := parts[0]
repo := parts[1]
branch := parts[2]
postSubmitJob, err := o.ProwOptions.GetPostSubmitJob(org, repo, branch)
if err != nil {
return err
}
jobSpec := prowjobv1.ProwJobSpec{
BuildSpec: postSubmitJob.BuildSpec,
Agent: prowjobv1.KnativeBuildAgent,
}
jobSpec.Type = prowjobv1.PostsubmitJob
//todo needs to change when we add support for multiple git providers with Prow
sourceURL := fmt.Sprintf("https://github.com/%s/%s.git", org, repo)
sourceSpec := &build.SourceSpec{
Git: &build.GitSourceSpec{
Url: sourceURL,
Revision: branch,
},
}
jobSpec.BuildSpec.Source = sourceSpec
env := map[string]string{}
// enrich with jenkins multi branch plugin env vars
env[jmbrBranchName] = branch
env[jmbrSourceURL] = jobSpec.BuildSpec.Source.Git.Url
env[repoOwnerEnv] = org
env[repoNameEnv] = repo
for i, step := range jobSpec.BuildSpec.Steps {
if len(step.Env) == 0 {
step.Env = []v1.EnvVar{}
}
for k, v := range env {
e := v1.EnvVar{
Name: k,
Value: v,
}
jobSpec.BuildSpec.Steps[i].Env = append(jobSpec.BuildSpec.Steps[i].Env, e)
}
}
if jobSpec.BuildSpec.Template != nil {
if len(jobSpec.BuildSpec.Template.Env) == 0 {
jobSpec.BuildSpec.Template.Env = []v1.EnvVar{}
}
for k, v := range env {
e := v1.EnvVar{
Name: k,
Value: v,
}
jobSpec.BuildSpec.Template.Env = append(jobSpec.BuildSpec.Template.Env, e)
}
}
p := pjutil.NewProwJob(jobSpec, nil)
p.Status = prowjobv1.ProwJobStatus{
State: prowjobv1.PendingState,
}
p.Spec.Refs = &prowjobv1.Refs{
BaseRef: branch,
Org: org,
Repo: repo,
}
client, err := o.KubeClient()
if err != nil {
return err
}
_, err = prow.CreateProwJob(client, o.currentNamespace, p)
return err
}
func (o *StartPipelineOptions) startJenkinsJob(name string) error {
job := o.Jobs[name]
jenkins, err := o.JenkinsClient()
if err != nil {
return err
}
// ignore errors as it could be there's no last build yet
previous, _ := jenkins.GetLastBuild(job)
params := url.Values{}
err = jenkins.Build(job, params)
if err != nil {
return err
}
i := 0
for {
last, err := jenkins.GetLastBuild(job)
// lets ignore the first query in case there's no build yet
if i > 0 && err != nil {
return err
}
i++
if last.Number != previous.Number {
log.Infof("Started build of %s at %s\n", util.ColorInfo(name), util.ColorInfo(last.Url))
log.Infof("%s %s\n", util.ColorStatus("view the log at:"), util.ColorInfo(util.UrlJoin(last.Url, "/console")))
if o.Tail {
return o.tailBuild(name, &last)
}
return nil
}
time.Sleep(time.Second)
}
}