-
Notifications
You must be signed in to change notification settings - Fork 73
/
Jenkinsfile.groovy
187 lines (171 loc) · 6.91 KB
/
Jenkinsfile.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
def notifySlack(String buildStatus = 'STARTED') {
// Build status of null means success.
buildStatus = buildStatus ?: 'SUCCESS'
def color
if (buildStatus == 'STARTED') {
color = '#D4DADF'
} else if (buildStatus == 'SUCCESS') {
color = '#BDFFC3'
} else if (buildStatus == 'UNSTABLE') {
color = '#FFFE89'
} else {
color = '#FF9FA1'
}
def msg = "${buildStatus}: `${env.JOB_NAME}` #${env.BUILD_NUMBER}: ${env.GIT_COMMIT}\n${env.BUILD_URL}"
slackSend(color: color, channel: '#status-k8s', message: msg)
}
def fetchParamsFromGitLog() {
def myParams = [:];
// Copy configured params
for (entry in params) {
myParams[entry.key] = entry.value;
}
// Is this a LONG test?
if ("${env.JOB_NAME}" == "kube-arangodb-long") {
myParams["LONG"] = true;
myParams["KUBECONFIGS"] = "gke-jenkins-1";
}
// Fetch params configured in git commit messages
// Syntax: [ci OPT=value]
// Example: [ci TESTOPTIONS="-test.run ^TestSimpleSingle$"]
def options = sh(returnStdout: true, script: "git log --reverse remotes/origin/master..HEAD | grep -o \'\\[ci[^\\[]*\\]\' | sed -E \'s/\\[ci (.*)\\]/\\1/\'").trim().split("\n")
for (opt in options) {
def idx = opt.indexOf('=');
if (idx > 0) {
def key = opt.substring(0, idx);
def value = opt.substring(idx+1).replaceAll('^\"|\"$', '');
myParams[key] = value;
//println("Overwriting myParams.${key} with ${value}");
}
}
// Show params in log
for (entry in myParams) {
println("Using myParams.${entry.key} with ${entry.value}");
}
return myParams;
}
def kubeConfigRoot = "/home/jenkins/.kube"
def buildBuildSteps(Map myParams) {
return {
timestamps {
timeout(time: 15) {
withEnv([
"DEPLOYMENTNAMESPACE=${myParams.TESTNAMESPACE}-${env.GIT_COMMIT}",
"DOCKERNAMESPACE=${myParams.DOCKERNAMESPACE}",
"IMAGETAG=jenkins-test",
"LONG=${myParams.LONG ? 1 : 0}",
"TESTOPTIONS=${myParams.TESTOPTIONS}",
]) {
sh "make clean"
sh "make"
sh "make run-unit-tests"
sh "make docker-test"
}
}
}
}
}
def buildTestSteps(Map myParams, String kubeConfigRoot, String kubeconfig) {
return {
timestamps {
timeout(time: myParams.LONG ? 180 : 30) {
withCredentials([
string(credentialsId: 'ENTERPRISEIMAGE', variable: 'DEFAULTENTERPRISEIMAGE'),
string(credentialsId: 'ENTERPRISELICENSE', variable: 'DEFAULTENTERPRISELICENSE'),
]) {
withEnv([
"CLEANDEPLOYMENTS=1",
"DEPLOYMENTNAMESPACE=${myParams.TESTNAMESPACE}-${env.GIT_COMMIT}",
"DOCKERNAMESPACE=${myParams.DOCKERNAMESPACE}",
"ENTERPRISEIMAGE=${myParams.ENTERPRISEIMAGE}",
"ENTERPRISELICENSE=${myParams.ENTERPRISELICENSE}",
"ARANGODIMAGE=${myParams.ARANGODIMAGE}",
"IMAGETAG=jenkins-test",
"KUBECONFIG=${kubeConfigRoot}/${kubeconfig}",
"LONG=${myParams.LONG ? 1 : 0}",
"TESTOPTIONS=${myParams.TESTOPTIONS}",
]) {
sh "make run-tests"
}
}
}
}
}
}
def buildCleanupSteps(Map myParams, String kubeConfigRoot, String kubeconfig) {
return {
timestamps {
timeout(time: 15) {
withEnv([
"DEPLOYMENTNAMESPACE=${myParams.TESTNAMESPACE}-${env.GIT_COMMIT}",
"DOCKERNAMESPACE=${myParams.DOCKERNAMESPACE}",
"KUBECONFIG=${kubeConfigRoot}/${kubeconfig}",
]) {
sh "./scripts/collect_logs.sh ${env.DEPLOYMENTNAMESPACE} ${kubeconfig}"
archive includes: 'logs/*'
sh "make cleanup-tests"
}
}
}
}
}
pipeline {
options {
buildDiscarder(logRotator(daysToKeepStr: '7', numToKeepStr: '10'))
lock resource: 'kube-arangodb'
}
agent any
parameters {
booleanParam(name: 'LONG', defaultValue: false, description: 'Execute long running tests')
string(name: 'DOCKERNAMESPACE', defaultValue: 'arangodb', description: 'DOCKERNAMESPACE sets the docker registry namespace in which the operator docker image will be pushed', )
string(name: 'KUBECONFIGS', defaultValue: 'kube-ams1,scw-183a3b', description: 'KUBECONFIGS is a comma separated list of Kubernetes configuration files (relative to /home/jenkins/.kube) on which the tests are run', )
string(name: 'TESTNAMESPACE', defaultValue: 'jenkins', description: 'TESTNAMESPACE sets the kubernetes namespace to ru tests in (this must be short!!)', )
string(name: 'ENTERPRISEIMAGE', defaultValue: '', description: 'ENTERPRISEIMAGE sets the docker image used for enterprise tests', )
string(name: 'ARANGODIMAGE', defaultValue: '', description: 'ARANGODIMAGE sets the docker image used for tests (except enterprise and update tests)', )
string(name: 'ENTERPRISELICENSE', defaultValue: '', description: 'ENTERPRISELICENSE sets the enterprise license key for enterprise tests', )
string(name: 'TESTOPTIONS', defaultValue: '', description: 'TESTOPTIONS is used to pass additional test options to the integration test', )
}
stages {
stage('Build') {
steps {
script {
def myParams = fetchParamsFromGitLog();
def buildSteps = buildBuildSteps(myParams);
buildSteps();
}
}
}
stage('Test') {
steps {
script {
def myParams = fetchParamsFromGitLog();
def configs = "${myParams.KUBECONFIGS}".split(",")
def testTasks = [:]
for (kubeconfig in configs) {
testTasks["${kubeconfig}"] = buildTestSteps(myParams, kubeConfigRoot, kubeconfig)
}
parallel testTasks
}
}
}
}
post {
always {
script {
def myParams = fetchParamsFromGitLog();
def configs = "${myParams['KUBECONFIGS']}".split(",")
def cleanupTasks = [:]
for (kubeconfig in configs) {
cleanupTasks["${kubeconfig}"] = buildCleanupSteps(myParams, kubeConfigRoot, kubeconfig)
}
parallel cleanupTasks
}
}
failure {
notifySlack('FAILURE')
}
success {
notifySlack('SUCCESS')
}
}
}