-
Notifications
You must be signed in to change notification settings - Fork 5
/
jenkins.go
52 lines (48 loc) · 1.28 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
package devOps
import (
"fmt"
"github.com/bndr/gojenkins"
"time"
)
type JenkinsConn struct {
jenkins *gojenkins.Jenkins
}
//连接jenkins
func CreateJenkinsConn(jenkinsUrl, jenkinsAccount, jenkinsPassword string) (jenkinsConn *JenkinsConn, err error) {
jenkinsConn = new(JenkinsConn)
jenkinsConn.jenkins = gojenkins.CreateJenkins(nil, jenkinsUrl, jenkinsAccount, jenkinsPassword)
//输出jobs信息
allJobs, err := jenkinsConn.jenkins.GetAllJobNames()
if err != nil {
errorLog("CreateJenkins Error!:", err.Error())
return jenkinsConn, err
}
fmt.Println(yellowBg, "jobs信息:", reset)
for jobNum, job := range allJobs {
infoLog(fmt.Sprintf("%d. %s", jobNum, job.Name))
}
return jenkinsConn, err
}
//build job
func (jenkinsConn *JenkinsConn) BuildJobWithGitParam(jobName, paramName, paramValue string) error {
jobId, err := jenkinsConn.jenkins.BuildJob(jobName, map[string]string{paramName: paramValue})
if err != nil {
errorLog("BuildJobWithGitParam Error!:", err.Error())
return err
}
for {
task, err := jenkinsConn.jenkins.GetQueueItem(jobId)
if err != nil {
errorLog("GetQueueItem Error!:", err.Error())
break
}
time.Sleep(500 * time.Millisecond)
if task.Raw.Why == "" {
info2Log("task over")
break
} else {
infoLog(task.Raw.Why)
}
}
return nil
}