-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathversion.go
136 lines (117 loc) · 3.48 KB
/
version.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package common
import (
"fmt"
"runtime"
"runtime/debug"
"github.com/prometheus/client_golang/prometheus"
"github.com/urfave/cli"
)
var (
NAME = "gitlab-runner"
VERSION = ""
REVISION = ""
BRANCH = "HEAD"
BUILT = ""
)
var AppVersion = AppVersionInfo{
Name: NAME,
Version: VERSION,
Revision: REVISION,
Branch: BRANCH,
GOVersion: runtime.Version(),
BuiltAt: BUILT,
OS: runtime.GOOS,
Architecture: runtime.GOARCH,
}
type AppVersionInfo struct {
Name string `json:"name"`
Version string `json:"version"`
Revision string `json:"revision"`
Branch string `json:"branch"`
GOVersion string `json:"go_version"`
BuiltAt string `json:"built_at"`
OS string `json:"os"`
Architecture string `json:"architecture"`
}
func (v *AppVersionInfo) Printer(c *cli.Context) {
fmt.Print(v.Extended())
}
func (v *AppVersionInfo) Line() string {
return fmt.Sprintf("%s %s (%s)", v.Name, v.Version, v.Revision)
}
func (v *AppVersionInfo) ShortLine() string {
return fmt.Sprintf("%s (%s)", v.Version, v.Revision)
}
func (v *AppVersionInfo) UserAgent() string {
return fmt.Sprintf("%s %s (%s; %s; %s/%s)", v.Name, v.Version, v.Branch, v.GOVersion, v.OS, v.Architecture)
}
func (v *AppVersionInfo) Variables() JobVariables {
return JobVariables{
{Key: "CI_RUNNER_VERSION", Value: v.Version, Public: true, Internal: true, File: false},
{Key: "CI_RUNNER_REVISION", Value: v.Revision, Public: true, Internal: true, File: false},
{
Key: "CI_RUNNER_EXECUTABLE_ARCH",
Value: fmt.Sprintf("%s/%s", v.OS, v.Architecture),
Public: true,
Internal: true,
File: false,
},
}
}
func (v *AppVersionInfo) Extended() string {
version := fmt.Sprintf("Version: %s\n", v.Version)
version += fmt.Sprintf("Git revision: %s\n", v.Revision)
version += fmt.Sprintf("Git branch: %s\n", v.Branch)
version += fmt.Sprintf("GO version: %s\n", v.GOVersion)
version += fmt.Sprintf("Built: %s\n", v.BuiltAt)
version += fmt.Sprintf("OS/Arch: %s/%s\n", v.OS, v.Architecture)
return version
}
// NewMetricsCollector returns a prometheus.Collector which represents current build information.
func (v *AppVersionInfo) NewMetricsCollector() *prometheus.GaugeVec {
labels := map[string]string{
"name": v.Name,
"version": v.Version,
"revision": v.Revision,
"branch": v.Branch,
"go_version": v.GOVersion,
"built_at": v.BuiltAt,
"os": v.OS,
"architecture": v.Architecture,
}
labelNames := make([]string, 0, len(labels))
for n := range labels {
labelNames = append(labelNames, n)
}
buildInfo := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "gitlab_runner_version_info",
Help: "A metric with a constant '1' value labeled by different build stats fields.",
},
labelNames,
)
buildInfo.With(labels).Set(1)
return buildInfo
}
func init() {
info, ok := debug.ReadBuildInfo()
if ok {
if AppVersion.Version == "" {
AppVersion.Version = info.Main.Version
}
for _, setting := range info.Settings {
switch {
case setting.Key == "vcs.revision" && AppVersion.Revision == "" && len(setting.Value) >= 8:
AppVersion.Revision = setting.Value[:8]
case setting.Key == "vcs.time" && AppVersion.BuiltAt == "":
AppVersion.BuiltAt = setting.Value
}
}
}
if AppVersion.Version == "" || AppVersion.Version == "(devel)" {
AppVersion.Version = "development version"
}
if AppVersion.Revision == "" {
AppVersion.Revision = "HEAD"
}
}