-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
206 lines (173 loc) · 6.14 KB
/
main.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
package main
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/avast/retry-go"
"github.com/bndr/gojenkins"
"github.com/spf13/cobra"
"net/http"
"os"
"strings"
"time"
)
const (
defaultJenkinsUrl = "http://127.0.0.1:8080"
defaultWait = false
defaultWaitPollSecond = 10
defaultWaitMaxAttempts = 60
desc = `This command triggers Jenkins job.
You can specify the '--job'/'-j' flag to determine the name of the Jenkins job to run.
To passing job parameters, use either the '--params'/'-p' flag in key=value format,
can specify multiple or separate parameters with commas: foo=bar,baz=qux.
You can also use the '--params-json'/'-P' passing JSON format parameters from the command line.
$ jenkins-trigger -j myjob
$ jenkins-trigger -j myjob -p foo=bar -p baz=qux
$ jenkins-trigger -j myjob -p foo=bar,baz=qux
$ jenkins-trigger -j myjob -P '{"foo":"bar","baz":"qux"}'
You can specify the '--jenkins-url' flag to set the url of the Jenkins server,
and '--jenkins-user'/'--jenkins-pat' flag to set the user and personal access token (PAT)
if the Jenkins server requires auth to access.
$ jenkins-trigger -j myjob --jenkins-url http://myjenkins.com:8080 --jenkins-user me --jenkins-pat mytoken
You can specify the '--wait' flag to waiting for the job complete, and return the results.
Use '--poll-time' flag (in duration format) to set how often to poll the jenkins server for results.
Use '--max-attempts' flag to set the max count of polling for results.
$ jenkins-trigger -j myjob --wait
$ jenkins-trigger -j myjob --wait --poll-time 10s --max-attempts 60
`
)
func main() {
c := config{
Jenkins: jenkins{
Url: defaultJenkinsUrl,
},
Job: job{},
Wait: wait{
Enabled: defaultWait,
PollTime: defaultWaitPollSecond * time.Second,
MaxAttempts: defaultWaitMaxAttempts,
},
}
params := params{}
cmd := &cobra.Command{
Use: "jenkins-trigger",
Short: "Trigger Jenkins job in Go",
Long: desc,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
c.Job.Params, err = params.init()
if err != nil {
return
}
return triggerBuild(c)
},
}
flags := cmd.Flags()
flags.StringVar(&c.Jenkins.Url, "jenkins-url", c.Jenkins.Url, "URL of the Jenkins server")
flags.StringVar(&c.Jenkins.User, "jenkins-user", c.Jenkins.User, "User for accessing Jenkins")
flags.StringVar(&c.Jenkins.Pat, "jenkins-pat", c.Jenkins.Pat, "Personal access token (PAT) for accessing Jenkins")
flags.BoolVarP(&c.Jenkins.Insecure, "insecure", "k", c.Jenkins.Insecure, "Allow insecure Jenkins server connections when using SSL")
flags.StringVarP(&c.Job.Name, "job", "j", c.Job.Name, "The name of the Jenkins job to run")
flags.StringSliceVarP(¶ms.slice, "params", "p", params.slice, "The parameters of the job in key=value format, can specify multiple or separate parameters with commas, e.g., foo=bar,baz=qux")
flags.StringVarP(¶ms.json, "params-json", "P", params.json, "The parameters of the job in JSON format, e.g., {\"foo\":\"bar\",\"baz\":\"qux\"}")
flags.BoolVar(&c.Wait.Enabled, "wait", c.Wait.Enabled, "Wait for the job to complete, and return the results")
flags.DurationVar(&c.Wait.PollTime, "poll-time", c.Wait.PollTime, "How often (duration) to poll the Jenkins server for results")
flags.UintVar(&c.Wait.MaxAttempts, "max-attempts", c.Wait.MaxAttempts, "Max count of polling for results")
cmd.MarkFlagRequired("job")
if err := cmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func triggerBuild(c config) error {
fmt.Printf("Triggering Jenkins build for job: %+v, wait: %+v\n", c.Job, c.Wait)
jenkins, err := c.Jenkins.createClient()
if err != nil {
return err
}
queueId, err := jenkins.BuildJob(context.Background(), c.Job.Name, c.Job.Params)
if err != nil {
return err
}
fmt.Printf("Job %s triggered successfully\n", c.Job.Name)
if !c.Wait.Enabled {
return nil
}
return retry.Do(
pollBuildResult(c, jenkins, queueId),
retry.DelayType(retry.FixedDelay),
retry.Delay(c.Wait.PollTime),
retry.Attempts(c.Wait.MaxAttempts),
)
}
func pollBuildResult(c config, jenkins *gojenkins.Jenkins, queueId int64) func() error {
return func() error {
fmt.Printf("Polling build result for job %s\n", c.Job.Name)
build, err := jenkins.GetBuildFromQueueID(context.Background(), queueId)
if err != nil {
return err
}
if build.IsGood(context.Background()) {
fmt.Printf("Job %s, build number %d successfully\n", c.Job.Name, build.GetBuildNumber())
return nil
}
if build.IsRunning(context.Background()) {
fmt.Printf("Job %s, build number %d is still running, retry after %s\n", c.Job.Name, build.GetBuildNumber(), c.Wait.PollTime)
return &IsStillRunning{time.Now(), c.Job.Name, build.GetBuildNumber()}
}
return retry.Unrecoverable(fmt.Errorf("Job %s Build number %d did not complete successfully\n", c.Job.Name, build.GetBuildNumber()))
}
}
// IsStillRunning indicate a Jenkins job is not done yet
type IsStillRunning struct {
time time.Time
jobName string
buildNumber int64
}
func (r *IsStillRunning) Error() string {
return fmt.Sprintf("job %s, build number %d is still running. (%s)\n", r.jobName, r.buildNumber, r.time.Format(time.Stamp))
}
type config struct {
Jenkins jenkins
Job job
Wait wait
}
type wait struct {
Enabled bool
PollTime time.Duration
MaxAttempts uint
}
type jenkins struct {
Url string
User string
Pat string
Insecure bool
}
func (j *jenkins) createClient() (*gojenkins.Jenkins, error) {
client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: j.Insecure},
}}
return gojenkins.CreateJenkins(client, j.Url, j.User, j.Pat).Init(context.Background())
}
type job struct {
Name string
Params map[string]string
}
type params struct {
slice []string
json string
}
func (p *params) init() (map[string]string, error) {
params := make(map[string]string)
if p.json != "" {
if err := json.Unmarshal([]byte(p.json), ¶ms); err != nil {
return nil, err
}
}
for _, v := range p.slice {
split := strings.Split(v, "=")
params[split[0]] = strings.Join(split[1:], "=")
}
return params, nil
}