-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
96 lines (86 loc) · 2.4 KB
/
build.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"fmt"
"log"
"os/exec"
"strings"
)
func buildTags(p *Project) {
// exec pre commands
for _, c := range p.Build.PreCommands {
s := strings.Split(c.Command, " ")
cmd := exec.Command(s[0], s[1:]...)
cmd.Dir = p.Build.WorkingDir
output, err := cmd.CombinedOutput()
if err != nil {
log.Println("buildTags() PreCommands", string(output), err)
}
}
// clear any local changes
cmd := exec.Command("git", "reset", "--hard")
cmd.Dir = p.Build.WorkingDir
output, err := cmd.CombinedOutput()
if err != nil {
log.Println("buildTags() git reset", string(output), err)
}
// git fetch
cmd = exec.Command("git", "fetch")
cmd.Dir = p.Build.WorkingDir
output, err = cmd.CombinedOutput()
if err != nil {
log.Println("buildTags() git fetch", string(output), err)
}
// get current tag
cmd = exec.Command("git", "describe", "--tags")
cmd.Dir = p.Build.WorkingDir
currentTagB, err := cmd.CombinedOutput()
currentTag := string(currentTagB)
currentTag = strings.TrimSuffix(currentTag, "\n")
if err != nil {
log.Println("buildTags() currentTag", currentTag, err)
}
// get latest tag
cmd = exec.Command("bash", "-c", "git describe --tags `git rev-list --tags --max-count=1`")
cmd.Dir = p.Build.WorkingDir
latestTagB, err := cmd.CombinedOutput()
latestTag := string(latestTagB)
latestTag = strings.TrimSuffix(latestTag, "\n")
if err != nil {
log.Println("buildTags() latestTag", latestTag, err)
}
// checkout latest tag
cmd = exec.Command("git", "checkout", "tags/"+fmt.Sprintf("%s", latestTag))
cmd.Dir = p.Build.WorkingDir
checkoutTag, err := cmd.CombinedOutput()
if err != nil {
log.Println("buildTags() checkoutTag", string(checkoutTag), err)
}
// exec post commands
for _, c := range p.Build.PostCommands {
s := strings.Split(c.Command, " ")
cmd := exec.Command(s[0], s[1:]...)
cmd.Dir = p.Build.WorkingDir
output, err := cmd.CombinedOutput()
if err != nil {
log.Println("buildTags() PostCommands", string(output), err)
}
}
}
func buildCommands(p *Project) {
for _, c := range p.Build.Commands {
s := strings.Split(c.Command, " ")
cmd := exec.Command(s[0], s[1:]...)
cmd.Dir = p.Build.WorkingDir
output, err := cmd.CombinedOutput()
if err != nil {
log.Println("buildTags() checkoutTag", string(output), err)
}
}
}
func build(p *Project) {
if p.Build.Type == "tags" {
buildTags(p)
} else if p.Build.Type == "commands" {
buildCommands(p)
}
}