-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenkins.go
96 lines (89 loc) · 2.5 KB
/
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
package main
import (
"github.com/bndr/gojenkins"
"strings"
"fmt"
)
type CI struct {
jenkins *gojenkins.Jenkins
}
func (c *CI) connect(url string, username string, password string) {
c.jenkins = gojenkins.CreateJenkins(nil, url, username, password)
c.jenkins.Init()
}
func (c *CI) updateBuild(projectName string, branch string) {
projects := GlobalStorage.GetProjects()
jobs := GlobalStorage.GetAllGroupJobs(branch)
var err error
existingView, err := c.jenkins.GetView(branch)
if err != nil {
panic(err)
}
var view *gojenkins.View
if existingView.GetName() == "" {
view, err = c.jenkins.CreateView(branch, gojenkins.LIST_VIEW)
if err != nil {
panic(err)
}
} else {
fmt.Println("View " + existingView.GetName() + " already exists");
view, err = c.jenkins.GetView(branch)
if err != nil {
panic(err)
}
}
for _, project := range projects {
jobName := project.Name + "-" + branch
var environment string
if storedJob, ok := jobs[jobName]; ok {
fmt.Println(storedJob.Name)
environment = storedJob.Group
} else {
environment = branch
}
existingJob, err := c.jenkins.GetJob(jobName)
if (err != nil && existingJob == nil) {
if project.Name != projectName {
environment = project.DefaultBranch
} else if existingJob == nil {
environment = branch
} else if cfg,_ := existingJob.GetConfig(); strings.Contains(cfg, "<name>*/" + project.DefaultBranch + "</name>") {
environment = branch
}
projectJobConfig := strings.Replace(project.JobDefinition, "{{environment}}", environment, -1)
fmt.Println(projectJobConfig)
projectJob, err := c.jenkins.CreateJob(projectJobConfig, jobName)
if err != nil {
panic(err)
}
view.AddJob(projectJob.GetName())
} else {
fmt.Println("Job " + jobName + " already exists");
if cfg,_ := existingJob.GetConfig(); !strings.Contains(cfg, "<name>*/" + branch + "</name>") {
c.jenkins.DeleteJob(existingJob.GetName())
projectJobConfig := strings.Replace(project.JobDefinition, "{{environment}}", environment, -1)
fmt.Println(projectJobConfig)
projectJob, err := c.jenkins.CreateJob(projectJobConfig, jobName)
if err != nil {
panic(err)
}
view.AddJob(projectJob.GetName())
}
}
}
}
func (c *CI) removeBuild(projectName string, branch string) {
var err error
existingView, err := c.jenkins.GetView(branch)
if err != nil {
panic(err)
}
if existingView.GetName() == "" {
return
} else {
jobs := existingView.GetJobs()
for _, job := range jobs {
c.jenkins.DeleteJob(job.Name)
}
}
}