-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathJenkinsfile.tools.update-jenkins-dependencies
224 lines (193 loc) · 8.57 KB
/
Jenkinsfile.tools.update-jenkins-dependencies
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
import org.jenkinsci.plugins.workflow.libs.Library
@Library('jenkins-pipeline-shared-libraries')_
// This Jenkinsfile has not been tested on Apache Jenkins yet
// and will likely fail due to restrictions on Groovy sandbox methods
// See `findPlugin` and `getJenkinsVersion` methods
branchCreated = false
pipeline {
agent {
docker {
image env.AGENT_DOCKER_BUILDER_IMAGE
args env.AGENT_DOCKER_BUILDER_ARGS
label util.avoidFaultyNodes('ubuntu')
}
}
options {
timeout(time: 60, unit: 'MINUTES')
}
environment {
KOGITO_CI_EMAIL_TO = credentials("${JENKINS_EMAIL_CREDS_ID}")
}
stages {
stage('Checkout repo') {
steps {
script {
dir(getRepoName()) {
deleteDir()
// Checkout repository
checkout(githubscm.resolveRepository(getRepoName(), getGitAuthor(), getBuildBranch(), false, getGitAuthorCredsId()))
githubscm.setUserConfig(getGitAuthorCredsId())
branchCreated = getOrCreateGitBranch(getPRBranch(), getGitAuthorPushCredsId())
}
}
}
}
stage('Update DSL Gradle dependencies') {
steps {
script {
dir(getRepoName()) {
String lookupCmd = '''
cat dsl/seed/gradle.properties | grep 'version_jenkins_plugins_' | awk -F'version_jenkins_plugins_' '{print $2}' | awk -F'=' '{print $1}' | tr '\n' ','
'''
def plugins = sh(returnStdout: true, script: lookupCmd).trim().split(',')
echo "Got plugins ${plugins}"
def managedPluginsVersion = [:]
plugins.each {
String pluginShortName = it.replaceAll('_', '-')
String pluginVarName = "version_jenkins_plugins_${it}"
def plugin = findPlugin(pluginShortName)
if (plugin) {
managedPluginsVersion[pluginVarName] = plugin.getVersion()
} else {
unstable("Could not find plugin ${pluginShortName}. Please make sure it is installed")
}
}
echo "Got managed plugins version: ${managedPluginsVersion}"
managedPluginsVersion.each { pluginVarName, pluginVersion ->
echo "Updating plugin ${pluginVarName} with version ${pluginVersion}"
sh """
sed -i 's|${pluginVarName}=.*|${pluginVarName}=${pluginVersion}|g' dsl/seed/gradle.properties
"""
}
String jenkinsVersion = getJenkinsVersion()
echo "Updating Jenkins version to version ${jenkinsVersion}"
sh """
sed -i 's|version_jenkins=.*|version_jenkins=${jenkinsVersion}|g' dsl/seed/gradle.properties
"""
}
}
}
}
stage('Update Maven tests dependencies') {
steps {
script {
dir(getRepoName()) {
String lookupCmd = '''
cat .ci/jenkins/tests/pom.xml | grep '<version.jenkins.plugins.' | awk -F'>' '{print $1}' | awk -F'<version.jenkins.plugins.' '{print $2}' | tr '\n' ','
'''
def plugins = sh(returnStdout: true, script: lookupCmd).trim().split(',')
echo "Got plugins ${plugins}"
def managedPluginsVersion = [:]
plugins.each {
String pluginShortName = it
String pluginVarName = "version.jenkins.plugins.${it}"
def plugin = findPlugin(pluginShortName)
if (plugin) {
managedPluginsVersion[pluginVarName] = plugin.getVersion()
} else {
unstable("Could not find plugin ${pluginShortName}. Please make sure it is installed")
}
}
echo "Got managed plugins version: ${managedPluginsVersion}"
managedPluginsVersion.each { pluginVarName, pluginVersion ->
echo "Updating plugin ${pluginVarName} with version ${pluginVersion}"
sh """
sed -i 's|<${pluginVarName}>.*</${pluginVarName}>|<${pluginVarName}>${pluginVersion}</${pluginVarName}>|g' .ci/jenkins/tests/pom.xml
"""
}
String jenkinsVersion = getJenkinsVersion()
echo "Updating Jenkins version to version ${jenkinsVersion}"
sh """
sed -i 's|<version.jenkins>.*</version.jenkins>|<version.jenkins>${jenkinsVersion}</version.jenkins>|g' .ci/jenkins/tests/pom.xml
"""
}
}
}
}
stage('Create PR') {
steps {
script {
dir(getRepoName()) {
if (githubscm.isThereAnyChanges()) {
def commitMsg = "[${getBuildBranch()}] Update Jenkins dependencies to DSL/Jenkins files"
githubscm.commitChanges(commitMsg, {
githubscm.findAndStageNotIgnoredFiles('pom.xml')
githubscm.findAndStageNotIgnoredFiles('gradle.properties')
})
githubscm.pushObject('origin', getPRBranch(), getGitAuthorPushCredsId())
if (branchCreated) {
def prBody = "Generated by build ${BUILD_TAG}: ${BUILD_URL}.\nPlease review and merge."
prLink = githubscm.createPR(commitMsg, prBody, getBuildBranch(), getGitAuthorCredsId())
sendNotification("Please review PR ${prLink}")
} else {
echo "Branch ${getPRBranch()} was already created so assuming the PR exists alrerady ..."
}
} else {
println '[WARN] no changes to commit'
}
}
}
}
}
}
post {
unsuccessful {
script {
sendErrorNotification()
}
}
}
}
def findPlugin(String pluginShortName) {
return Jenkins.instance.pluginManager.plugins.find { it.getShortName() == pluginShortName }
}
String getJenkinsVersion() {
return Jenkins.getVersion()
}
void sendNotification(String body) {
emailext body: "**Jenkins dependencies update to DSL/Jenkins files**\n${body}",
subject: "[${getBuildBranch()}] Kogito pipelines",
to: env.KOGITO_CI_EMAIL_TO
}
void sendErrorNotification() {
String body = """
Job #${BUILD_NUMBER} was: **${currentBuild.currentResult}**
Please look here: ${BUILD_URL}
"""
sendNotification(body)
}
String getBuildBranch() {
return env.BUILD_BRANCH_NAME
}
String getGitAuthor() {
return env.GIT_AUTHOR
}
String getGitAuthorCredsId() {
return env.GIT_AUTHOR_CREDS_ID
}
String getGitAuthorPushCredsId() {
return env.GIT_AUTHOR_PUSH_CREDS_ID
}
String getPRBranch() {
return "${getBuildBranch()}-${getJenkinsVersion()}"
}
String getRepoName() {
return env.REPO_NAME
}
boolean getOrCreateGitBranch(String branch, String credentialsId) {
sh 'git fetch origin'
String branchRemoteResult = sh(script: "git ls-remote origin ${branch} | wc -l", returnStdout: true).trim()
if (Integer.parseInt(branchRemoteResult) > 0) {
echo "Branch ${branch} already exist ... will not create it. Checking out !"
sh """
git checkout origin/${branch} -b ${branch}
git merge origin/${getBuildBranch()}
"""
return false
} else {
echo "Branch ${branch} does not exist ... gonna create it"
githubscm.createBranch(branch)
githubscm.pushObject('origin', branch, credentialsId)
return true
}
}