-
Notifications
You must be signed in to change notification settings - Fork 67
/
release-pipeline.jenkins
366 lines (314 loc) · 13.6 KB
/
release-pipeline.jenkins
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// SPDX-License-Identifier: MIT
// Release pipeline script for jenkins
// Only the master branch is built by this script - the other branches are built by "continous-integration-multibranch-pipeline.jenkins"
// For details please refer to /sechub-doc/src/docs/asciidoc/documents/buildserver/jenkins-pipeline.adoc
def context = new SecHubBuildContext();
def sechubGitBranch ="unknown"
pipeline {
agent any
environment {
SECHUB_TRUSTALL= "true" // necessary for integration tests
SECHUB_BUILD_USE_TMP_CACHE ="true" // we use temporary cache, so always clean and timestamps in tests are always correct
}
stages {
stage('Initialize') {
steps {
script{
sechubGitBranch = sh(returnStdout: true, script: "git branch").trim()
echo "Branch is $sechubGitBranch"
def sechubGitTagsArray = sh(returnStdout: true, script: "git tag -l --contains HEAD").split()
echo "HEAD has following tags: $sechubGitTagsArray"
echo "WORKSPACE is ${env.WORKSPACE}"
context.init(sechubGitTagsArray)
echo "RELEASE info: "+context.getReleaseInfo()
if (context.isAnyRelease()){
releaseInfo = context.getReleaseInfo()
emailext (
subject:"[ STARTED ]Release - Build:${env.BUILD_NUMBER}",
body:"Continous integration build for release started.\n$releaseInfo\n\n"+
"${env.BUILD_URL}",
to: '$DEFAULT_RECIPIENTS'
)
}
callGradleWrapper("clean")
}
}
}
stage("Builds") {
when {
expression { context.isAnyRelease() }
}
parallel {
stage('Build Server and PDS') {
environment {
/* against build race conditions - See TestPortProvider.java */
SECHUB_TEST_WIREMOCK_HTTPS_PORT = getJenkinsExecutorPort(8143)
SECHUB_TEST_WIREMOCK_HTTP_PORT = getJenkinsExecutorPort(8180)
SECHUB_TEST_S3MOCK_HTTP_PORT = getJenkinsExecutorPort(9090)
SECHUB_TEST_S3MOCK_HTTPS_PORT = getJenkinsExecutorPort(9190)
}
steps {
script{
echo "SECHUB_TEST_WIREMOCK_HTTPS_PORT=${env.SECHUB_TEST_WIREMOCK_HTTPS_PORT}"
echo "SECHUB_TEST_WIREMOCK_HTTP_PORT=${env.SECHUB_TEST_WIREMOCK_HTTP_PORT}"
/*
* We do NOT build sechub-integrationtest
* Reason: because we do NOT want to have the integration tests executed, otherwise gradle will not execute them
* on integration phase again (because nothing has changed, so gradle will cache the results which are ignored ...
*/
callGradleWrapper("ensureLocalhostCertificate build generateOpenapi buildDeveloperAdminUI -x :sechub-integrationtest:test -x :sechub-cli:build -Psechub.test.wiremock.https_port=${env.SECHUB_TEST_WIREMOCK_HTTPS_PORT} -Psechub.test.wiremock.http_port=${env.SECHUB_TEST_WIREMOCK_HTTP_PORT}")
callGradleWrapper(":sechub-api-java:build :sechub-systemtest:build :sechub-pds-tools:buildPDSToolsCLI -Dsechub.build.stage=api-necessary")
}
}
}
stage('Build Client') {
steps {
script{
callGradleWrapper(':sechub-cli:buildGo :sechub-cli:testGo')
}
}
}
}
}
stage('Integration tests') {
when {
expression { context.isAnyRelease() }
}
environment {
/* against build race conditions - See TestPortProvider.java */
SERVER_PORT= getJenkinsExecutorPort(8243)
PDS_PORT= getJenkinsExecutorPort(8543)
/* setup s3 storage when defined as secrets - otherwise sharedvolume will be used with temp */
SECHUB_STORAGE_S3_ACCESSKEY = getStringCredentialsOrUndefined('sechub-storage-s3-accesskey')
SECHUB_STORAGE_S3_SECRETKEY = getStringCredentialsOrUndefined('sechub-storage-s3-secretkey')
SECHUB_STORAGE_S3_BUCKETNAME = getStringCredentialsOrUndefined('sechub-storage-s3-bucketname')
SECHUB_STORAGE_S3_ENDPOINT = getStringCredentialsOrUndefined('sechub-storage-s3-endpoint')
// we setup PDS storage with same s3 bucket data
PDS_STORAGE_S3_ACCESSKEY = getStringCredentialsOrUndefined('sechub-storage-s3-accesskey')
PDS_STORAGE_S3_SECRETKEY = getStringCredentialsOrUndefined('sechub-storage-s3-secretkey')
PDS_STORAGE_S3_BUCKETNAME = getStringCredentialsOrUndefined('sechub-storage-s3-bucketname')
PDS_STORAGE_S3_ENDPOINT = getStringCredentialsOrUndefined('sechub-storage-s3-endpoint')
SECHUB_INTEGRATIONTEST_PREPARE_WAIT_MS=100
/* avoid race conditions with management port */
MANAGEMENT_SERVER_PORT= getJenkinsExecutorPort(10250)
}
steps {
script{
// start integration tests and also create combined test report after all
callGradleWrapper("integrationtest -Psechub.integrationtest.serverport=${env.SERVER_PORT} -Psechub.integrationtest.pdsport=${env.PDS_PORT}")
}
}
}
stage('Build Documentation') {
when {
expression { context.isAnyRelease() }
}
steps {
script{
callGradleWrapper('documentation -Dsechub.build.stage=all')
}
}
}
stage('Publish release artifacts') {
when {
expression { context.isAnyRelease() }
}
steps {
echo 'This step is now ignored - because already done by GitHub actions - see https://github.com/mercedes-benz/sechub/actions'
}
}
/**
* How to handle these triggers in your build ?
* See /sechub-doc/src/docs/asciidoc/documents/buildserver/jenkins-pipeline.adoc
*/
stage('Trigger server released') {
when {
expression { context.isServerRelease() }
}
steps {
// trigger other pipeline job :
build job: 'sechub-server-released',
propagate: true,
wait: false,
parameters: [string(name: 'SERVERVERSION', value:"${context.serverVersion}")]
}
}
stage('Trigger client released') {
when {
expression { context.isClientRelease() }
}
steps {
// trigger other pipeline job :
build job: 'sechub-client-released',
propagate: true,
wait: false,
parameters: [string(name: 'CLIENTVERSION', value:"${context.clientVersion}")]
}
}
stage('Trigger PDS released') {
when {
expression { context.isPdsRelease() }
}
steps {
// trigger other pipeline job :
build job: 'sechub-pds-released',
propagate: true,
wait: false,
parameters: [string(name: 'PDS_VERSION', value:"${context.pdsVersion}")]
}
}
}
post {
failure {
script {
if (context.isAnyRelease()){
releaseInfo = context.getReleaseInfo()
emailext (
subject:"[ FAILED ]Release - Build:${env.BUILD_NUMBER}",
body:"Continous integration build with release publish failed.\n$releaseInfo\n\n"+
"${env.BUILD_URL}",
to: '$DEFAULT_RECIPIENTS'
)
}else{
emailext (
subject:"[ FAILED ]CI - Build:${env.BUILD_NUMBER}",
body:"Continous integration build failed.\n$releaseInfo\n\n"+
"${env.BUILD_URL}",
to: '$DEFAULT_RECIPIENTS'
)
}
}
}
success {
script {
if (context.isAnyRelease()){
releaseInfo = context.getReleaseInfo()
emailext (
subject:"[ SUCCESS ]Release - Build:${env.BUILD_NUMBER}",
body:"Continous integration build with release publish sucessfully done. Triggered additional jobs.\n$releaseInfo\n\n"+
"${env.BUILD_URL}",
to: '$DEFAULT_RECIPIENTS'
)
}
}
}
always {
junit '**/build/test-results/*/TEST-*.xml'
script{
callGradleWrapper('createCombinedTestReportZipfile')
}
archive 'build/archive/combined-sechub-testreport.zip'
}
}
}
// --------------------------------------------------------------------------
// - Script helper methods
// --------------------------------------------------------------------------
/* Gives back port number increased by executor number, so different between
jenkins executors (at least on same node) */
int getJenkinsExecutorPort(int originPort){
int executorNr = "${env.EXECUTOR_NUMBER}"
return originPort+executorNr
}
void callGradleWrapper(String gradleCommand) {
jdk = tool name: 'JDK_17'
env.JAVA_HOME = "${jdk}"
if (isUnix()) {
sh "./gradlew ${gradleCommand}"
} else {
bat "gradlew.bat ${gradleCommand}"
}
}
Object getStringCredentialsOrUndefined(String id){
result = 'undefined'
if (stringCredentialsExist(id)){
try {
withCredentials([string(credentialsId: id, variable: 'text')]) {
result = ''+text
}
} catch (_) {
}
}
return result
}
boolean stringCredentialsExist(String id) {
try {
withCredentials([string(credentialsId: id, variable: 'irrelevant')]) {
true
}
} catch (_) {
false
}
}
/**
* This class is necessary because jenkins - at least in version 2.150.2 - does not correct handle
* GIT tags and branches. Multi pipeline builds do not even checkout the tags. Normal pipeline builds do checkout the tags,
* but "when {tag pattern:'myPattern', comparator:'REGEXP' } does not work at all - no comment...
*
* To get the possibility to use tags for versioning this helper class was created
* At the begining of the pipeline there is a global variable defined which is also available inside build stages
* So we do init with git tags on head as array and use getter methods (not groovy but normal java getter, otherwise sandbox problems OMG!)
*/
class SecHubBuildContext{
String clientVersion
String serverVersion
String pdsVersion
boolean clientRelease
boolean serverRelease
boolean pdsRelease
String releaseInfoString
public SecHubBuildContext(){
}
/*
* Simplifies given version string . e.g. v0.4.1-client will be reduced to 0.4.1
*/
public String getSimplifiedVersion(String fullVersion){
if (fullVersion==null){
return "0.0.0";
}
String result = fullVersion;
if (result.startsWith("v")) {
result=result.substring(1);
}
int index = result.indexOf('-');
if (index==-1){
return result;
}
return result.substring(0,index);
}
public boolean isAnyRelease(){
return isClientRelease() || isServerRelease() || isPdsRelease();
}
public void init(String[ ] commitTags){
def versionCommitTags = commitTags.findAll {it.startsWith("v")}
// we got vX.Y.Z-server and vX.Y.Z-client tags
def clientVersionCommitTag = versionCommitTags.find{ it.contains("-client") }
def serverVersionCommitTag = versionCommitTags.find{ it.contains("-server") }
def pdsVersionCommitTag = versionCommitTags.find{ it.contains("-pds") }
releaseInfoString = "Release(s):"
if (clientVersionCommitTag == null || clientVersionCommitTag.empty){
clientRelease=false;
}else{
clientRelease=true;
clientVersion=getSimplifiedVersion(clientVersionCommitTag)
releaseInfoString=releaseInfoString+" " + clientVersionCommitTag
}
if (serverVersionCommitTag == null || serverVersionCommitTag.empty){
serverRelease=false;
}else{
serverRelease=true;
serverVersion=getSimplifiedVersion(serverVersionCommitTag)
releaseInfoString=releaseInfoString+" " + serverVersionCommitTag
}
if (pdsVersionCommitTag == null || pdsVersionCommitTag.empty){
pdsRelease=false;
}else{
pdsRelease=true;
pdsVersion=getSimplifiedVersion(pdsVersionCommitTag)
releaseInfoString=releaseInfoString+" " + pdsVersionCommitTag
}
}
public String getReleaseInfo(){
return releaseInfoString
}
}