forked from mercedes-benz/sechub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-versioning.gradle
186 lines (160 loc) · 7.41 KB
/
build-versioning.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
// SPDX-License-Identifier: MIT
import groovy.time.TimeCategory
import groovy.time.TimeDuration
/* ============================================================================
This file contains tasks doing versioining
Also 'buildVersionFiles' is executed for EVERY gradle call, which is
necessary to have allways exact same version in scope, even when calling
multiple times. As long as code does not change it will be same vesion...
============================================================================
Included from: "${rootProject.projectDir}/build.gradle"
============================================================================
*/
/*
* We do always build the files. See description in headline comment.
*/
buildVersionFiles()
allprojects{
// normal gradle build is for server so we use server image version
// go client uses generated version.go file for runtime info
project.version=VersionData.getServerVersion()
}
def buildVersionFiles(){
println("BUILD versioning")
def start = new Date()
// See more details about versioning definition in issue SECHUB-172
// This file contains some logic for calculation of the version number
def gitTags = git.tag.list()
def commitTags = gitTags.findAll { it.commit == git.head()}
def versionCommitTags = commitTags.findAll {it.name.startsWith("v")}
// we got vX.Y.Z-server and vX.Y.Z-client tags
def serverVersionCommitTag = versionCommitTags.find{ it.name.contains("-server") }
def clientVersionCommitTag = versionCommitTags.find{ it.name.contains("-client") }
def pdsVersionCommitTag = versionCommitTags.find{ it.name.contains("-pds") }
def unstagedChanges = git.status().unstaged
def stagedChanges = git.status().staged
def noUnstagedChanges = unstagedChanges.getAllChanges().isEmpty()
def noStagedChanges = stagedChanges.getAllChanges().isEmpty()
def hasChanged = !noUnstagedChanges || !noStagedChanges
def buildNumber= getBuildNr()
// ------------------------
// - Client
// ------------------------
// write version code for go client
String clientGoVersionTemplate = new File('./sechub-cli/src/daimler.com/sechub/cli/version.go.template').getText('UTF-8')
def clientVersion = buildVersionString(clientVersionCommitTag, hasChanged,buildNumber)
def reducedClientVersion = simplifiedVersion(clientVersion);
String clientGoVersionCode = clientGoVersionTemplate.replaceAll("__version__",clientVersion)
def clientVersionFile = new File('./sechub-cli/src/daimler.com/sechub/cli/version.go')
clientVersionFile.write(clientGoVersionCode)
/* write version info also as asciidoc file*/
def clientVersionAsciiDocFile = new File('./sechub-doc/src/docs/asciidoc/documents/gen/client-version.adoc')
clientVersionAsciiDocFile.write("// SPDX-License-Identifier: MIT\n:revnumber: Client "+reducedClientVersion+"\n:longrevnumber: Client "+clientVersion+"\n")
// ------------------------
// - Server
// ------------------------
def serverVersion = buildVersionString(serverVersionCommitTag, hasChanged,buildNumber)
def reducedServerVersion = simplifiedVersion(serverVersion);
/* write version info also as asciidoc file*/
def serverVersionAsciiDocFile = new File('./sechub-doc/src/docs/asciidoc/documents/gen/server-version.adoc')
serverVersionAsciiDocFile.write("// SPDX-License-Identifier: MIT\n:revnumber: Server "+reducedServerVersion+"\n:longrevnumber: Server "+serverVersion+"\n")
// ------------------------
// - PDS
// ------------------------
def pdsVersion = buildVersionString(pdsVersionCommitTag, hasChanged,buildNumber)
def reducedPDSVersion = simplifiedVersion(pdsVersion);
/* write version info also as asciidoc file*/
def pdsVersionAsciiDocFile = new File('./sechub-doc/src/docs/asciidoc/documents/gen/pds-version.adoc')
pdsVersionAsciiDocFile.write("// SPDX-License-Identifier: MIT\n:revnumber: PDS "+reducedPDSVersion+"\n:longrevnumber: PDS "+pdsVersion+"\n")
/* we use a simplified version for build artifacts-reason:
* there were multiple problems. E.g. a user downloading
* a client in version 1.0.0 does not want a folder called 1.0.0-b75..
*/
VersionData.setServerVersion(reducedServerVersion);
VersionData.setClientVersion(reducedClientVersion);
VersionData.setPdsVersion(reducedPDSVersion);
def stop = new Date()
TimeDuration td = TimeCategory.minus( stop, start )
println("- Server :"+VersionData.getServerVersion()+" ["+serverVersion+"]")
println("- Client :"+VersionData.getClientVersion()+" ["+clientVersion+"]")
println("- PDS :"+VersionData.getClientVersion()+" ["+pdsVersion+"]")
println("- Time elapsed for versioning:"+td)
}
/*
* Simplifies given version string . e.g. 0.4.1-b74 will be reduced to 0.4.1
*/
def simplifiedVersion(String fullVersion){
if (fullVersion==null){
return "0.0.0";
}
int index = fullVersion.indexOf('-');
if (index==-1){
return fullVersion;
}
return fullVersion.substring(0,index);
}
/**
* Builds version string. When commits are dirty they will be marked addtionally
* with "-dirty-$timestamp" so its clear there has been changes. commits having
* a dedicated version tag will lead to reduced version info, when no tag defined but
* only commit id version will be "0.0.0-$abreviatedCommitId"
* also build id is added. For local builds build number starts with l and continues
* with timestamp. Server builds will have a b and buildbumber
* Examples:
* <pre>
* All committed:
* local
* tag "v1.0.0-client" will be lead to "1.0.0-l20181108071705"
* tag "v1.1.0-server" will be lead to "1.1.0-l20181108071705"
*
* commit:"aebcd" will lead to "0.0.0-aebcd"
* build server (BUILD_NUMBER set)
* tag "v1.0.0-client" will be lead to "1.0.0-b123"
* tag "v1.1.0-server" will be lead to "1.1.0-b123"
*
* commit:"aebcd" will lead to "0.0.0-aebcd-b123"
*
*
* Additional change:
* local (no BUILD_NUMBER set)
* former tag "v1.0.0-client" will lead for example to "1.0.0-dirty-l1540999578066"
* former commit:"aebcd" will lead for example to "0.0.0-aebcd-dirty-l1540999578066"
* build server (BUILD_NUMBER set)
* former tag "v1.0.0-client" will lead for example to "1.0.0-dirty-l1540999578066"
* former commit:"aebcd" will lead for example to "0.0.0-aebcd-dirty-l1540999578066"
* </pre>
*/
def buildVersionString(commitTag, boolean hasChanged, buildNumber){
def calcversion = ""
if (commitTag == null) {
calcversion="0.0.0-${git.head().abbreviatedId}"
} else {
calcversion = commitTag.name - 'v'
// remove identifiers for server, client, ..
calcversion=calcversion-"-server"
calcversion=calcversion-"-client"
}
if (hasChanged){
calcversion="${calcversion}-dirty";
}
calcversion="${calcversion}-${buildNumber}";
return calcversion
}
def getBuildNr(){
if (getServerBuildNr()!=null){
return "b"+getServerBuildNr()
}else{
if (project.hasProperty('sechub.build.timestamp')){
if (project.getProperty('sechub.build.timestamp')=="false"){
return "latest"
}
}
return getLocalBuildNr()
}
}
def getServerBuildNr(){
return System.getenv('BUILD_NUMBER' )
}
def getLocalBuildNr() {
return new Date().format('yyyyMMddHHmmss')
}