forked from apache/incubator-kie-kogito-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile.promote
309 lines (259 loc) · 9.3 KB
/
Jenkinsfile.promote
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
import org.jenkinsci.plugins.workflow.libs.Library
@Library('jenkins-pipeline-shared-libraries')_
import org.kie.jenkins.MavenCommand
deployProperties = [:]
pipelineProperties = [:]
pipeline {
agent {
label 'kie-rhel7'
}
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
BOT_BRANCH_HASH = "${util.generateHash(10)}"
GITHUB_CLI_VERSION = '0.11.1'
}
stages {
stage('Initialization') {
steps {
script {
cleanWs()
if (params.DISPLAY_NAME) {
currentBuild.displayName = params.DISPLAY_NAME
}
readDeployProperties()
if (isRelease()) {
// Verify version is set and if on right release branch
assert getProjectVersion()
assert getOptaPlannerVersion()
assert getBuildBranch() == util.getReleaseBranchFromVersion(getProjectVersion())
}
installGithubCLI()
}
}
}
stage('Merge deploy PR and tag') {
when {
expression { return isRelease() }
}
steps{
script {
dir(getRepoName()) {
checkoutRepo()
mergeAndPush(getDeployPrLink())
tagLatest()
if(params.UPDATE_STABLE_BRANCH) {
githubscm.createBranch('stable')
forcePushProtectedBranch('stable', 'master')
}
}
}
}
}
stage('Set examples next snapshot version'){
when {
expression { return isRelease() }
}
steps {
script {
dir('bot') {
prepareForPR()
maven.mvnVersionsUpdateParentAndChildModules(getMavenCommand(), getSnapshotVersion(), true)
maven.mvnSetVersionProperty(getMavenCommand(),'version.org.optaplanner', util.getNextVersion(getOptaPlannerVersion(), 'micro'))
commitAndCreatePR()
}
dir(getRepoName()) {
sh "git checkout ${getBuildBranch()}"
mergeAndPush(getPipelinePrLink())
runMavenDeploy()
}
}
}
}
}
post {
cleanup {
script {
util.cleanNode('docker')
}
}
}
}
//////////////////////////////////////////////////////////////////////////////
// Deployment properties
//////////////////////////////////////////////////////////////////////////////
void readDeployProperties(){
String deployUrl = params.DEPLOY_BUILD_URL
if (deployUrl != ''){
if(!deployUrl.endsWith('/')){
deployUrl += '/'
}
sh "wget ${deployUrl}artifact/${PROPERTIES_FILE_NAME} -O ${PROPERTIES_FILE_NAME}"
deployProperties = readProperties file: PROPERTIES_FILE_NAME
// echo all properties
echo deployProperties.collect{ entry -> "${entry.key}=${entry.value}" }.join('\n')
}
}
boolean hasDeployProperty(String key){
return deployProperties[key] != null
}
String getDeployProperty(String key){
if(hasDeployProperty(key)){
return deployProperties[key]
}
return ''
}
String getParamOrDeployProperty(String paramKey, String deployPropertyKey){
if (params[paramKey] != ''){
return params[paramKey]
}
return getDeployProperty(deployPropertyKey)
}
//////////////////////////////////////////////////////////////////////////////
// Getter / Setter
//////////////////////////////////////////////////////////////////////////////
boolean isRelease() {
return env.RELEASE.toBoolean()
}
String getRepoName() {
return env.REPO_NAME
}
String getProjectVersion() {
return getParamOrDeployProperty('PROJECT_VERSION', 'project.version')
}
String getOptaPlannerVersion() {
return getParamOrDeployProperty('OPTAPLANNER_VERSION', 'optaplanner.version')
}
String getSnapshotVersion() {
return util.getNextVersion(getProjectVersion(), 'micro')
}
String getGitTag() {
return params.GIT_TAG != '' ? params.GIT_TAG : getProjectVersion()
}
String getBuildBranch() {
return params.BUILD_BRANCH_NAME
}
String getGitAuthor() {
return env.GIT_AUTHOR
}
String getGitAuthorCredsID(){
return env.AUTHOR_CREDS_ID
}
String getBotAuthorCredsID(){
return env.BOT_CREDENTIALS_ID
}
String getDeployPrLink(){
return getDeployProperty("${getRepoName()}.pr.link")
}
String getPipelinePrLink(){
return pipelineProperties["${getRepoName()}.pr.link"]
}
void setPipelinePrLink(String value){
pipelineProperties["${getRepoName()}.pr.link"] = value
}
String getSnapshotBranch(){
return "${getSnapshotVersion().toLowerCase()}-${env.BOT_BRANCH_HASH}"
}
//////////////////////////////////////////////////////////////////////////////
// Git
//////////////////////////////////////////////////////////////////////////////
void checkoutRepo() {
deleteDir()
checkout(githubscm.resolveRepository(getRepoName(), getGitAuthor(), getBuildBranch(), false))
// need to manually checkout branch since on a detached branch after checkout command
sh "git checkout ${getBuildBranch()}"
}
void mergeAndPush(String prLink) {
if (prLink) {
githubscm.mergePR(prLink, getGitAuthorCredsID())
githubscm.pushObject('origin', getBuildBranch(), getGitAuthorCredsID())
}
}
void tagLatest() {
if (getGitTag()) {
githubscm.tagLocalAndRemoteRepository('origin', getGitTag(), getGitAuthorCredsID(), env.BUILD_TAG, true)
}
}
void prepareForPR() {
checkoutRepo()
githubscm.forkRepo(getBotAuthorCredsID())
githubscm.createBranch(getSnapshotBranch())
}
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
'''
}
void commitAndCreatePR() {
def commitMsg = "[${getBuildBranch()}] Update version to ${getSnapshotVersion()}"
def prBody = "Generated by build ${BUILD_TAG}: ${BUILD_URL}.\nPlease do not merge, it should be merged automatically."
// Not using githubscm.commitChanges() because globbing won't work.
// See: https://github.com/kiegroup/kogito-runtimes/pull/570#discussion_r449268738
addNotIgnoredPoms()
sh "git commit -m '${commitMsg}'"
githubscm.pushObject('origin', getSnapshotBranch(), getBotAuthorCredsID())
setPipelinePrLink(githubscm.createPR(commitMsg, prBody, getBuildBranch(), getBotAuthorCredsID()))
}
void installGithubCLI() {
sh """
wget https://github.com/cli/cli/releases/download/v${env.GITHUB_CLI_VERSION}/gh_${env.GITHUB_CLI_VERSION}_linux_amd64.tar.gz
tar xzf gh_${env.GITHUB_CLI_VERSION}_linux_amd64.tar.gz
mv gh_${env.GITHUB_CLI_VERSION}_linux_amd64/bin/gh .
rm -r gh_${env.GITHUB_CLI_VERSION}_linux_amd64*
"""
}
void setDefaultBranch(String defaultBranch) {
withCredentials([string(credentialsId: env.GITHUB_TOKEN_CREDS_ID, variable: 'GITHUB_TOKEN')]) {
// gh command from https://github.com/cli/cli/issues/929#issuecomment-629253585
def newDefaultBranch = sh(script: "../gh api -XPATCH 'repos/${getGitAuthor()}/${getRepoName()}' -f default_branch=${defaultBranch} | jq '.default_branch'", returnStdout: true).trim()
if (newDefaultBranch == "\"${defaultBranch}\"") {
echo "[INFO] ${getGitAuthor()}/${getRepoName()}'s default branch has been updated to ${newDefaultBranch}."
} else {
error 'Couldn\'t update default branch.'
}
}
}
void forcePushProtectedBranch(String defaultBranch, String tempBranch) {
setDefaultBranch(tempBranch)
withCredentials([usernamePassword(credentialsId: getGitAuthorCredsID(), usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
sh """
git config --local credential.helper \"!f() { echo username=\\$GIT_USERNAME; echo password=\\$GIT_PASSWORD; }; f\"
git push --delete origin ${defaultBranch}
git push origin ${defaultBranch}
"""
}
setDefaultBranch(defaultBranch)
}
MavenCommand getMavenCommand() {
mvnCmd = new MavenCommand(this, ['-fae'])
.withSettingsXmlId(env.MAVEN_SETTINGS_CONFIG_FILE_ID)
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')
}