/
main.go
59 lines (49 loc) · 1.13 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
package main
import (
"github.com/drone/drone-go/drone"
"log"
"os"
"strings"
)
type Params struct {
Repos string `json:"repository"`
Server string `json:"server"`
Token string `json:"token"`
}
func parseRepo(repo string) (string, string) {
var (
owner string
name string
)
parts := strings.Split(repo, "/")
if len(parts) == 2 {
owner = parts[0]
name = parts[1]
}
return owner, name
}
func main() {
token := os.Getenv("TOKEN")
host := os.Getenv("HOST")
repo := os.Getenv("REPO")
branch := os.Getenv("BRANCH")
client := drone.NewClientToken(host, token)
owner, name := parseRepo(repo)
log.Println(token)
log.Println(host)
log.Println(repo)
log.Println(branch)
// get the latest build for the specified repository
build, err := client.BuildLast(owner, name, branch)
if err != nil {
log.Printf("Error: unable to get latest build for %s.\n", repo)
os.Exit(1)
}
// start a new build
_, err = client.BuildFork(owner, name, build.Number)
if err != nil {
log.Printf("Error: unable to trigger a new build for %s.\n", repo)
log.Panic(err)
}
log.Printf("Starting new build %d for %s\n", build.Number, repo)
}