Skip to content

Commit

Permalink
Merge pull request #4 from bootjp/develop2
Browse files Browse the repository at this point in the history
add more option of slack and add all success massage
  • Loading branch information
bootjp authored Jan 7, 2021
2 parents e92449f + a29deb5 commit 2a180a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ JENKINS_TOKEN="secret_token" jenkins_consecutive_fail_detector -url http://examp
# or
JENKINS_USER="login_user" JENKINS_PASSWORD="login_password" jenkins_consecutive_fail_detector -url http://example.com:8080/jenkins
```

### Slack notification

Add environment value `SLACK_WEBHOOK` are enable slack webhook.
Add environment value `SLACK_USERNAME` modify notify slack username.
Add environment value `SLACK_CHANNNEL` modify notify slack channel.
24 changes: 17 additions & 7 deletions detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func main() {
jenkinsToken := os.Getenv("JENKINS_TOKEN")
jenkinsUser := os.Getenv("JENKINS_USER")
jenkinsPassword := os.Getenv("JENKINS_PASSWORD")
webhookURL := os.Getenv("WEBHOOK_URL")
slackWebhookURL := os.Getenv("SLACK_WEBHOOK_URL")
slackUsername := os.Getenv("SLACK_USERNAME")
slackChannel := os.Getenv("SLACK_CHANNNEL")

var jenkins *gojenkins.Jenkins
if jenkinsToken != "" {
Expand Down Expand Up @@ -88,23 +90,31 @@ func main() {

fmt.Println(errors)

if webhookURL != "" {
if slackWebhookURL != "" {
payload := slack.Payload{
Text: errors,
Username: "jenkins_consecutive_fail_detector",
IconEmoji: ":warning:",
}
err := slack.Send(webhookURL, "", payload)

if slackUsername != "" {
payload.Username = slackUsername
}
if slackChannel != "" {
payload.Channel = slackChannel
}

err := slack.Send(slackWebhookURL, "", payload)
if len(err) > 0 {
fmt.Printf("error: %s\n", err)
fmt.Printf("webhook error: %s\n", err)
}
}

exitCode := 0
if len(detectFailJobs) > 0 {
exitCode = 1
os.Exit(1)
}
os.Exit(exitCode)

fmt.Printf("%d jonbs checked. all status success or retring now\n", len(jobs))
}

func JenkinsInit(url string, auth ...string) *gojenkins.Jenkins {
Expand Down

0 comments on commit 2a180a7

Please sign in to comment.