forked from apache/incubator-kie-kogito-pipelines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile.buildchain
278 lines (247 loc) · 12.5 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
@Library('jenkins-pipeline-shared-libraries')_
import org.kie.jenkins.MavenCommand
import org.kie.jenkins.MavenSettingsUtils
agentLabel = "${env.ADDITIONAL_LABEL?.trim() ?: 'ubuntu'} && !built-in"
timeoutValue = env.ADDITIONAL_TIMEOUT?.trim() ?: '360'
jdkTool = env.BUILD_JDK_TOOL
sonarJdkTool = env.SONAR_JDK_TOOL
mavenTool = env.BUILD_MAVEN_TOOL
mavenDeployArtifacts = Boolean.valueOf(env.ENABLE_DEPLOY)
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() ? 'kie-pr-settings' : 'kie-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 ?: 'incubator-kie-kogito-pipelines'
buildChainConfigBranch = env.BUILDCHAIN_CONFIG_BRANCH ?: '\${BRANCH:main}'
buildChainConfigGitAuthor = env.BUILDCHAIN_CONFIG_AUTHOR ?: '\${AUTHOR:apache}'
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 {
docker {
image env.AGENT_DOCKER_BUILDER_IMAGE
label util.avoidFaultyNodes(agentLabel)
args env.AGENT_DOCKER_BUILDER_ARGS
}
}
options {
timestamps()
timeout(time: timeoutValue, unit: 'MINUTES')
}
stages {
stage('Initialize') {
steps {
script {
sh 'printenv > env_props'
archiveArtifacts artifacts: 'env_props'
util.waitForDocker()
sh 'ls -last /var/run/docker.sock'
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
}
}
}
stage('Perform build steps') {
stages {
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"
sh 'sudo alternatives --install /usr/local/bin/build-chain build-chain ${NODE_HOME}/bin/build-chain 1'
sh 'build-chain || true'
}
}
}
stage('Build projects') {
steps {
script {
env.BUILD_MVN_OPTS_CURRENT = "${env.BUILD_MVN_OPTS_CURRENT ?: ''} ${getBuildMavenOptsCurrent()}"
echo "BUILD_MVN_OPTS_CURRENT = ${BUILD_MVN_OPTS_CURRENT}"
Closure buildchainCommandClosure = {
configFileProvider([configFile(fileId: settingsXmlId, variable: 'MAVEN_SETTINGS_FILE')]) {
withCredentials([string(credentialsId: "${BUILDCHAIN_CONFIG_GIT_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}"
sh getBuildChainCommandline(true)
}
}
}
if (mavenDeployArtifacts) {
withCredentials([usernamePassword(credentialsId: mavenDeployRepositoryCredsId, usernameVariable: 'REPOSITORY_USER', passwordVariable: 'REPOSITORY_TOKEN')]) {
env.DEPLOY_MVN_OPTS = "${env.DEPLOY_MVN_OPTS ?: ''} -DdeployAtEnd -Dapache.repository.username=${REPOSITORY_USER} -Dapache.repository.password=${REPOSITORY_TOKEN} -DretryFailedDeploymentCount=5"
echo "DEPLOY_MVN_OPTS = ${DEPLOY_MVN_OPTS}"
// for buildchain not to leak credentials this needs to stay in withCredentials scope
buildchainCommandClosure()
}
} else {
buildchainCommandClosure()
}
}
}
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 }
}
steps {
script {
dir(getProjectFolder()) {
configFileProvider([configFile(fileId: settingsXmlId, variable: 'MAVEN_SETTINGS_FILE')]) {
withCredentials([string(credentialsId: 'SONARCLOUD_TOKEN', variable: 'TOKEN')]) {
new MavenCommand(this)
.withProperty('sonar.login', "${TOKEN}")
.withProperty('sonar.organization', 'apache') // override what's in pom.xml for now
.withProperty('sonar.projectKey', env.SONAR_PROJECT_KEY)
.withLogFileName('sonar_analysis.maven.log')
.withSettingsXmlFile(MAVEN_SETTINGS_FILE)
.run("-e -nsu validate -Psonarcloud-analysis -Denforcer.skip=true ${env.SONARCLOUD_ANALYSIS_MVN_OPTS ?: ''}")
}
}
}
}
}
}
}
}
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)
}
}
}
}
}
post {
unsuccessful {
script {
if (isPRBuildChainType()) {
pullrequest.postComment(util.getMarkdownTestSummary(notificationJobName, getReproducer(true), "${BUILD_URL}", 'GITHUB'), "${BUILDCHAIN_CONFIG_GIT_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 {
cleanWs()
}
}
}
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/apache_${project}/${project}"
if (!fileExists(projectFolder)) {
projectFolder = "bc/apache_${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"
}