forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_import.go
236 lines (220 loc) · 7.71 KB
/
common_import.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
package cmd
import (
"fmt"
"net/url"
"time"
"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/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
corev1 "k8s.io/api/core/v1"
)
// ImportProject imports a MultiBranchProject into Jenkins for the given git URL
func (o *CommonOptions) ImportProject(gitURL string, dir string, jenkinsfile string, branchPattern, credentials string, failIfExists bool, gitProvider gits.GitProvider, authConfigSvc auth.AuthConfigService, isEnvironment bool, batchMode bool) error {
jenk, err := o.JenkinsClient()
if err != nil {
return err
}
secrets, err := o.LoadPipelineSecrets(kube.ValueKindGit, "")
if err != nil {
return err
}
if gitURL == "" {
return fmt.Errorf("No Git repository URL found!")
}
gitInfo, err := gits.ParseGitURL(gitURL)
if err != nil {
return fmt.Errorf("Failed to parse git URL %s due to: %s", gitURL, err)
}
if branchPattern == "" {
patterns, err := o.TeamBranchPatterns()
if err != nil {
return err
}
branchPattern = patterns.DefaultBranchPattern
}
if branchPattern == "" {
log.Infof("Querying if the repo is a fork at %s with kind %s\n", gitProvider.ServerURL(), gitProvider.Kind())
fork, err := o.Git().IsFork(gitProvider, gitInfo, dir)
if err != nil {
return fmt.Errorf("No branch pattern specified and could not determine if the git repository is a fork: %s", err)
}
if fork {
// lets figure out which branches to enable for a fork
branch, err := o.Git().Branch(dir)
if err != nil {
return fmt.Errorf("Failed to get current branch in dir %s: %s", dir, err)
}
if branch == "" {
return fmt.Errorf("Failed to get current branch in dir %s", dir)
}
// TODO do we need to scape any wacky characters to make it a valid branch pattern?
branchPattern = branch
log.Infof("No branch pattern specified and this repository appears to be a fork so defaulting the branch patterns to run CI/CD on to: %s\n", branchPattern)
} else {
branchPattern = jenkins.BranchPattern(gitProvider.Kind())
}
}
createCredential := true
if credentials == "" {
// lets try find the credentials from the secrets
credentials = findGitCredentials(gitProvider, secrets)
if credentials != "" {
createCredential = false
}
}
if credentials == "" {
// TODO lets prompt the user to add a new credential for the git provider...
config := authConfigSvc.Config()
u := gitInfo.HostURL()
server := config.GetOrCreateServer(u)
if len(server.Users) == 0 {
// lets check if the host was used in `~/.jx/gitAuth.yaml` instead of URL
s2 := config.GetOrCreateServer(gitInfo.Host)
if s2 != nil && len(s2.Users) > 0 {
server = s2
u = gitInfo.Host
}
}
user, err := config.PickServerUserAuth(server, "user name for the Jenkins Pipeline", batchMode, "", o.In, o.Out, o.Err)
if err != nil {
return err
}
if user.Username == "" {
return fmt.Errorf("Could find a username for git server %s", u)
}
credentials, err = o.updatePipelineGitCredentialsSecret(server, user)
if err != nil {
return err
}
if credentials == "" {
return fmt.Errorf("Failed to find the created pipeline secret for the server %s", server.URL)
} else {
createCredential = false
}
}
if createCredential {
_, err = jenk.GetCredential(credentials)
if err != nil {
config := authConfigSvc.Config()
u := gitInfo.HostURL()
server := config.GetOrCreateServer(u)
if len(server.Users) == 0 {
// lets check if the host was used in `~/.jx/gitAuth.yaml` instead of URL
s2 := config.GetOrCreateServer(gitInfo.Host)
if s2 != nil && len(s2.Users) > 0 {
server = s2
u = gitInfo.Host
}
}
user, err := config.PickServerUserAuth(server, "user name for the Jenkins Pipeline", batchMode, "", o.In, o.Out, o.Err)
if err != nil {
return err
}
if user.Username == "" {
return fmt.Errorf("Could find a username for git server %s", u)
}
err = jenk.CreateCredential(credentials, user.Username, user.ApiToken)
if err != nil {
return fmt.Errorf("error creating jenkins credential %s at %s %v", credentials, jenk.BaseURL(), err)
}
log.Infof("Created credential %s for host %s user %s\n", util.ColorInfo(credentials), util.ColorInfo(u), util.ColorInfo(user.Username))
}
}
org := gitInfo.Organisation
err = o.retry(10, time.Second*10, func() error {
folder, err := jenk.GetJob(org)
if err != nil {
// could not find folder so lets try create it
jobUrl := util.UrlJoin(jenk.BaseURL(), jenk.GetJobURLPath(org))
folderXml := jenkins.CreateFolderXml(jobUrl, org)
err = jenk.CreateJobWithXML(folderXml, org)
if err != nil {
return fmt.Errorf("Failed to create the %s folder in jenkins: %s", org, err)
}
} else {
c := folder.Class
if c != "com.cloudbees.hudson.plugins.folder.Folder" {
log.Warnf("Warning the folder %s is of class %s", org, c)
}
}
return nil
})
if err != nil {
return err
}
err = o.retry(10, time.Second*10, func() error {
projectXml := jenkins.CreateMultiBranchProjectXml(gitInfo, gitProvider, credentials, branchPattern, jenkinsfile)
jobName := gitInfo.Name
job, err := jenk.GetJobByPath(org, jobName)
if err == nil {
if failIfExists {
return fmt.Errorf("Job already exists in Jenkins at %s", job.Url)
} else {
log.Warnf("Job already exists in Jenkins at %s\n", job.Url)
}
} else {
err = jenk.CreateFolderJobWithXML(projectXml, org, jobName)
if err != nil {
return fmt.Errorf("Failed to create MultiBranchProject job %s in folder %s due to: %s", jobName, org, err)
}
job, err = jenk.GetJobByPath(org, jobName)
if err != nil {
return fmt.Errorf("Failed to find the MultiBranchProject job %s in folder %s due to: %s", jobName, org, err)
}
log.Infof("Created Jenkins Project: %s\n", util.ColorInfo(job.Url))
o.logImportedProject(isEnvironment, gitInfo)
params := url.Values{}
err = jenk.Build(job, params)
if err != nil {
return fmt.Errorf("Failed to trigger job %s due to %s", job.Url, err)
}
}
return nil
})
if err != nil {
return err
}
// register the webhook
suffix := gitProvider.JenkinsWebHookPath(gitURL, "")
webhookUrl := util.UrlJoin(jenk.BaseURL(), suffix)
webhook := &gits.GitWebHookArguments{
Owner: gitInfo.Organisation,
Repo: gitInfo,
URL: webhookUrl,
}
return gitProvider.CreateWebHook(webhook)
}
func (o *CommonOptions) logImportedProject(isEnvironment bool, gitInfo *gits.GitRepositoryInfo) {
log.Blank()
if !isEnvironment {
log.Infof("Watch pipeline activity via: %s\n", util.ColorInfo(fmt.Sprintf("jx get activity -f %s -w", gitInfo.Name)))
log.Infof("Browse the pipeline log via: %s\n", util.ColorInfo(fmt.Sprintf("jx get build logs %s", gitInfo.PipelinePath())))
log.Infof("Open the Jenkins console via %s\n", util.ColorInfo("jx console"))
log.Infof("You can list the pipelines via: %s\n", util.ColorInfo("jx get pipelines"))
log.Infof("When the pipeline is complete: %s\n", util.ColorInfo("jx get applications"))
log.Blank()
log.Infof("For more help on available commands see: %s\n", util.ColorInfo("https://jenkins-x.io/developing/browsing/"))
log.Blank()
}
log.Info(util.ColorStatus("Note that your first pipeline may take a few minutes to start while the necessary images get downloaded!\n\n"))
}
// findGitCredentials finds the credential name from the pipeline git Secrets
func findGitCredentials(gitProvider gits.GitProvider, secrets *corev1.SecretList) string {
if secrets == nil {
return ""
}
u := gitProvider.ServerURL()
for _, secret := range secrets.Items {
annotations := secret.Annotations
if annotations != nil {
gitUrl := annotations[kube.AnnotationURL]
if u == gitUrl {
return secret.Name
}
}
}
return ""
}