-
Notifications
You must be signed in to change notification settings - Fork 66
/
continous-integration-multibranch-pipeline.jenkins
341 lines (284 loc) · 12.2 KB
/
continous-integration-multibranch-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
// SPDX-License-Identifier: MIT
// Continous integration build pipeline script for jenkins
// -------------------------------------------------------
// Please define a new "multibranch pipeline" job and use this jenkinsfile there
// Master branch is never build by this script - this branch is build by "release-pipeline.jenkins"
// But all other branches use this as CI build pipeline
// For details please refer /sechub-doc/src/docs/asciidoc/documents/buildserver/jenkins-pipeline.adoc
def sechubGitBranch ="unknown"
pipeline {
agent any
parameters {
booleanParam(name: 'DEBUG_OUTPUT_ENABLED', defaultValue: false, description: 'When enabled more informations will be printed.')
}
environment {
SECHUB_TRUSTALL= "true" // necessary for integration tests
SECHUB_SCAN_ENABLED = getStringCredentialsOrUndefined('sechub-scan-enabled')
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 = "${env.GIT_BRANCH}"
echo "Branch is $sechubGitBranch"
if( sechubGitBranch == "master" ) {
echo "Cancel build, because master is built by release-pipeline.jenkins!"
currentBuild.result = 'SUCCESS'
return
}
}
}
}
stage("Builds") {
parallel {
stage('Build Server & API Java') {
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-cli:build -Psechub.test.wiremock.https_port=${env.SECHUB_TEST_WIREMOCK_HTTPS_PORT} -Psechub.test.wiremock.http_port=${env.SECHUB_TEST_WIREMOCK_HTTP_PORT} --console=plain")
callGradleWrapper(":sechub-api-java:build :sechub-systemtest:build :sechub-pds-tools:buildPDSToolsCLI -Dsechub.build.stage=api-necessary --console=plain")
}
}
}
stage('Build Client') {
steps {
script{
callGradleWrapper(":sechub-cli:buildGo :sechub-cli:testGo --console=plain")
}
}
}
}
}
stage("test & security") {
parallel {
stage('Integration tests') {
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
}
steps {
script{
// start integration tests and also create combined test report after all
callGradleWrapper(":sechub-integrationtest:startIntegrationTestInstances :sechub-systemtest:integrationtest :sechub-integrationtest:integrationtest :sechub-integrationtest:stopIntegrationTestInstances -Dsechub.build.stage=all -Psechub.integrationtest.serverport=${env.SERVER_PORT} -Psechub.integrationtest.pdsport=${env.PDS_PORT} --console=plain")
callGradleWrapper("createCombinedTestReport -Dsechub.build.stage=all --console=plain")
}
}
}
stage('Security scan') {
when{
not {
environment name: 'SECHUB_SCAN_ENABLED', value: 'undefined'
}
}
environment {
SECHUB_PROJECT = 'sechub'
SECHUB_USERID = credentials('sechub-userid')
SECHUB_APITOKEN = credentials('sechub-api-token')
SECHUB_SERVER= credentials('sechub-server')
}
steps {
script{
executeSecHubScan()
}
}
}
}
}
stage('Build Documentation') {
steps {
script{
callGradleWrapper("documentation -Dsechub.build.stage=all --console=plain")
}
}
}
}
post {
failure {
emailext (
subject:"[ BUILD FAILED ] Branch: $sechubGitBranch, build #${env.BUILD_NUMBER}",
body:"CI build for branch '$sechubGitBranch' failed.\n"+
"${env.BUILD_URL}/console",
to: '$DEFAULT_RECIPIENTS'
)
}
fixed {
emailext (
subject:"[ BUILD FIXED ] Branch: $sechubGitBranch, build #${env.BUILD_NUMBER}",
body:"CI build for branch '$sechubGitBranch' has been fixed.\n"+
"${env.BUILD_URL}/console",
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}"
}
}
void executeSecHubScan() {
if (isUnix()) {
sh "sechub scan"
} else {
bat "sechub.exe scan"
}
}
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{
boolean clientVersion
boolean serverVersion
boolean clientBuildDone
boolean serverBuildDone
String releaseInfoString
public SecHubBuildContext(){
}
public boolean isStartEmailNeeded(){
return isAnyRelease()
}
public boolean isFailEmailNeeded(){
return true;// always
}
public boolean isSuccesssEmailNeeded(){
return isAnyRelease()
}
public void setClientBuildDone(boolean done){
this.clientBuildDone=done
}
public void setServerBuildDone(boolean done){
this.serverBuildDone=done
}
public void setWebsiteBuildDone(boolean done){
this.websiteBuildDone=done
}
/**
* Returns true when client or sever deployment.
* Interesting for interdepent deployment tests.
* E.g.
* - a server deployment should have integration test + client build before.
* - a client deployment should have also integration test - means server build before
* to have a working local integration test server
*/
public boolean isClientOrServerRelease(){
return isClientRelease() || isServerRelease();
}
public boolean isAnyRelease(){
return isClientRelease() || isServerRelease();
}
public boolean isSuccessFul(){
if (! isAnyRelease()){
return false
}
if (isClientRelease()) {
if (! this.clientBuildDone){
return false
}
}
if (isServerRelease()) {
if (! this.serverBuildDone){
return false8443
}
}
return true
}
public boolean isClientRelease(){
return clientVersion
}
public boolean isServerRelease(){
return serverVersion
}
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") }
releaseInfoString = "Release(s):"
if (clientVersionCommitTag == null || clientVersionCommitTag.empty){
clientVersion=false;
}else{
clientVersion=true;
releaseInfoString=releaseInfoString+" " + clientVersionCommitTag
}
if (serverVersionCommitTag == null || serverVersionCommitTag.empty){
serverVersion=false;
}else{
serverVersion=true;
releaseInfoString=releaseInfoString+" " + serverVersionCommitTag
}
}
public String getReleaseInfo(){
return releaseInfoString
}
}