-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathJenkinsfile
214 lines (203 loc) · 7.2 KB
/
Jenkinsfile
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
import java.util.regex.Matcher
pipeline {
environment {
lockFilePath = null
version = null
silverpeasCore = null
}
agent {
docker {
image 'silverpeas/silverbuild:6.4'
args '''
-v $HOME/.m2:/home/silverbuild/.m2
-v $HOME/.gitconfig:/home/silverbuild/.gitconfig
-v $HOME/.ssh:/home/silverbuild/.ssh
-v $HOME/.gnupg:/home/silverbuild/.gnupg
'''
}
}
stages {
stage('Waiting for core running build if any') {
steps {
script {
println "Current branch is ${env.BRANCH_NAME}"
println "Actual stable branch is ${env.STABLE_BRANCH}"
def pom = readMavenPom()
silverpeasCore = getSilverpeasCoreProject(pom)
version = computeSnapshotVersion(pom)
lockFilePath = createLockFile(version, 'components')
waitForDependencyRunningBuildIfAny(version, 'core')
}
}
}
stage('Build') {
steps {
script {
sh "/opt/wildfly-for-tests/wildfly-*.Final/bin/standalone.sh -c standalone-full.xml &> /dev/null &"
checkParentPOMVersion(version)
boolean coreDependencyExists = existsDependency(version, 'core')
def coreVersion
if (coreDependencyExists) {
coreVersion = version
} else {
coreVersion = getCoreDependencyVersion()
}
sh """
sed -i -e "s/<core.version>[\\\${}0-9a-zA-Z.-]\\+/<core.version>${coreVersion}/g" pom.xml
mvn -U versions:set -DgenerateBackupPoms=false -DnewVersion=${version}
mvn clean install -Pdeployment -Djava.awt.headless=true -Dcontext=ci
/opt/wildfly-for-tests/wildfly-*.Final/bin/jboss-cli.sh --connect :shutdown
"""
deleteLockFile(lockFilePath)
}
}
}
stage('Quality Analysis') {
// quality analyse with our SonarQube service is performed only for PR against our main
// repository and for master branch
when {
expression {
env.BRANCH_NAME.startsWith('PR') &&
env.CHANGE_URL?.startsWith('https://github.com/Silverpeas')
}
}
steps {
script {
String jdkHome = sh(script: 'echo ${SONAR_JDK_HOME}', returnStdout: true).trim()
withSonarQubeEnv {
sh """
JAVA_HOME=$jdkHome mvn ${SONAR_MAVEN_GOAL} \\
-Dsonar.projectKey=Silverpeas_Silverpeas-Components \\
-Dsonar.organization=silverpeas \\
-Dsonar.pullrequest.branch=${env.BRANCH_NAME} \\
-Dsonar.pullrequest.key=${env.CHANGE_ID} \\
-Dsonar.pullrequest.base=master \\
-Dsonar.pullrequest.provider=github \\
-Dsonar.host.url=${SONAR_HOST_URL} \\
-Dsonar.token=${SONAR_AUTH_TOKEN} \\
-Dsonar.scanner.force-deprecated-java-version=true
"""
}
timeout(time: 30, unit: 'MINUTES') {
// Just in case something goes wrong, pipeline will be killed after a timeout
def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv
if (qg.status != 'OK' && qg.status != 'WARNING') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
}
}
}
post {
always {
deleteLockFile(lockFilePath)
step([$class : 'Mailer',
notifyEveryUnstableBuild: true,
sendToIndividuals : true])
}
}
}
def computeSnapshotVersion(pom) {
final String version = pom.version
final String defaultVersion = env.BRANCH_NAME == 'master' ? version :
env.BRANCH_NAME.toLowerCase().replaceAll('[# -]', '')
Matcher m = env.CHANGE_TITLE =~ /^(Bug #?\d+|Feature #?\d+).*$/
String snapshot = m.matches()
? m.group(1).toLowerCase().replaceAll(' #?', '')
: ''
if (snapshot.isEmpty()) {
m = env.CHANGE_TITLE =~ /^\[([^\[\]]+)].*$/
snapshot = m.matches()
? m.group(1).toLowerCase().replaceAll('[/><|:&?!;,*%$=}{#~\'"\\\\°)(\\[\\]]', '').trim().replaceAll('[ @]', '-')
: ''
}
return snapshot.isEmpty() ? defaultVersion : "${pom.properties['next.release']}-${snapshot}"
}
def getCoreDependencyVersion() {
copyArtifacts projectName: "Silverpeas_${silverpeasCore}_AutoDeploy", flatten: true
def lastBuild = readYaml file: 'build.yaml'
sh "rm build.yaml"
return lastBuild.version
}
def existsDependency(version, projectName) {
def exitCode = sh script: "test -d \$HOME/.m2/repository/org/silverpeas/${projectName}/${version}", returnStatus: true
return exitCode == 0
}
static def createLockFilePath(version, projectName) {
final String lockFilePath = "\$HOME/.m2/${version}_${projectName}_build.lock"
return lockFilePath
}
def createLockFile(version, projectName) {
final String lockFilePath = createLockFilePath(version, projectName)
sh "touch ${lockFilePath}"
return lockFilePath
}
def deleteLockFile(lockFilePath) {
if (isLockFileExisting(lockFilePath)) {
sh "rm -f ${lockFilePath}"
}
}
def isLockFileExisting(lockFilePath) {
if (lockFilePath?.trim()?.length() > 0) {
def exitCode = sh script: "test -e ${lockFilePath}", returnStatus: true
return exitCode == 0
}
return false
}
def waitForDependencyRunningBuildIfAny(version, projectName) {
final String dependencyLockFilePath = createLockFilePath(version, projectName)
timeout(time: 3, unit: 'HOURS') {
waitUntil {
return !isLockFileExisting(dependencyLockFilePath)
}
}
if (isLockFileExisting(dependencyLockFilePath)) {
error "After timeout dependency lock file ${dependencyLockFilePath} is yet existing!!!!"
}
}
def checkParentPOMVersion(version) {
def pom = readMavenPom()
int idx = pom.parent.version.indexOf('-SNAPSHOT')
if (idx > 0) {
String[] snapshot = version.split('-')
String parentVersion = pom.parent.version.substring(0, idx) + '-' + snapshot[snapshot.length - 1]
echo "Update parent POM to ${parentVersion}"
sh """
mvn versions:update-parent -DgenerateBackupPoms=false -DparentVersion="[${parentVersion}]"
"""
}
}
def getSilverpeasCoreProject(pom) {
String silverpeasCoreProject
switch (env.BRANCH_NAME) {
case 'master':
silverpeasCoreProject = 'Master'
break
case env.STABLE_BRANCH:
silverpeasCoreProject = 'Stable'
break
default:
Matcher branchMatcher = env.BRANCH_NAME =~ /\d+.\d+.x/
if (branchMatcher.matches()) {
// an old stable project
silverpeasCoreProject = env.BRANCH_NAME
} else {
// this is a PR
String version = pom.version
Matcher versionMatcher = version =~ /\d+.\d+.\d+-SNAPSHOT/
if (versionMatcher.matches()) {
if (version.startsWith(env.STABLE_BRANCH.replace('.x', ''))) {
silverpeasCoreProject = 'Stable'
} else {
silverpeasCoreProject = version.replaceFirst('.\\d+-SNAPSHOT', '.x')
}
} else {
silverpeasCoreProject = 'Master'
}
}
break
}
return silverpeasCoreProject
}