forked from apache/incubator-kie-kogito-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile.deploy
338 lines (297 loc) · 10.5 KB
/
Jenkinsfile.deploy
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
import org.jenkinsci.plugins.workflow.libs.Library
@Library('jenkins-pipeline-shared-libraries')_
import org.kie.jenkins.MavenCommand
import org.kie.jenkins.MavenStagingHelper
deployProperties = [:]
pipeline {
agent {
label 'kie-rhel7 && kie-mem16g'
}
tools {
maven 'kie-maven-3.6.2'
jdk 'kie-jdk11'
}
options {
timeout(time: 360, unit: 'MINUTES')
}
// parameters {
// For parameters, check into .jenkins/dsl/jobs.groovy file
// }
environment {
// Static env is defined into .jenkins/dsl/jobs.groovy file
// Keep here for visitibility
MAVEN_OPTS = '-Xms1024m -Xmx4g'
BOT_BRANCH_HASH = "${util.generateHash(10)}"
MAVEN_DEPLOY_LOCAL_DIR="${WORKSPACE}/maven_deploy_dir"
}
stages {
stage('Initialize') {
steps {
script {
cleanWs()
if (params.DISPLAY_NAME) {
currentBuild.displayName = params.DISPLAY_NAME
}
if (isRelease() || isCreatePr()) {
// Verify version is set
assert getProjectVersion()
assert getOptaPlannerVersion()
if(isRelease()) {
// Verify if on right release branch
assert getBuildBranch() == util.getReleaseBranchFromVersion(getProjectVersion())
}
}
checkoutRepo()
}
}
post {
success {
script {
setDeployPropertyIfNeeded('git.branch', getBuildBranch())
setDeployPropertyIfNeeded('git.author', getGitAuthor())
setDeployPropertyIfNeeded('project.version', getProjectVersion())
setDeployPropertyIfNeeded('release', isRelease())
setDeployPropertyIfNeeded('optaplanner.version', getOptaPlannerVersion())
}
}
}
}
stage('Prepare for PR'){
when {
expression { return isRelease() || isCreatePr() }
}
steps {
prepareForPR()
}
}
stage('Update project version'){
when {
expression { return getProjectVersion() != '' }
}
steps {
script {
maven.mvnVersionsUpdateParentAndChildModules(getMavenCommand(), getProjectVersion(), !isRelease())
// Update Optaplanner version
maven.mvnSetVersionProperty(getMavenCommand(), 'version.org.optaplanner', getOptaPlannerVersion())
}
}
}
stage('Build kogito-examples') {
steps {
script {
getMavenCommand().skipTests(params.SKIP_TESTS).run('clean install')
}
}
post {
always {
saveReports(params.SKIP_TESTS)
}
}
}
stage('Check kogito-examples with persistence') {
steps {
script {
sh "cp -r ${getRepoName()} ${getRepoName()}-persistence"
getMavenCommand("${getRepoName()}-persistence")
.skipTests(params.SKIP_TESTS)
.withProfiles(['persistence'])
.run('clean install')
}
}
post {
always {
saveReports(params.SKIP_TESTS)
}
}
}
stage('Check kogito-examples with events') {
steps {
script {
sh "cp -r ${getRepoName()} ${getRepoName()}-events"
getMavenCommand("${getRepoName()}-events")
.skipTests(params.SKIP_TESTS)
.withProfiles(['events'])
.run('clean install')
}
}
post {
always {
saveReports(params.SKIP_TESTS)
}
}
}
stage('Deploy artifacts') {
steps {
script {
if (env.MAVEN_DEPLOY_REPOSITORY && env.MAVEN_REPO_CREDS_ID) {
// Deploy to specific repository with credentials
runMavenDeployLocally()
maven.uploadLocalArtifacts(env.MAVEN_REPO_CREDS_ID, getLocalDeploymentFolder(), getMavenRepoZipUrl())
} else if(!isRelease() || env.MAVEN_DEPLOY_REPOSITORY){
// Normal deploy
runMavenDeploy()
} else {
// Deploy locally and then to staging
runMavenDeployLocally()
runMavenStage()
}
}
}
}
stage('Create PR'){
when {
expression { return isRelease() || isCreatePr() }
}
steps {
commitAndCreatePR()
}
post {
success {
script {
setDeployPropertyIfNeeded("${getRepoName()}.pr.source.uri", "https://github.com/${getBotAuthor()}/${getRepoName()}")
setDeployPropertyIfNeeded("${getRepoName()}.pr.source.ref", getBotBranch())
setDeployPropertyIfNeeded("${getRepoName()}.pr.target.uri", "https://github.com/${getGitAuthor()}/${getRepoName()}")
setDeployPropertyIfNeeded("${getRepoName()}.pr.target.ref", getBuildBranch())
}
}
}
}
}
post {
always {
script {
def propertiesStr = deployProperties.collect{ entry -> "${entry.key}=${entry.value}" }.join("\n")
writeFile(text: propertiesStr, file: env.PROPERTIES_FILE_NAME)
archiveArtifacts(artifacts: env.PROPERTIES_FILE_NAME)
}
}
cleanup {
script {
util.cleanNode('docker')
}
}
}
}
void saveReports(boolean allowEmpty=false){
junit testResults: '**/target/surefire-reports/**/*.xml, **/target/failsafe-reports/**/*.xml', allowEmptyResults: allowEmpty
}
void checkoutRepo() {
dir(getRepoName()) {
deleteDir()
checkout(githubscm.resolveRepository(getRepoName(), getGitAuthor(), getBuildBranch(), false))
}
}
void prepareForPR() {
dir(getRepoName()) {
githubscm.forkRepo(getBotAuthorCredsID())
githubscm.createBranch(getBotBranch())
}
}
void addNotIgnoredPoms() {
// based on https://stackoverflow.com/a/59888964/8811872
sh '''
find . -type f -name 'pom.xml' > found_poms.txt
poms_to_add=""
while IFS= read -r pom; do
if ! git check-ignore -q "\$pom"; then
poms_to_add="\$poms_to_add \$pom"
fi
done < found_poms.txt
rm found_poms.txt
git add \$poms_to_add
git status
'''
}
void commitAndCreatePR() {
def commitMsg = "[${getBuildBranch()}] Update version to ${getProjectVersion()}"
def prBody = "Generated by build ${BUILD_TAG}: ${BUILD_URL}."
if (isRelease()) {
prBody += '\nPlease do not merge, it should be merged automatically after testing.'
} else {
prBody += '\nPlease review and merge.'
}
// Not using githubscm.commitChanges() because globbing won't work.
// See: https://github.com/kiegroup/kogito-runtimes/pull/570#discussion_r449268738
dir(getRepoName()) {
addNotIgnoredPoms()
sh "git commit -m '${commitMsg}'"
githubscm.pushObject('origin', getBotBranch(), getBotAuthorCredsID())
deployProperties["${getRepoName()}.pr.link"] = githubscm.createPR(commitMsg, prBody, getBuildBranch(), getBotAuthorCredsID())
}
}
boolean isRelease() {
return env.RELEASE.toBoolean()
}
boolean isCreatePr() {
return params.CREATE_PR
}
String getRepoName(){
return env.REPO_NAME
}
String getGitAuthor() {
// GIT_AUTHOR can be env or param
return "${GIT_AUTHOR}"
}
String getBuildBranch(){
return params.BUILD_BRANCH_NAME
}
String getProjectVersion(){
return params.PROJECT_VERSION
}
String getOptaPlannerVersion(){
return params.OPTAPLANNER_VERSION
}
String getBotBranch(){
return "${getProjectVersion()}-${env.BOT_BRANCH_HASH}"
}
String getBotAuthor(){
return env.GIT_AUTHOR_BOT
}
String getBotAuthorCredsID(){
return env.BOT_CREDENTIALS_ID
}
void setDeployPropertyIfNeeded(String key, def value){
if (value != null && value != ''){
deployProperties[key] = value
}
}
MavenCommand getMavenCommand(String directory = ''){
directory = directory ?: getRepoName()
MavenCommand mvnCmd = new MavenCommand(this, ['-fae'])
.withSettingsXmlId(env.MAVEN_SETTINGS_CONFIG_FILE_ID)
.inDirectory(directory)
.withProperty('full')
if (env.MAVEN_DEPENDENCIES_REPOSITORY) {
mvnCmd.withDependencyRepositoryInSettings('deps-repo', env.MAVEN_DEPENDENCIES_REPOSITORY)
}
return mvnCmd
}
void runMavenDeploy(){
mvnCmd = getMavenCommand()
if(env.MAVEN_DEPLOY_REPOSITORY){
mvnCmd.withDeployRepository(env.MAVEN_DEPLOY_REPOSITORY)
}
mvnCmd.skipTests(true).run('clean deploy')
}
void runMavenDeployLocally() {
getMavenCommand()
.skipTests(true)
.withLocalDeployFolder(getLocalDeploymentFolder())
.run('clean deploy')
}
void runMavenStage() {
MavenStagingHelper stagingHelper = getStagingHelper()
deployProperties.putAll(stagingHelper.stageLocalArtifacts(env.NEXUS_STAGING_PROFILE_ID, getLocalDeploymentFolder()))
stagingHelper.promoteStagingRepository(env.NEXUS_BUILD_PROMOTION_PROFILE_ID)
}
MavenStagingHelper getStagingHelper() {
return new MavenStagingHelper(this, getMavenCommand())
.withNexusReleaseUrl(env.NEXUS_RELEASE_URL)
.withNexusReleaseRepositoryId(env.NEXUS_RELEASE_REPOSITORY_ID)
}
String getLocalDeploymentFolder(){
return "${env.MAVEN_DEPLOY_LOCAL_DIR}/${getRepoName()}"
}
String getMavenRepoZipUrl() {
return "${env.MAVEN_DEPLOY_REPOSITORY.replaceAll('/content/', '/service/local/').replaceFirst('/*$', '')}/content-compressed"
}