-
Notifications
You must be signed in to change notification settings - Fork 785
/
build_number.go
106 lines (96 loc) · 2.77 KB
/
build_number.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
97
98
99
100
101
102
103
104
105
106
package builds
import (
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"io/ioutil"
"os"
"regexp"
"strings"
)
var (
numericStringRegex = regexp.MustCompile("[0-9]+")
)
// GetBuildNumber returns the build number using environment variables and/or pod Downward API files
func GetBuildNumber() string {
buildNumber := os.Getenv("JX_BUILD_NUMBER")
if buildNumber != "" {
return buildNumber
}
buildNumber = os.Getenv("BUILD_NUMBER")
if buildNumber != "" {
return buildNumber
}
buildID := os.Getenv("BUILD_ID")
if buildID != "" {
return buildID
}
// if we are in a knative build pod we can discover it via the dowmward API if the `/etc/podinfo/labels` file exists
const podInfoLabelsFile = "/etc/podinfo/labels"
exists, err := util.FileExists(podInfoLabelsFile)
if err != nil {
log.Warnf("failed to detect if the file %s exists: %s\n", podInfoLabelsFile, err)
} else if exists {
data, err := ioutil.ReadFile(podInfoLabelsFile)
if err != nil {
log.Warnf("failed to load downward API pod labels from %s due to: %s\n", podInfoLabelsFile, err)
} else {
text := strings.TrimSpace(string(data))
if text != "" {
return GetBuildNumberFromLabelsFileData(text)
}
}
}
return ""
}
// GetBuildNumberFromLabelsFileData parses the /etc/podinfo/labels style downward API file for a pods labels
// and returns the build number if it can be discovered
func GetBuildNumberFromLabelsFileData(text string) string {
m := LoadDownwardAPILabels(text)
return GetBuildNumberFromLabels(m)
}
// GetBuildNumberFromLabels returns the build number from the given Pod labels
func GetBuildNumberFromLabels(m map[string]string) string {
if m == nil {
return ""
}
answer := ""
for _, key := range []string{LabelBuildName, "build-number", LabelOldBuildName, LabelPipelineRunName} {
answer = m[key]
if answer != "" {
break
}
}
if answer != "" {
return lastNumberFrom(answer)
}
return ""
}
// lastNumberFrom splits a string such as "jstrachan-mynodething-master-1-build" via "-" and returns the last
// numeric string
func lastNumberFrom(text string) string {
// lets remove any whilespace or double quotes
text = strings.TrimSpace(text)
text = strings.TrimPrefix(text, "\"")
text = strings.TrimSuffix(text, "\"")
paths := strings.Split(text, "-")
for i := len(paths) - 1; i >= 0; i-- {
path := paths[i]
if numericStringRegex.MatchString(path) {
return path
}
}
return ""
}
// LoadDownwardAPILabels parses the /etc/podinfo/labels text into a map of label values
func LoadDownwardAPILabels(text string) map[string]string {
m := map[string]string{}
lines := strings.Split(text, "\n")
for _, line := range lines {
l := strings.TrimSpace(line)
paths := strings.SplitN(l, "=", 2)
if len(paths) == 2 {
m[paths[0]] = paths[1]
}
}
return m
}