-
Notifications
You must be signed in to change notification settings - Fork 4
/
Jenkinsfile.buildchain
315 lines (280 loc) · 12.3 KB
/
Jenkinsfile.buildchain
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
@Library('jenkins-pipeline-shared-libraries')_
import org.kie.jenkins.MavenCommand
import org.kie.jenkins.MavenSettingsUtils
agentLabel = "${env.ADDITIONAL_LABEL?.trim() ?: 'kie-rhel8 && docker && kie-mem16g'} && !built-in"
timeoutValue = env.ADDITIONAL_TIMEOUT?.trim() ?: '180'
jdkTool = env.BUILD_JDK_TOOL
sonarJdkTool = env.SONAR_JDK_TOOL
mavenTool = env.BUILD_MAVEN_TOOL
mavenDeployArtifacts = env.ENABLE_DEPLOY.toBoolean()
mavenDeployRepository = env.MAVEN_DEPLOY_REPOSITORY ?: ''
mavenDeployRepositoryCredsId = env.MAVEN_DEPLOY_REPOSITORY_CREDS_ID ?: ''
mavenDeployLocalDir = env.MAVEN_DEPLOY_LOCAL_DIR ?: ''
buildChainType = env.BUILDCHAIN_TYPE?.trim() ?: 'cross_pr'
buildChainProject = env.BUILDCHAIN_PROJECT?.trim()
defaultSettingsXmlId = isPRBuildChainType() || isFDBBuildChainType() ? 'kogito_pr_settings' : 'kogito_release_settings'
settingsXmlId = env.MAVEN_SETTINGS_FILE_ID ?: defaultSettingsXmlId
enableSonarCloudAnalysis = env.ENABLE_SONARCLOUD ? env.ENABLE_SONARCLOUD.toBoolean() : false
downstreamBuild = env.DOWNSTREAM_BUILD ? env.DOWNSTREAM_BUILD.toBoolean() : false
buildChainConfigRepo = env.BUILDCHAIN_CONFIG_REPO ?: 'kogito-pipelines'
buildChainConfigBranch = env.BUILDCHAIN_CONFIG_BRANCH ?: '\${BRANCH:main}'
buildChainConfigGitAuthor = env.BUILDCHAIN_CONFIG_AUTHOR ?: '\${AUTHOR:kiegroup}'
buildChainConfigDefinitionFilePath = env.BUILDCHAIN_CONFIG_FILE_PATH ?: '.ci/pull-request-config.yaml'
buildChainAction = isFDBBuildChainType() ? 'full_downstream' : buildChainType
buildChainAdditionalArguments = buildChainProject ? [ "-p ${buildChainProject}" ] : []
if (isBranchBuildChainType()) {
buildChainAdditionalArguments.add("-b ${env.GIT_BRANCH_NAME.trim()}")
buildChainAdditionalArguments.add("-g ${env.GIT_AUTHOR.trim()}")
if(env.BUILDCHAIN_FULL_BRANCH_DOWNSTREAM_BUILD) {
buildChainAdditionalArguments.add("--fullProjectDependencyTree")
}
} else {
buildChainAdditionalArguments.add("-u ${env.ghprbPullLink}")
}
skipTests = params.SKIP_TESTS ?: false
skipIntegrationTests = params.SKIP_INTEGRATION_TESTS ?: false
launchDownstreamJobs = env.LAUNCH_DOWNSTREAM_JOBS ? env.LAUNCH_DOWNSTREAM_JOBS.split(',') : []
notificationJobName = env.NOTIFICATION_JOB_NAME ?: 'PR'
pipeline {
agent {
label agentLabel
}
tools {
nodejs 'nodejs-16.2.0'
}
options {
timestamps()
timeout(time: timeoutValue, unit: 'MINUTES')
}
environment {
FIREFOX_FOLDER = '/opt/tools/firefox-60esr'
MAVEN_OPTS = '-Xms1024m -Xmx12g'
}
stages {
stage('Initialize') {
steps {
script {
sh 'printenv > env_props'
archiveArtifacts artifacts: 'env_props'
if (mavenDeployArtifacts) {
assert mavenDeployRepository : 'Please provide the maven deploy repository'
assert mavenDeployRepositoryCredsId : 'Please provide the maven deploy repository credentials'
}
currentBuild.displayName = params.DISPLAY_NAME ?: env.GIT_BRANCH_NAME ?: currentBuild.displayName
// Needed by Kogito Apps
setupCypressEnv('12.17.0')
}
}
}
stage('check space before build') {
steps {
script {
try {
util.spaceLeft()
} catch (err) {
echo "Error when checking the space on node ... ${err}"
}
}
}
}
stage('Install build-chain tool') {
steps {
script {
println '[INFO] Getting build-chain version from composite action file'
def buildChainVersion = buildChain.getBuildChainVersionFromCompositeActionFile()
if ([null, 'null'].contains(buildChainVersion)) {
def errorMessage = "[ERROR] The build-chain version can't be recovered. Please contact administrator"
println errorMessage
error(errorMessage)
}
println "[INFO] build-chain version recovered '${buildChainVersion}'"
sh "npm install -g @kie/build-chain-action@${buildChainVersion}${env.NPM_REGISTRY_URL ? " -registry=${NPM_REGISTRY_URL}" : ''}"
sh "npm list -g | grep build-chain"
}
}
}
stage('Build projects') {
tools {
jdk jdkTool
maven mavenTool
}
steps {
script {
env.LD_LIBRARY_PATH = "${env.LD_LIBRARY_PATH ? "${env.LD_LIBRARY_PATH}:": ''}${GRAALVM_HOME}/lib/server"
echo "LD_LIBRARY_PATH = ${LD_LIBRARY_PATH}"
env.BUILD_MVN_OPTS_CURRENT = "${env.BUILD_MVN_OPTS_CURRENT ?: ''} ${getBuildMavenOptsCurrent()}"
echo "BUILD_MVN_OPTS_CURRENT = ${BUILD_MVN_OPTS_CURRENT}"
if (mavenDeployArtifacts) {
env.DEPLOY_MVN_OPTS = "${env.DEPLOY_MVN_OPTS ?: ''} -DaltDeploymentRepository=local::default::file://${getMavenDeployLocalDir()}"
echo "DEPLOY_MVN_OPTS = ${DEPLOY_MVN_OPTS}"
}
configFileProvider([configFile(fileId: settingsXmlId, variable: 'MAVEN_SETTINGS_FILE')]) {
withCredentials([string(credentialsId: "${GIT_AUTHOR_TOKEN_CREDENTIALS_ID}", variable: 'GITHUB_TOKEN')]) {
env.BUILD_MVN_OPTS = "${env.BUILD_MVN_OPTS ?: ''} -s ${MAVEN_SETTINGS_FILE} -Dmaven.wagon.http.ssl.insecure=true -Dmaven.test.failure.ignore=true"
echo "BUILD_MVN_OPTS = ${BUILD_MVN_OPTS}"
util.runWithPythonVirtualEnv("${getBuildChainCommandline(true)}", 'swf')
}
}
}
}
post {
always {
script {
// Remove `node_modules` to avoid heap space issues with junit command thereafter
// Related to https://github.com/jenkinsci/junit-plugin/issues/478 and https://github.com/jenkinsci/junit-plugin/issues/467
sh 'find . -type d -name node_modules -exec rm -rf {} \\; || true'
junit(testResults: '**/junit.xml, **/target/surefire-reports/**/*.xml, **/target/failsafe-reports/**/*.xml, **/target/invoker-reports/**/*.xml', allowEmptyResults: true)
archiveArtifacts(artifacts: '**/cypress/screenshots/**,**/cypress/videos/**', fingerprint: false, allowEmptyArchive: true)
}
}
unsuccessful {
script {
util.archiveConsoleLog('', 300)
}
}
}
}
stage('Sonar analysis') {
when {
expression { return enableSonarCloudAnalysis }
}
tools {
jdk sonarJdkTool
maven mavenTool
}
steps {
script {
dir(getProjectFolder()) {
maven.runMavenWithSettingsSonar(settingsXmlId, "-e -nsu validate -Psonarcloud-analysis -Denforcer.skip=true ${env.SONARCLOUD_ANALYSIS_MVN_OPTS ?: ''}", 'SONARCLOUD_TOKEN', 'sonar_analysis.maven.log')
}
}
}
}
stage('Upload artifacts to repository') {
when {
expression { return mavenDeployArtifacts }
}
tools {
jdk jdkTool
maven mavenTool
}
steps {
script {
echo "enviroment property mavenDeployArtifacts: " + mavenDeployArtifacts;
// Upload to specific repository with credentials
String mavenDeployRepositoryZipUrl = "${mavenDeployRepository.replaceAll('/content/', '/service/local/').replaceFirst('/*$', '')}/content-compressed"
maven.uploadLocalArtifacts(mavenDeployRepositoryCredsId, getMavenDeployLocalDir(), mavenDeployRepositoryZipUrl)
}
}
}
stage('Trigger downstream jobs') {
when {
expression { return launchDownstreamJobs }
}
steps {
script {
launchDownstreamJobs.each {
echo "Launch downstream jobs with path ${it}"
build(job: "${it}", wait: false, parameters: [], propagate: false)
}
}
}
}
stage('check space after build') {
steps {
script {
util.spaceLeft()
}
}
}
}
post {
unsuccessful {
script {
if (isPRBuildChainType()) {
pullrequest.postComment(util.getMarkdownTestSummary(notificationJobName, getReproducer(true), "${BUILD_URL}", 'GITHUB'), "${GIT_AUTHOR_TOKEN_CREDENTIALS_ID}")
} else if (shouldNotify()) {
withCredentials([string(credentialsId: "${JENKINS_EMAIL_CREDS_ID}", variable: 'KOGITO_CI_EMAIL_TO')]) {
mailer.sendMarkdownTestSummaryNotification(env.NOTIFICATION_JOB_NAME ?: '', "[${env.GIT_BRANCH_NAME}] ${getRepoNameCamelCase(env.REPO_NAME)}", [env.KOGITO_CI_EMAIL_TO], getReproducer())
}
}
}
}
cleanup {
script {
// Clean also docker in case of usage of testcontainers lib
util.cleanNode('docker')
sh 'rm -rf /tmp/quarkus'
}
}
}
}
void setupCypressEnv(String cypressVersion) {
if (env.CYPRESS_BINARY_URL) {
env.CYPRESS_INSTALL_BINARY = "${CYPRESS_BINARY_URL}/cypress-${cypressVersion}.zip"
}
}
boolean isPRBuildChainType() {
return buildChainType == 'cross_pr'
}
boolean isFDBBuildChainType() {
return buildChainType == 'full_downstream'
}
boolean isSingleBuildChainType() {
return buildChainType == 'single'
}
boolean isBranchBuildChainType() {
return buildChainType == 'branch'
}
String getBuildMavenOptsCurrent() {
List opts_current = []
enableSonarCloudAnalysis ? opts_current.add('-Prun-code-coverage') : null
skipTests ? opts_current.add('-DskipTests') : null
skipIntegrationTests ? opts_current.add('-DskipITs') : null
return opts_current.join(' ')
}
boolean shouldNotify() {
return env.ENABLE_NOTIFICATION
}
String getProjectFolder() {
def project = (buildChainProject ? util.getProjectGroupName(buildChainProject) : util.getProjectTriggeringJob())[1]
// First guessing whether there is a clone defined into the buildchain config
// If not, fallback to simple folder structure
String projectFolder = "bc/kiegroup_${project}/${project}"
if (!fileExists(projectFolder)) {
projectFolder = "bc/kiegroup_${project}"
}
return projectFolder
}
String getBuildChainCommandline(boolean includeToken=false) {
return "build-chain build ${buildChainAction} ${includeToken ? "--token ${GITHUB_TOKEN} " : ''}-f 'https://raw.githubusercontent.com/${buildChainConfigGitAuthor}/${buildChainConfigRepo}/${buildChainConfigBranch}/${buildChainConfigDefinitionFilePath}' -o 'bc' ${buildChainAdditionalArguments.join(' ')} --skipParallelCheckout"
}
String getReproducer(boolean isGH = false) {
String reproducer = """
${env.QUARKUS_BRANCH ? "export QUARKUS_BRANCH=${env.QUARKUS_BRANCH}" : ''}
${env.BUILD_MVN_OPTS_CURRENT ? "export BUILD_MVN_OPTS_CURRENT=${env.BUILD_MVN_OPTS_CURRENT}" : ''}
${getBuildChainCommandline()}
NOTE: To install the build-chain tool, please refer to https://github.com/kiegroup/github-action-build-chain#local-execution
"""
if(isGH) {
return """
<details>
<summary><b>Reproducer</b></summary>
${reproducer}
</details>
"""
} else {
return """
```spoiler Reproducer
${reproducer}
```
"""
}
}
String getRepoNameCamelCase(String repo) {
List words = repo.split('-') as List
return words.collect { it.isEmpty() ? it : it.substring(0, 1).toUpperCase() + it.substring(1).toLowerCase() }.join(' ')
}
String getMavenDeployLocalDir() {
return mavenDeployLocalDir ?: "${WORKSPACE}/maven_deploy_dir"
}