This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
import_jenkins.go
279 lines (245 loc) · 9.31 KB
/
import_jenkins.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
package importcmd
import (
"fmt"
"net/url"
"time"
"github.com/jenkins-x-labs/trigger-pipeline/pkg/jenkinsutil"
"github.com/jenkins-x-labs/trigger-pipeline/pkg/jenkinsutil/factory"
gojenkins "github.com/jenkins-x/golang-jenkins"
v1 "github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/jenkins"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/pipelinescheduler"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
)
// ImportProjectIntoJenkins imports a MultiBranchProject into Jenkins for the given git URL
func (o *ImportOptions) ImportProjectIntoJenkins(jc gojenkins.JenkinsClient, gitURL, dir, jenkinsfile, branchPattern, credentials string, failIfExists bool, gitProvider gits.GitProvider, authConfigSvc auth.ConfigService, isEnvironment, batchMode bool) error {
if gitURL == "" {
return fmt.Errorf("no Git repository URL provided")
}
gitInfo, err := gits.ParseGitURL(gitURL)
if err != nil {
return errors.Wrapf(err, "parsing git URL %q", gitURL)
}
if branchPattern == "" {
branchPattern, err = o.getBranchPattern(gitProvider, dir)
if err != nil {
return errors.Wrapf(err, "getting branch pattern in dir %q", dir)
}
}
// TODO - not sure what the credentials ID should be here...
credentials, err = o.updateJenkinsCredentials(credentials, jc, gitProvider)
if err != nil {
return errors.Wrapf(err, "updating credentials %q in Jenkins", credentials)
}
if err := o.createJenkinsJob(jc, gitInfo.Organisation); err != nil {
return errors.Wrap(err, "creating Jenkins job")
}
opts := &JenkinsProjectOptions{
GitInfo: gitInfo,
GitProvider: gitProvider,
Credentials: credentials,
BranchPattern: branchPattern,
Jenkinsfile: jenkinsfile,
}
if err := o.createJenkinsProject(opts, jc, gitInfo.Organisation, failIfExists, isEnvironment); err != nil {
return errors.Wrap(err, "creating Jenkins project")
}
if err := o.registerJenkinsWebhook(jc, gitProvider, gitInfo, gitURL); err != nil {
return errors.Wrap(err, "registering Jenkins webhook")
}
return nil
}
func (o *ImportOptions) JenkinsClient() (gojenkins.JenkinsClient, error) {
cf, err := factory.NewClientFactory()
if err != nil {
return nil, err
}
handles := o.CommonOptions.GetIOFileHandles()
jo := jenkinsutil.JenkinsOptions{
ClientFactory: cf,
BatchMode: o.CommonOptions.BatchMode,
IOFileHandles: &handles,
}
_, jsvc, err := jo.PickCustomJenkinsName(&o.Destination.Jenkins.JenkinsSelectorOptions, true)
if err != nil {
return nil, errors.Wrap(err, "failed to find a Jenkins server")
}
return jsvc.CreateClient()
}
func (o *ImportOptions) createJenkinsJob(jc gojenkins.JenkinsClient, organisation string) error {
return o.Retry(10, time.Second*10, func() error {
folder, err := jc.GetJob(organisation)
if err != nil {
// could not find folder so lets try create it
jobUrl := util.UrlJoin(jc.BaseURL(), jc.GetJobURLPath(organisation))
folderXML := jenkins.CreateFolderXML(jobUrl, organisation)
err = jc.CreateJobWithXML(folderXML, organisation)
if err != nil {
return errors.Wrapf(err, "creting the %q folder in Jenkins", organisation)
}
} else {
c := folder.Class
if c != "com.cloudbees.hudson.plugins.folder.Folder" {
log.Logger().Warnf("The folder %s is of class %s", organisation, c)
}
}
return nil
})
}
// JenkinsProjectOptions store the required options to create a Jenkins project
type JenkinsProjectOptions struct {
GitInfo *gits.GitRepository
GitProvider gits.GitProvider
Credentials string
BranchPattern string
Jenkinsfile string
}
func (o *ImportOptions) createJenkinsProject(opts *JenkinsProjectOptions, jc gojenkins.JenkinsClient,
organisation string, failIfExists bool, isEnvironment bool) error {
return o.Retry(10, time.Second*10, func() error {
projectXml := jenkins.CreateMultiBranchProjectXml(
opts.GitInfo,
opts.GitProvider,
opts.Credentials,
opts.BranchPattern,
opts.Jenkinsfile)
jobName := opts.GitInfo.Name
job, err := jc.GetJobByPath(organisation, jobName)
if err != nil {
err = jc.CreateFolderJobWithXML(projectXml, organisation, jobName)
if err != nil {
return errors.Wrapf(err, "creating MultiBranchProject job %q in folder %q",
jobName, organisation)
}
job, err = jc.GetJobByPath(organisation, jobName)
if err != nil {
return errors.Wrapf(err, "checking the MultiBranchProject job %q in folder %q exists",
jobName, organisation)
}
log.Logger().Infof("Created Jenkins Project: %s", util.ColorInfo(job.Url))
o.LogImportedProject(isEnvironment, opts.GitInfo)
params := url.Values{}
err = jc.Build(job, params)
if err != nil {
return errors.Wrapf(err, "triggering the job %q", job.Url)
}
log.Logger().Infof("Triggered Jenkins job: %s", job.Url)
return nil
}
if failIfExists {
return fmt.Errorf("job already exists in Jenkins at %s", job.Url)
} else {
log.Logger().Infof("Job already exists in Jenkins at %s", job.Url)
}
return nil
})
}
func (o *ImportOptions) registerJenkinsWebhook(jc gojenkins.JenkinsClient, gitProvider gits.GitProvider,
gitInfo *gits.GitRepository, gitURL string) error {
suffix := gitProvider.JenkinsWebHookPath(gitURL, "")
jenkBaseURL := o.ExternalJenkinsBaseURL
if jenkBaseURL == "" {
jenkBaseURL = jc.BaseURL()
}
isInsecureSSL, err := o.IsInsecureSSLWebhooks()
if err != nil {
return errors.Wrapf(err, "checking if we need to setup insecure SSL webhook")
}
webhookUrl := util.UrlJoin(jenkBaseURL, suffix)
webhook := &gits.GitWebHookArguments{
Owner: gitInfo.Organisation,
Repo: gitInfo,
URL: webhookUrl,
InsecureSSL: isInsecureSSL,
}
return gitProvider.CreateWebHook(webhook)
}
func (o *ImportOptions) getBranchPattern(gitProvider gits.GitProvider, dir string) (string, error) {
var branchPattern string
patterns, err := o.TeamBranchPatterns()
if err != nil {
return branchPattern, errors.Wrap(err, "getting branch pattern from team settings")
}
branchPattern = patterns.DefaultBranchPattern
if branchPattern != "" {
return branchPattern, nil
}
log.Logger().Debugf("Checking if the repository is a fork at %s with kind %s", gitProvider.ServerURL(), gitProvider.Kind())
fork, err := o.Git().IsFork(dir)
if err != nil {
return branchPattern, errors.Wrap(err, "checking git repository is a fork")
}
if fork {
branch, err := o.Git().Branch(dir)
if err != nil {
return branchPattern, errors.Wrapf(err, "getting the current branch in dir %q", dir)
}
if branch == "" {
return branchPattern, fmt.Errorf("no branch found in dir %q", dir)
}
branchPattern = branch
} else {
branchPattern = jenkins.BranchPattern(gitProvider.Kind())
}
log.Logger().Debugf("Using branch pattern: %s", branchPattern)
return branchPattern, nil
}
func (o *ImportOptions) updateJenkinsCredentials(credentials string, jc gojenkins.JenkinsClient, gitProvider gits.GitProvider) (string, error) {
if credentials == "" {
credentials = gitProvider.ServerURL()
}
if credentials == "" {
return credentials, fmt.Errorf("no server configured in the git provider")
}
_, err := jc.GetCredential(credentials)
if err == nil {
return credentials, nil
}
auth := gitProvider.UserAuth()
if err := jc.CreateCredential(credentials, auth.Username, auth.ApiToken); err != nil {
return credentials, errors.Wrapf(err, "creating credentials %q in Jenkins", credentials)
}
return credentials, nil
}
// GenerateProwConfig regenerates the Prow configurations after we have created a SourceRepository
func (o *ImportOptions) GenerateProwConfig(currentNamespace string, devEnv *v1.Environment) error {
kubeClient, err := o.KubeClient()
if err != nil {
return err
}
jxClient, _, err := o.JXClient()
if err != nil {
return err
}
defaultSchedulerName := devEnv.Spec.TeamSettings.DefaultScheduler.Name
config, plugins, err := pipelinescheduler.GenerateProw(false, true, jxClient, currentNamespace, defaultSchedulerName, devEnv, nil)
if err != nil {
return errors.Wrapf(err, "failed to update the Prow 'config' and 'plugins' ConfigMaps when regenerating prow config from source repositories")
}
err = pipelinescheduler.ApplyDirectly(kubeClient, currentNamespace, config, plugins)
if err != nil {
return errors.Wrapf(err, "applying Prow config in namespace %s", currentNamespace)
}
log.Logger().Infof("regenerated Prow configuration")
return nil
}
// LogImportedProject logs more details for an imported project
func (o *ImportOptions) LogImportedProject(isEnvironment bool, gitInfo *gits.GitRepository) {
log.Blank()
if !isEnvironment {
if !o.Destination.Jenkins.Enabled {
log.Logger().Infof("Watch pipeline activity via: %s", util.ColorInfo(fmt.Sprintf("jx get activity -f %s -w", gitInfo.Name)))
log.Logger().Infof("Browse the pipeline log via: %s", util.ColorInfo(fmt.Sprintf("jx get build logs %s", gitInfo.PipelinePath())))
log.Logger().Infof("You can list the pipelines via: %s", util.ColorInfo("jx get pipelines"))
log.Logger().Infof("When the pipeline is complete: %s", util.ColorInfo("jx get applications"))
log.Blank()
}
log.Logger().Infof("For more help on available commands see: %s", util.ColorInfo("https://jenkins-x.io/developing/browsing/"))
log.Blank()
}
log.Logger().Info(util.ColorStatus("Note that your first pipeline may take a few minutes to start while the necessary images get downloaded!\n"))
}