Skip to content

Commit

Permalink
add more log
Browse files Browse the repository at this point in the history
  • Loading branch information
bootjp committed Mar 4, 2021
1 parent 66782c9 commit 1f773cf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ The conditions for notification are

```bash
go get github.com/bootjp/jenkins_consecutive_fail_detector
JENKINS_TOKEN="secret_token" jenkins_consecutive_fail_detector -url http://example.com:8080/jenkins
# or
JENKINS_USER="login_user" JENKINS_PASSWORD="login_password" jenkins_consecutive_fail_detector -url http://example.com:8080/jenkins
```

### install release binary
### install release binary (x86)
```bash
curl -LO https://github.com/bootjp/jenkins_consecutive_fail_detector/releases/download/v0.0.0/jenkins_consecutive_fail_detector-linux-amd64
curl -LO https://github.com/bootjp/jenkins_consecutive_fail_detector/releases/latest/download/jenkins_consecutive_fail_detector-linux-amd64
chmod +x jenkins_consecutive_fail_detector-linux-amd64
JENKINS_TOKEN="secret_token" jenkins_consecutive_fail_detector -url http://example.com:8080/jenkins
# or
JENKINS_USER="login_user" JENKINS_PASSWORD="login_password" jenkins_consecutive_fail_detector -url http://example.com:8080/jenkins
```

### running check
```bash
JENKINS_USER="login_user" JENKINS_TOKEN="secret_token" jenkins_consecutive_fail_detector -url https://example.com:8080/jenkins
# or
JENKINS_USER="login_user" JENKINS_PASSWORD="login_password" jenkins_consecutive_fail_detector -url https://example.com:8080/jenkins
```

### Slack notification
Expand Down
33 changes: 24 additions & 9 deletions detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
url := flag.String("url", "", "jenkins server url")
flag.Parse()
if *url == "" {
log.Fatalln("jenkins url is required. -url http://example.com:8080/jenkins ")
log.Fatalln("jenkins url is required. -url https://example.com:8080/jenkins ")
}

fmt.Printf("jenkins url: %s\n", *url)
Expand All @@ -40,7 +40,11 @@ func main() {
jenkinsPassword := os.Getenv("JENKINS_PASSWORD")
slackWebhookURL := os.Getenv("SLACK_WEBHOOK_URL")
slackUsername := os.Getenv("SLACK_USERNAME")
// for lower v0.0.5, compatible typo config name.
slackChannel := os.Getenv("SLACK_CHANNNEL")
if slackChannel == "" {
slackChannel = os.Getenv("SLACK_CHANNEL")
}

var jenkins *gojenkins.Jenkins
if jenkinsToken != "" {
Expand Down Expand Up @@ -159,8 +163,12 @@ func DetectFailJobs(jobs []*gojenkins.Job) []*FailJob {

lastBuild, err := job.GetLastBuild()
if err != nil {
ids, _ := job.GetAllBuildIds()
fmt.Println(ids)

logger.Println("got err GetLastBuild")
logger.Println("got err GetLastBuild by " + job.GetName() + " call by DetectFailJobs")
fmt.Printf("%v", lastBuild)
logger.Println(err)
logger.Println(errors.WithStack(err))

ej := &FailJob{
Expand Down Expand Up @@ -228,35 +236,42 @@ func DetectFailJobs(jobs []*gojenkins.Job) []*FailJob {
}

func IsOverHoursFailedJob(job *gojenkins.Job) (bool, error) {
fmt.Println(job.GetAllBuildIds())
latestBuild, err := job.GetLastBuild()
if err != nil {
logger.Println("got err GetLastBuild")
logger.Println("got err GetLastBuild by " + job.GetName())
fmt.Printf("%v", latestBuild)
logger.Println(err)
logger.Println(errors.WithStack(err))

return false, err
}

if time.Since(latestBuild.GetTimestamp()) > 1*time.Hour {
return true, nil
}
return false, nil
return time.Since(latestBuild.GetTimestamp()) > 1*time.Hour, nil
}

func IsConsecutiveFailJob(job *gojenkins.Job) (bool, error) {
buildIds, err := job.GetAllBuildIds()

if err != nil {
logger.Println("got err GetAllBuildIds")
logger.Println("got err GetAllBuild by " + job.GetName() + "")
logger.Println(err)
logger.Println(errors.WithStack(err))

return false, err
}

if len(buildIds) > 0 {
return false, nil
}

fmt.Println(buildIds)
fmt.Println(job.GetName())

lastBuild, err := job.GetLastBuild()
if err != nil {
logger.Println("got err GetLastBuild")
logger.Println("got err GetLastBuild by " + job.GetName())
fmt.Printf("%v", lastBuild)
logger.Println(err)
logger.Println(errors.WithStack(err))

Expand Down

0 comments on commit 1f773cf

Please sign in to comment.