/
BuildServer.fs
220 lines (193 loc) · 6.12 KB
/
BuildServer.fs
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
namespace Fake.Core
open System
/// The server type option.
type BuildServer =
| TeamFoundation
| TeamCity
| CCNet
| Jenkins
| Travis
| AppVeyor
| GitLabCI
| GitHubActions
| Bamboo
| BitbucketPipelines
| LocalBuild
/// The trace mode option.
type TraceMode =
| Console
| Xml
[<AbstractClass>]
type BuildServerInstaller() =
abstract member Install: unit -> unit
abstract member Detect: unit -> bool
/// <summary>
/// Contains functions which allow build scripts to interact with a build server.
/// </summary>
[<RequireQualifiedAccess>]
module BuildServer =
/// <summary>
/// A constant label for local builds
/// </summary>
/// [omit]
let localBuildLabel = "LocalBuild"
/// <summary>
/// Defines the XML output file - used for build servers like CruiseControl.NET.
/// This output file can be specified by using the *logfile* build parameter.
/// </summary>
let mutable xmlOutputFile =
Environment.environVarOrDefault "logfile" "./output/Results.xml"
/// <summary>
/// Build number retrieved from Bamboo
/// </summary>
/// [omit]
let bambooBuildNumber = Environment.environVar "bamboo_buildNumber"
/// <summary>
/// Checks if we are on Bamboo
/// </summary>
/// [omit]
let isBambooBuild = String.IsNullOrEmpty bambooBuildNumber |> not
/// <summary>
/// Checks if we are on Team Foundation
/// </summary>
/// [omit]
let isTFBuild =
let tfbuild = Environment.environVar "TF_BUILD"
not (isNull tfbuild) && tfbuild.ToLowerInvariant() = "true"
/// <summary>
/// Build number retrieved from Team Foundation
/// </summary>
/// [omit]
let tfBuildNumber = Environment.environVar "BUILD_BUILDNUMBER"
/// <summary>
/// Build number retrieved from TeamCity
/// </summary>
/// [omit]
let tcBuildNumber = Environment.environVar "BUILD_NUMBER"
/// <summary>
/// Build number retrieved from Travis
/// </summary>
/// [omit]
let travisBuildNumber = Environment.environVar "TRAVIS_BUILD_NUMBER"
/// <summary>
/// Checks if we are on GitLab CI
/// </summary>
/// [omit]
let isGitlabCI =
Environment.environVar "CI_SERVER_NAME" = "GitLab CI"
|| Environment.environVar "GITLAB_CI" = "true"
/// <summary>
/// Build number retrieved from GitLab CI
/// </summary>
/// [omit]
let gitlabCIBuildNumber =
if isGitlabCI then
// https://github.com/fsharp/FAKE/issues/2290
let s = Environment.environVar "CI_PIPELINE_ID"
if String.IsNullOrEmpty s then
let id = Environment.environVar "CI_BUILD_ID"
if isNull id then "" else id
else
s
else
""
// <summary>
/// Checks if we are on GitHub Actions
/// </summary>
/// [omit]
let isGitHubActionsBuild =
Environment.environVarAsBoolOrDefault "GITHUB_ACTIONS" false
/// <summary>
/// Build number retrieved from GitHub Actions
/// </summary>
/// [omit]
let gitHubActionsBuildNumber =
if isGitHubActionsBuild then
Environment.environVar "GITHUB_RUN_NUMBER"
else
""
/// <summary>
/// Build number retrieved from Jenkins
/// </summary>
/// [omit]
let jenkinsBuildNumber = tcBuildNumber
/// <summary>
/// CruiseControl.NET Build label
/// </summary>
/// [omit]
let ccBuildLabel = Environment.environVar "CCNETLABEL"
/// <summary>
/// AppVeyor build number
/// </summary>
/// [omit]
let appVeyorBuildVersion = Environment.environVar "APPVEYOR_BUILD_VERSION"
/// <summary>
/// The current build server
/// </summary>
let buildServer =
if Environment.hasEnvironVar "JENKINS_HOME" then
Jenkins
elif Environment.hasEnvironVar "TEAMCITY_VERSION" then
TeamCity
elif not (String.IsNullOrEmpty ccBuildLabel) then
CCNet
elif not (String.IsNullOrEmpty travisBuildNumber) then
Travis
elif not (String.IsNullOrEmpty appVeyorBuildVersion) then
AppVeyor
elif isGitlabCI then
GitLabCI
elif isGitHubActionsBuild then
GitHubActions
elif isTFBuild then
TeamFoundation
elif isBambooBuild then
Bamboo
elif Environment.hasEnvironVar "BITBUCKET_COMMIT" then
BitbucketPipelines
else
LocalBuild
/// <summary>
/// The current build version as detected from the current build server.
/// </summary>
let buildVersion =
let getVersion = Environment.environVarOrDefault "buildVersion"
match buildServer with
| Jenkins -> getVersion jenkinsBuildNumber
| TeamCity -> getVersion tcBuildNumber
| CCNet -> getVersion ccBuildLabel
| Travis -> getVersion travisBuildNumber
| AppVeyor -> getVersion appVeyorBuildVersion
| GitLabCI -> getVersion gitlabCIBuildNumber
| GitHubActions -> getVersion gitHubActionsBuildNumber
| TeamFoundation -> getVersion tfBuildNumber
| Bamboo -> getVersion bambooBuildNumber
| LocalBuild -> getVersion localBuildLabel
| BitbucketPipelines -> getVersion ""
/// <summary>
/// Is true when the current build is a local build.
/// </summary>
let isLocalBuild = LocalBuild = buildServer
let mutable ansiColorSupport =
match buildServer with
| Jenkins -> false
| TeamCity -> true
| CCNet -> false
| Travis -> true
| AppVeyor -> true
| GitLabCI -> true
| GitHubActions -> true
| TeamFoundation -> false
| Bamboo -> false
| BitbucketPipelines -> false
| LocalBuild -> false
/// <summary>
/// Install given list of build servers
/// </summary>
///
/// <param name="servers">The list of build servers to install</param>
let install (servers: BuildServerInstaller list) =
servers
|> List.iter (fun f ->
if f.Detect() then
f.Install())