-
Notifications
You must be signed in to change notification settings - Fork 2
/
caplin-version-2.3.1.gradle
233 lines (204 loc) · 6.89 KB
/
caplin-version-2.3.1.gradle
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
221
222
223
224
225
226
227
228
229
230
231
232
233
// VARIABLES
// HASH_LENGTH sets the number of characters in the hash on the end of the version number
Integer HASH_LENGTH = project.ext.properties.hashLength ?: 7
// NO_COMMIT_COUNT specifies whether to omit the commit count from the version number
Boolean NO_COMMIT_COUNT = project.ext.properties.noCommitCount ?: false
// DEFAULT_CHANGELIST sets the build number suffix for non-CI builds
String DEFAULT_CHANGELIST = "dev"
// BRANCH_METHOD defines the method for finding the git branch name
// Available options are 'env' and 'cmd'
String BRANCH_METHOD = "env"
// MAIN
// Please be careful editing anything underneath here as this could have serious consequences on ALL BUILDS
def is_jenkins_ci = (System.getenv('BUILD_NUMBER') != null)
def is_gitlab_ci = (System.getenv('CI_COMMIT_SHA') != null)
def perforce_changelist = System.getenv('P4_CHANGELIST')
// COMMITS_COUNT_OVERRIDE sets the commit count to the passed in value
Integer commitsNumberOverride = System.getenv('COMMITS_COUNT_OVERRIDE')?.isInteger() ? System.getenv('COMMITS_COUNT_OVERRIDE').toInteger() : null
/*
* Standarises the way we find the branch name
*/
String getBranchName(String branch_method, Boolean is_gitlab_ci) {
switch(branch_method) {
case 'cmd':
return getBranchNameByCmd()
break
default:
return getBranchNameByEnv(is_gitlab_ci)
break
}
}
/*
* Gets the hash name
*/
String getHash(Integer hash_length, Boolean is_gitlab_ci) {
if (is_gitlab_ci) {
return System.getenv('CI_COMMIT_SHA').substring(0, hash_length)
}
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', "--short=${hash_length}", 'HEAD'
standardOutput = stdout
}
return stdout.toString().trim()
}
/*
* Gets the total number of commits
*/
Integer getNumberOfCommits(commitsNumberOverride, Boolean is_gitlab_ci) {
if (commitsNumberOverride != null) {
return commitsNumberOverride
}
if (is_gitlab_ci){
return System.getenv('CI_PIPELINE_IID').toInteger()
}
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-list', 'HEAD'
standardOutput = stdout
}
int commitsNumber = 0
stdout.toString().eachLine { commitsNumber++ }
return commitsNumber
}
/*
* Gets branch name based on git branch
*/
String getBranchNameByCmd() {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'branch'
standardOutput = stdout
}
stdout.toString().eachLine {
if (it.startsWith("*")) {
return it.toLowerCase().replaceFirst("^\\* ", "")
}
}
}
/*
* Gets branch name based on GIT_BRANCH_ENV_NAME
*/
String getBranchNameByEnv(Boolean is_gitlab_ci) {
if (is_gitlab_ci) {
return System.getenv('CI_COMMIT_BRANCH')
}
return System.getenv('GIT_BRANCH').replaceFirst('origin/', '')
}
String getPrId() {
//return "-PR" + System.getenv('pullRequestId')
return "-PR" + System.getenv('CI_MERGE_REQUEST_IID')
}
/*
* Gets branch name based on GIT_BRANCH_ENV_NAME
*/
String getShortBranchName(branch_method, is_gitlab_ci) {
def gitBranchName = getBranchName(branch_method, is_gitlab_ci)
if (gitBranchName != null) {
gitBranchName = gitBranchName.replaceFirst("^\\* ", "")
def prefix = gitBranchName.replaceFirst(~/[^\/]+$/, '')
gitBranchName = gitBranchName.replaceFirst(prefix, '')
}
return gitBranchName
}
/*
* Override environment variable to prevent appending featue branch to the end
* of the version for non-standard release branches
*/
boolean checkPluginOverride() {
return (System.getenv('VERSION_PLUGIN_OVERRIDE').toString().equals("false"));
}
/*
* Works out if the current branch is the main branch
*/
boolean isMainBranch(branch_method, is_gitlab_ci) {
def gitBranchName = getBranchName(branch_method, is_gitlab_ci)
def branches = ["master"] // default main branch
if (project.hasProperty('mainBranches')) {
branches = ext.mainBranches
}
if (gitBranchName != null) {
return branches.any { gitBranchName.toLowerCase() == it }
}
return false
}
/*
* Works out if the branch is a Support branch
*/
boolean isSupport(branch_method, is_gitlab_ci) {
def isSupport = false
def gitBranchName = getBranchName(branch_method, is_gitlab_ci)
if (gitBranchName != null) {
if (gitBranchName.toLowerCase().startsWith('support/') || gitBranchName.toLowerCase().startsWith('support-')) {
isSupport = true
}
}
return isSupport
}
/*
* Works out if the branch is a Release branch
*/
boolean isRelease(branch_method, is_gitlab_ci) {
def isRelease = false
def gitBranchName = getBranchName(branch_method, is_gitlab_ci)
if (gitBranchName != null) {
if (gitBranchName.toLowerCase().startsWith('release/') || gitBranchName.toLowerCase().startsWith('release-')) {
isRelease = true
}
}
return isRelease
}
/*
* Works out if the branch is a Hotfix branch
*/
boolean isHotfix(branch_method, is_gitlab_ci) {
def isHotfix = false
def gitBranchName = getBranchName(branch_method, is_gitlab_ci)
if (gitBranchName != null) {
if (gitBranchName.toLowerCase().startsWith('hotfix/') || gitBranchName.toLowerCase().startsWith('hotfix-')) {
isHotfix = true
}
}
return isHotfix
}
def CHANGELIST = DEFAULT_CHANGELIST
if (is_jenkins_ci || is_gitlab_ci) {
if (perforce_changelist != null) {
CHANGELIST = perforce_changelist
} else {
def featureBranchName = !isMainBranch(BRANCH_METHOD, is_gitlab_ci) && !isRelease(BRANCH_METHOD, is_gitlab_ci) && !isSupport(BRANCH_METHOD, is_gitlab_ci) && !isHotfix(BRANCH_METHOD, is_gitlab_ci) && !checkPluginOverride() && getShortBranchName(BRANCH_METHOD, is_gitlab_ci) != null ? "-${getShortBranchName(BRANCH_METHOD, is_gitlab_ci)}" : ""
if (System.getenv('CI_MERGE_REQUEST_IID')) {
featureBranchName = getPrId()
}
if (NO_COMMIT_COUNT) {
CHANGELIST = "${getHash(HASH_LENGTH, is_gitlab_ci)}${featureBranchName}"
} else {
CHANGELIST = "${getNumberOfCommits(commitsNumberOverride, is_gitlab_ci)}-${getHash(HASH_LENGTH, is_gitlab_ci)}${featureBranchName}"
}
}
}
String VERSION = "${ext.major}.${ext.minor}.${ext.patch}"
String FULL_VERSION = "${VERSION}-${CHANGELIST}"
allprojects {
version = FULL_VERSION
}
// Task for printing out the version during builds
task printVersion {
doLast {
println "*************************************"
println "VERSION=${FULL_VERSION}"
println "*************************************"
}
}
// Task for Jenkins to find the version quickly
task printCIVersion {
doLast {
println "version=${FULL_VERSION}"
}
}
// Print only CI version with nothing extra
task printCIVersionOnly {
doLast {
println "${FULL_VERSION}"
}
}