-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrigger.go
More file actions
48 lines (37 loc) · 984 Bytes
/
trigger.go
File metadata and controls
48 lines (37 loc) · 984 Bytes
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
package main
import (
"crypto/tls"
"log"
"net/http"
"time"
)
func (s *server) triggerJob(job string) bool {
url := createJobURL(s.param.jenkins.URL, job)
if s.param.jenkins.User == "" {
url = string(url + "?token=" + s.param.jenkins.Token)
}
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return false
}
// if user and token is defined, use it for basic auth
if s.param.jenkins.User != "" {
req.SetBasicAuth(s.param.jenkins.User, s.param.jenkins.Token)
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
timeout := time.Duration(5 * time.Second)
client := &http.Client{Transport: tr, Timeout: timeout}
resp, err := client.Do(req)
if err != nil {
log.Print("Error:", err)
return false
}
if !(200 <= resp.StatusCode && resp.StatusCode <= 299) {
log.Printf("... %v trigger failed with status code %v\n", job, resp.StatusCode)
} else {
log.Printf("... %v triggered\n", job)
}
return true
}