This repository was archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (72 loc) · 2 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
package main
import (
"context"
"flag"
"log"
"os"
"time"
"github.com/codeship/codeship-go"
)
var (
timeout = flag.Int("source", 300, "Timeout in seconds.")
)
func main() {
flag.Parse()
organization := os.Getenv("CODESHIP_ORGANIZATION")
username := os.Getenv("CODESHIP_USERNAME")
password := os.Getenv("CODESHIP_PASSWORD")
projectID := os.Getenv("CI_PROJECT_ID")
buildID := os.Getenv("CI_BUILD_ID")
endTime := time.Now().Add(time.Duration(*timeout) * time.Second)
ctx := context.Background()
auth := codeship.NewBasicAuth(username, password)
client, err := codeship.New(auth)
if err != nil {
log.Fatalf("Codeship authentication, %v", err)
}
org, err := client.Organization(ctx, organization)
if err != nil {
log.Fatalf("Codeship can not find organization, %v", err)
}
for time.Now().Before(endTime) {
builds, _, err := org.ListBuilds(ctx, projectID, codeship.PerPage(50))
if err != nil {
log.Fatalf("Codeship cannot list builds, %v", err)
}
var ref string
var start time.Time
for _, build := range builds.Builds {
if build.UUID == buildID {
ref = build.Ref
start = build.QueuedAt
}
}
for _, build := range builds.Builds {
if build.Ref == ref && build.UUID != buildID && build.Status != "success" && build.Status != "skipped" && build.Status != "stopped" && build.Status != "error" {
if build.QueuedAt.After(start) {
log.Printf("The is a newer build: %s", build.Links.Steps)
_, _, err := org.StopBuild(ctx, projectID, buildID)
if err != nil {
log.Fatalf("Cannot stop build, %v", err)
}
os.Exit(0)
}
}
}
done := true
for _, build := range builds.Builds {
if build.Ref == ref && build.UUID != buildID && build.Status != "success" && build.Status != "skipped" && build.Status != "stopped" && build.Status != "error" {
if build.QueuedAt.Before(start) {
log.Printf("Waiting for older build")
done = false
break
}
}
}
if done {
os.Exit(0)
}
time.Sleep(1 * time.Second)
}
log.Fatalf("Timeout")
}