-
Notifications
You must be signed in to change notification settings - Fork 210
/
Fabric8Commands.groovy
557 lines (465 loc) · 19.2 KB
/
Fabric8Commands.groovy
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#!/usr/bin/groovy
package io.fabric8
import com.cloudbees.groovy.cps.NonCPS
import io.fabric8.kubernetes.client.DefaultKubernetesClient
import groovy.json.JsonSlurper
import io.fabric8.openshift.client.DefaultOpenShiftClient
import io.fabric8.openshift.client.OpenShiftClient
import io.fabric8.kubernetes.client.KubernetesClient
def getProjectVersion(){
def file = readFile('pom.xml')
def project = new XmlSlurper().parseText(file)
return project.version.text()
}
def getReleaseVersion(String artifact) {
def modelMetaData = new XmlSlurper().parse("https://oss.sonatype.org/content/repositories/releases/${artifact}/maven-metadata.xml")
def version = modelMetaData.versioning.release.text()
return version
}
def getMavenCentralVersion(String artifact) {
def modelMetaData = new XmlSlurper().parse("http://central.maven.org/maven2/${artifact}/maven-metadata.xml")
def version = modelMetaData.versioning.release.text()
return version
}
def getVersion(String repo, String artifact) {
repo = removeTrailingSlash(repo)
artifact = removeTrailingSlash(artifact)
def modelMetaData = new XmlSlurper().parse(repo+'/'+artifact+'/maven-metadata.xml')
def version = modelMetaData.versioning.release.text()
return version
}
def isArtifactAvailableInRepo(String repo, String groupId, String artifactId, String version, String ext) {
repo = removeTrailingSlash(repo)
groupId = removeTrailingSlash(groupId)
artifactId = removeTrailingSlash(artifactId)
def url = new URL("${repo}/${groupId}/${artifactId}/${version}/${artifactId}-${version}.${ext}")
HttpURLConnection connection = url.openConnection()
connection.setRequestMethod("GET")
connection.setDoInput(true)
try {
connection.connect()
new InputStreamReader(connection.getInputStream(),"UTF-8")
return true
} catch( FileNotFoundException e1 ) {
echo "File not yet available: ${url.toString()}"
return false
} finally {
connection.disconnect()
}
}
def removeTrailingSlash (String myString){
if (myString.endsWith("/")) {
return myString.substring(0, myString.length() - 1)
}
return myString
}
def getRepoIds() {
// we could have multiple staging repos created, we need to write the names of all the generated files to a well known
// filename so we can use the workflow readFile (wildcards wont works and new File wont with slaves as groovy is executed on the master jenkins
// We write the names of the files that contain the repo ids used for staging. Each staging repo id is read from each file and returned as a list
sh 'find target/nexus-staging/staging/ -maxdepth 1 -name "*.properties" > target/nexus-staging/staging/repos.txt'
def repos = readFile('target/nexus-staging/staging/repos.txt')
def list = []
// workflow closure not working here https://issues.jenkins-ci.org/browse/JENKINS-26481
def filelines = new String(repos).split( '\n' )
for(int i = 0; i < filelines.size(); i++){
def matcher = readFile(filelines[i]) =~ 'stagingRepository.id=(.+)'
list << matcher[0][1]
}
return list
}
def getDockerHubImageTags(String image) {
try {
return "https://registry.hub.docker.com/v1/repositories/${image}/tags".toURL().getText()
} catch (err) {
return "NO_IMAGE_FOUND"
}
}
def searchAndReplaceMavenVersionProperty(String property, String newVersion){
// example matches <fabric8.version>2.3</fabric8.version> <fabric8.version>2.3.12</fabric8.version> <fabric8.version>2.3.12.5</fabric8.version>
sh "find -type f -name 'pom.xml' | xargs sed -i -r 's/${property}[0-9][0-9]{0,2}.[0-9][0-9]{0,2}(.[0-9][0-9]{0,2})?(.[0-9][0-9]{0,2})?</${property}${newVersion}</g'"
sh "git commit -a -m 'Bump ${property} version'"
}
def searchAndReplaceMavenSnapshotProfileVersionProperty(String property, String newVersion){
// example matches <fabric8.version>2.3-SNAPSHOT</fabric8.version> <fabric8.version>2.3.12-SNAPSHOT</fabric8.version> <fabric8.version>2.3.12.5-SNAPSHOT</fabric8.version>
sh "find -type f -name 'pom.xml' | xargs sed -i -r 's/${property}[0-9][0-9]{0,2}.[0-9][0-9]{0,2}(.[0-9][0-9]{0,2})?(.[0-9][0-9]{0,2})?-SNAPSHOT</${property}${newVersion}-SNAPSHOT</g'"
sh "git commit -a -m 'Bump ${property} development profile SNAPSHOT version'"
}
def setupWorkspaceForRelease(String project, Boolean useGitTagForNextVersion, String mvnExtraArgs = "", String currentVersion = ""){
sh "git config user.email fabric8-admin@googlegroups.com"
sh "git config user.name fabric8-release"
sh "git tag -d \$(git tag)"
sh "git fetch --tags"
if (useGitTagForNextVersion){
def newVersion = getNewVersionFromTag(currentVersion)
echo "New release version ${newVersion}"
sh "mvn -U versions:set -DnewVersion=${newVersion} " + mvnExtraArgs
sh "git commit -a -m 'release ${newVersion}'"
pushTag(newVersion)
} else {
sh 'mvn build-helper:parse-version versions:set -DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion} ' + mvnExtraArgs
}
def releaseVersion = getProjectVersion()
// delete any previous branches of this release
try {
sh "git checkout -b release-v${releaseVersion}"
} catch (err){
sh "git branch -D release-v${releaseVersion}"
sh "git checkout -b release-v${releaseVersion}"
}
try {
deleteRemoteBranch("release-v${releaseVersion}")
} catch (err){
}
}
// if no previous tag found default 1.0.0 is used, else assume version is in the form major.minor or major.minor.micro version
def getNewVersionFromTag(pomVersion = null){
def version = '0.0.1'
// Set known prerelease prefixes, needed for the proper sort order
// in the next command
sh "git config versionsort.prereleaseSuffix -RC"
sh "git config versionsort.prereleaseSuffix -M"
// if the repo has no tags this command will fail
sh "git tag --sort version:refname | tail -1 > version.tmp"
def tag = readFile 'version.tmp'
if (tag == null || tag.size() == 0){
echo "no existing tag found using version ${version}"
return version
}
tag = tag.trim()
echo "Testing to see if version ${tag} is semver compatible"
def semver = tag =~ /(?i)\bv?(?<major>0|[1-9]\d*)(?:\.(?<minor>0|[1-9]\d*)(?:\.(?<patch>0|[1-9]\d*))?)?(?:-(?<prerelease>[\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+(?<build>[\da-z\-]+(?:\.[\da-z\-]+)*))?\b/
if (semver.matches()) {
echo "Version ${tag} is semver compatible"
def majorVersion = semver.group('major') as int
def minorVersion = (semver.group('minor') ?: 0) as int
def patchVersion = ((semver.group('patch') ?: 0) as int) + 1
echo "Testing to see if current POM version ${pomVersion} is semver compatible"
def pomSemver = pomVersion.trim() =~ /(?i)\bv?(?<major>0|[1-9]\d*)(?:\.(?<minor>0|[1-9]\d*)(?:\.(?<patch>0|[1-9]\d*))?)?(?:-(?<prerelease>[\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+(?<build>[\da-z\-]+(?:\.[\da-z\-]+)*))?\b/
if (pomSemver.matches()) {
echo "Current POM version ${pomVersion} is semver compatible"
def pomMajorVersion = pomSemver.group('major') as int
def pomMinorVersion = (pomSemver.group('minor') ?: 0) as int
def pomPatchVersion = (pomSemver.group('patch') ?: 0) as int
if (pomMajorVersion > majorVersion ||
(pomMajorVersion == majorVersion &&
(pomMinorVersion > minorVersion) || (pomMinorVersion == minorVersion && pomPatchVersion > patchVersion)
)
) {
majorVersion = pomMajorVersion
minorVersion = pomMinorVersion
patchVersion = pomPatchVersion
}
}
def newVersion = "${majorVersion}.${minorVersion}.${patchVersion}"
echo "New version is ${newVersion}"
return newVersion
} else {
echo "Version is not semver compatible"
// strip the v prefix from the tag so we can use in a maven version number
def previousReleaseVersion = tag.substring(tag.lastIndexOf('v')+1)
echo "Previous version found ${previousReleaseVersion}"
// if there's an int as the version then turn it into a major.minor.micro version
if (previousReleaseVersion.isNumber()){
return previousReleaseVersion + '.0.1'
} else {
// if previous tag is not a number and doesnt have a '.' version seperator then error until we have one
if (previousReleaseVersion.lastIndexOf('.') == 0){
error "found invalid latest tag [${previousReleaseVersion}] set to major.minor.micro to calculate next release version"
}
// increment the release number after the last seperator '.'
def microVersion = previousReleaseVersion.substring(previousReleaseVersion.lastIndexOf('.')+1) as int
return previousReleaseVersion.substring(0, previousReleaseVersion.lastIndexOf('.')+1) + (microVersion+1)
}
}
}
def stageSonartypeRepo () {
try {
sh "mvn clean -B"
sh "mvn -V -B -e -U install org.sonatype.plugins:nexus-staging-maven-plugin:1.6.7:deploy -P release -DnexusUrl=https://oss.sonatype.org -DserverId=oss-sonatype-staging -Ddocker.push.registry=${env.FABRIC8_DOCKER_REGISTRY_SERVICE_HOST}:${env.FABRIC8_DOCKER_REGISTRY_SERVICE_PORT}"
// lets not archive artifacts until we if we just use nexus or a content repo
//step([$class: 'ArtifactArchiver', artifacts: '**/target/*.jar', fingerprint: true])
} catch (err) {
hubot room: 'release', message: "Release failed when building and deploying to Nexus ${err}"
currentBuild.result = 'FAILURE'
error "ERROR Release failed when building and deploying to Nexus ${err}"
}
// the sonartype staging repo id gets written to a file in the workspace
return getRepoIds()
}
def releaseSonartypeRepo(String repoId){
try {
// release the sonartype staging repo
sh "mvn org.sonatype.plugins:nexus-staging-maven-plugin:1.6.5:rc-release -DserverId=oss-sonatype-staging -DnexusUrl=https://oss.sonatype.org -DstagingRepositoryId=${repoId} -Ddescription=\"Next release is ready\" -DstagingProgressTimeoutMinutes=60"
} catch (err) {
sh "mvn org.sonatype.plugins:nexus-staging-maven-plugin:1.6.5:rc-drop -DserverId=oss-sonatype-staging -DnexusUrl=https://oss.sonatype.org -DstagingRepositoryId=${repoId} -Ddescription=\"Error during release: ${err}\" -DstagingProgressTimeoutMinutes=60"
currentBuild.result = 'FAILURE'
error "ERROR releasing sonartype repo ${repoId}: ${err}"
}
}
def dropStagingRepo(String repoId){
echo "Not a release so dropping staging repo ${repoId}"
sh "mvn org.sonatype.plugins:nexus-staging-maven-plugin:1.6.5:rc-drop -DserverId=oss-sonatype-staging -DnexusUrl=https://oss.sonatype.org -DstagingRepositoryId=${repoId} -Ddescription=\"Dry run\" -DstagingProgressTimeoutMinutes=60"
}
def helm(){
def pluginVersion = getReleaseVersion("io/fabric8/fabric8-maven-plugin")
try {
sh "mvn io.fabric8:fabric8-maven-plugin:${pluginVersion}:helm"
sh "mvn io.fabric8:fabric8-maven-plugin:${pluginVersion}:helm-push"
} catch (err) {
error "ERROR with helm push ${err}"
}
}
def pushTag(String releaseVersion){
sh "git tag -fa v${releaseVersion} -m 'Release version ${releaseVersion}'"
sh "git push origin v${releaseVersion}"
}
def updateGithub(){
def releaseVersion = getProjectVersion()
sh "git push origin release-v${releaseVersion}"
}
def updateNextDevelopmentVersion(String releaseVersion, String mvnExtraArgs = ""){
// update poms back to snapshot again
sh 'mvn build-helper:parse-version versions:set -DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion}-SNAPSHOT ' + mvnExtraArgs
def snapshotVersion = getProjectVersion()
sh "git commit -a -m '[CD] prepare for next development iteration ${snapshotVersion}'"
sh "git push origin release-v${releaseVersion}"
}
def hasChangedSinceLastRelease(){
sh "git log --name-status HEAD^..HEAD -1 --grep=\"prepare for next development iteration\" --author='fusesource-ci' >> gitlog.tmp"
def myfile = readFile('gitlog.tmp')
//sh "rm gitlog.tmp"
// if the file size is 0 it means the CI user was not the last commit so project has changed
if (myfile.length() == 0 ) {
return true
} else {
return false
}
}
def getOldVersion(){
def matcher = readFile('website/src/docs/index.page') =~ '<h1>Documentation for version (.+)</h1>'
matcher ? matcher[0][1] : null
}
def updateDocsAndSite(String newVersion){
// get previous version
def oldVersion = getOldVersion()
if (oldVersion == null){
echo "No previous version found"
return
}
// use perl so that we we can easily turn off regex in the SED query as using dots in version numbers returns unwanted results otherwise
sh "find . -name '*.md' ! -name Changes.md ! -path '*/docs/jube/**.*' | xargs perl -p -i -e 's/\\Q${oldVersion}/${newVersion}/g'"
sh "find . -path '*/website/src/**.*' | xargs perl -p -i -e 's/\\Q${oldVersion}/${newVersion}/g'"
sh "git commit -a -m '[CD] Update docs following ${newVersion} release'"
}
def runSystemTests(){
sh 'cd systests && mvn clean && mvn integration-test verify'
}
def createPullRequest(String message, String project, String branch){
def githubToken = getGitHubToken()
def apiUrl = new URL("https://api.github.com/repos/${project}/pulls")
echo "creating PR for ${apiUrl}"
try {
HttpURLConnection connection = apiUrl.openConnection()
if(githubToken.length() > 0)
{
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestMethod("POST")
connection.setDoOutput(true)
connection.connect()
def body = """
{
"title": "${message}",
"head": "${branch}",
"base": "master"
}
"""
echo "sending body: ${body}\n"
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream())
writer.write(body)
writer.flush()
// execute the POST request
def rs = new JsonSlurper().parse(new InputStreamReader(connection.getInputStream(),"UTF-8"))
connection.disconnect()
echo "Received PR id: ${rs.number}"
return rs.number +''
} catch (err) {
error "ERROR ${err}"
}
}
def addMergeCommentToPullRequest(String pr, String project){
def githubToken = getGitHubToken()
def apiUrl = new URL("https://api.github.com/repos/${project}/issues/${pr}/comments")
echo "merge PR using comment sent to ${apiUrl}"
try {
def HttpURLConnection connection = apiUrl.openConnection()
if(githubToken.length() > 0)
{
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestMethod("POST")
connection.setDoOutput(true)
connection.connect()
def body = '{"body":"[merge] lgtm"}'
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream())
writer.write(body)
writer.flush()
// execute the POST request
new InputStreamReader(connection.getInputStream())
connection.disconnect()
} catch (err) {
error "ERROR ${err}"
}
}
def drop(String pr, String project){
def githubToken = getGitHubToken()
def apiUrl = new URL("https://api.github.com/repos/${project}/pulls/${pr}")
def branch
HttpURLConnection connection
OutputStreamWriter writer
echo "closing PR ${apiUrl}"
try {
connection = apiUrl.openConnection()
if(githubToken.length() > 0)
{
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestMethod("POST")
connection.setDoOutput(true)
connection.connect()
def body = '''
{
"body": "release aborted",
"state": "closed"
}
'''
writer = new OutputStreamWriter(connection.getOutputStream())
writer.write(body)
writer.flush()
// execute the POST request
def rs = new JsonSlurper().parse(new InputStreamReader(connection.getInputStream(),"UTF-8"))
connection.disconnect()
branchName = rs.head.ref
} catch (err) {
error "ERROR ${err}"
}
try {
apiUrl = new URL("https://api.github.com/repos/${project}/git/refs/heads/${branchName}")
connection = apiUrl.openConnection()
if(githubToken.length() > 0)
{
connection.setRequestProperty("Authorization", "Bearer ${githubToken}")
}
connection.setRequestMethod("DELETE")
connection.setDoOutput(true)
connection.connect()
writer = new OutputStreamWriter(connection.getOutputStream())
writer.write(body)
writer.flush()
// execute the POST request
new InputStreamReader(connection.getInputStream())
connection.disconnect()
} catch (err) {
error "ERROR ${err}"
}
}
def deleteRemoteBranch(String branchName){
container(name: 'clients') {
sh 'chmod 600 /root/.ssh-git/ssh-key'
sh 'chmod 600 /root/.ssh-git/ssh-key.pub'
sh 'chmod 700 /root/.ssh-git'
sh "git push origin --delete ${branchName}"
}
}
def getGitHubToken(){
def tokenPath = '/home/jenkins/.apitoken/hub'
def githubToken = readFile tokenPath
if (!githubToken?.trim()) {
error "No GitHub token found in ${tokenPath}"
}
return githubToken.trim()
}
@NonCPS
def isSingleNode() {
KubernetesClient kubernetes = new DefaultKubernetesClient()
if (kubernetes.nodes().list().getItems().size() == 1){
return true
} else {
return false
}
}
@NonCPS
def hasService(String name) {
KubernetesClient kubernetes = new DefaultKubernetesClient()
try {
def service = kubernetes.services().withName(name).get()
if (service != null) {
return service.metadata != null
}
return false
} catch (e) {
// ignore errors
return false
}
}
@NonCPS
def getServiceURL(String serviceName, String namespace = null, String protocol = "http", boolean external = true) {
KubernetesClient kubernetes = new DefaultKubernetesClient()
if (namespace == null) namespace = kubernetes.getNamespace()
return KubernetesHelper.getServiceURL(kubernetes, serviceName, namespace, protocol, external)
}
def isOpenShiftS2I() {
def openshiftYaml = "target/classes/META-INF/fabric8/openshift.yml"
try {
if (fileExists(openshiftYaml)) {
def contents = readFile(openshiftYaml)
if (contents != null) {
if (contents.contains('kind: "ImageStream"') || contents.contains('kind: ImageStream') || contents.contains('kind: \'ImageStream\'')) {
echo "OpenShift YAML contains an ImageStream"
return true
} else {
echo "OpenShift YAML does not contain an ImageStream so not using S2I binary mode"
}
}
} else {
echo "Warning OpenShift YAML ${openshiftYaml} does not exist!"
}
} catch (e) {
error "Failed to load ${openshiftYaml} due to ${e}"
}
return false
}
/**
* Deletes the given namespace if it exists
*
* @param name the name of the namespace
* @return true if the delete was successful
*/
@NonCPS
def deleteNamespace(String name) {
KubernetesClient kubernetes = new DefaultKubernetesClient()
try {
def namespace = kubernetes.namespaces().withName(name).get()
if (namespace != null) {
echo "Deleting namespace ${name}..."
kubernetes.namespaces().withName(name).delete()
echo "Deleted namespace ${name}"
// TODO should we wait for the namespace to really go away???
namespace = kubernetes.namespaces().withName(name).get()
if (namespace != null) {
echo "Namespace ${name} still exists!"
}
return true
}
return false
} catch (e) {
// ignore errors
return false
}
}
@NonCPS
def isOpenShift() {
return new DefaultOpenShiftClient().isAdaptable(OpenShiftClient.class)
}
return this