-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
149 lines (120 loc) · 5.15 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
#!/usr/bin/env groovy
openshift.withCluster() { // Use "default" cluster or fallback to OpenShift cluster detection
echo "Hello from the project running Jenkins: ${openshift.project()}"
//Create template with maven settings.xml, so we have credentials for nexus
podTemplate(
// If the project uses Java 11 instead of Java 17, change to
// inheritFrom: 'maven',
// and update the envVars section
inheritFrom: 'kb-jenkins-agent-java',
cloud: 'openshift', //cloud must be openshift
envVars: [
// If the project uses Java 11 instead of Java 17, remove the lines with
// USE_JAVA_VERSION and MAVEN_SKIP_RC
// and update inheritsFrom a few lines above
envVar(key: 'USE_JAVA_VERSION', value: 'java-17'),
envVar(key: 'MAVEN_SKIP_RC', value: 'true'),
//This fixes the error with en_US.utf8 not being found
envVar(key:"LC_ALL", value:"C.utf8")
],
volumes: [ //mount the settings.xml
secretVolume(mountPath: '/etc/m2', secretName: 'maven-settings')
]) {
//Stages outside a node declaration runs on the jenkins host
String projectName = encodeName("${JOB_NAME}")
echo "name=${projectName}"
try {
//GO to a node with maven and settings.xml
node(POD_LABEL) {
//Do not use concurrent builds
properties([disableConcurrentBuilds()])
def mvnCmd = "mvn -s /etc/m2/settings.xml --batch-mode"
stage('checkout') {
checkout scm
}
stage('Mvn clean package') {
sh "${mvnCmd} -PallTests clean package"
}
stage('Analyze build results') {
recordIssues aggregatingResults: true,
tools: [java(),
javaDoc(),
mavenConsole(),
taskScanner(highTags:'FIXME', normalTags:'TODO', includePattern: '**/*.java', excludePattern: 'target/**/*')]
}
stage('Create test project') {
recreateProject(projectName)
openshift.withProject(projectName) {
stage("Create build and deploy application") {
openshift.newBuild("--strategy source", "--binary", "-i kb-infra/kb-s2i-tomcat90", "--name ds-discover")
openshift.startBuild("ds-discover", "--from-dir=.", "--follow")
openshift.newApp("ds-discover", "-e BUILD_NUMBER=latest")
openshift.create("route", "edge", "--service=ds-discover")
}
}
}
stage('Push to Nexus (if Master)') {
sh 'env'
echo "Branch name ${env.BRANCH_NAME}"
if (env.BRANCH_NAME == 'master') {
sh "${mvnCmd} clean deploy -DskipTests=true"
} else {
echo "Branch ${env.BRANCH_NAME} is not master, so no mvn deploy"
}
}
stage('Cleanup') {
openshift.selector("project/${projectName}").delete()
}
}
} catch (e) {
currentBuild.result = 'FAILURE'
throw e
}
}
}
private void recreateProject(String projectName) {
echo "Delete the project ${projectName}, ignore errors if the project does not exist"
try {
openshift.selector("project/${projectName}").delete()
openshift.selector("project/${projectName}").watch {
echo "Waiting for the project ${projectName} to be deleted"
return it.count() == 0
}
} catch (e) {
}
//
// //Wait for the project to be gone
// sh "until ! oc get project ${projectName}; do date;sleep 2; done; exit 0"
echo "Create the project ${projectName}"
openshift.newProject(projectName)
}
/**
* Encode the jobname as a valid openshift project name
* @param jobName the name of the job
* @return the jobname as a valid openshift project name
*/
private static String encodeName(groovy.lang.GString jobName) {
def jobTokens = jobName.tokenize("/")
def org = jobTokens[0]
if(org.contains('-')) {
org = org.tokenize("-").collect{it.take(1)}.join("")
} else {
org = org.take(3)
}
// Repository have a very long name, lets shorten it further
def repo = jobTokens[1]
if(repo.contains('-')) {
repo = repo.tokenize("-").collect{it.take(1)}.join("")
} else if(repo.contains('_')) {
repo = repo.tokenize("_").collect{it.take(1)}.join("")
} else {
repo = repo.take(3)
}
def name = ([org, repo] + jobTokens.drop(2)).join("-")
.replaceAll("\\s", "-")
.replaceAll("_", "-")
.replace("/", '-')
.replaceAll("^openshift-", "")
.toLowerCase()
return name
}