forked from strimzi/strimzi-kafka-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkins.groovy
145 lines (127 loc) · 5.22 KB
/
jenkins.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
/**
* Download oc origin
*/
def downloadOcOrigin() {
def originDownloadUrl = "https://github.com/openshift/origin/releases/download/v3.11.0/openshift-origin-client-tools-v3.11.0-0cbc58b-linux-64bit.tar.gz"
sh(script: "rm -rf ~/.kube")
sh(script: "mkdir -p /tmp/openshift")
timeout(time: 5, unit: 'MINUTES') {
status = sh(
script: "wget $originDownloadUrl -O openshift.tar.gz -q",
returnStatus: true
)
}
//////////////////////////////////////////////////
sh(script: "tar xzf openshift.tar.gz -C /tmp/openshift --strip-components 1")
sh(script: "sudo cp /tmp/openshift/oc /usr/bin/oc")
sh(script: "sudo rm -rf /tmp/openshift/")
sh(script: "sudo rm -rf openshift.tar.gz")
}
/**
* Prepare and start origin environment.
*/
def startOrigin(String username = "developer", String password = "developer") {
downloadOcOrigin()
timeout(time: 15, unit: 'MINUTES') {
status = sh(
script: "oc cluster up --public-hostname=\$(hostname -I | awk '{print \$1}') --base-dir ${ORIGIN_BASE_DIR} --enable=*,service-catalog,web-console --insecure-skip-tls-verify=true",
returnStatus: true
)
}
def url = sh(script: "echo \"\$(hostname -I | awk '{print \$1}')\"", returnStdout: true).trim()
createUserOrigin("${username}", "https://${url}:8443")
login("https://${url}:8443", "${username}", "${password}")
fixPVOnOrigin()
}
/**
* Login kubernetes user
* @param url url to cluster
* @param user username
* @param pass password
*/
def login(String url, String user, String pass) {
for (i = 0; i < 10; i++) {
println("[INFO] Logging in on OCP ${url} as ${user}")
def status = sh(script: "oc login -u \"${user}\" -p \"${pass}\" --insecure-skip-tls-verify=true ${url}", returnStatus: true)
if (status != 0) {
echo "[WARN] Login failed, waiting 1 minute before next try"
sh(script: "sleep 60")
} else {
break
}
}
}
/**
* create user in cluster and set him as cluster-admin
* @param username
* @param url
*/
def createUserOrigin(String username, String url) {
println("Create user in on OCP ${username}")
sh(script: "oc login -u system:admin --insecure-skip-tls-verify=true --config=${KUBE_CONFIG_PATH} ${url}")
sh(script: "oc adm policy add-cluster-role-to-user cluster-admin ${username} --rolebinding-name=cluster-admin --config=${KUBE_CONFIG_PATH}")
sh(script: "oc label node localhost rack-key=zone --config=${KUBE_CONFIG_PATH}")
}
def fixPVOnOrigin() {
sh(script: "oc get pv")
sh(script: "oc adm policy add-scc-to-group hostmount-anyuid system:serviceaccounts")
}
/**
* Function for teardown the test cluster.
*/
def teardownEnvironment(String workspace) {
def status = sh(
script: "oc cluster down",
returnStatus: true
)
if (status != 0) {
echo "OpenShift failed to stop"
}
sh "for i in \$(mount | grep openshift | awk '{ print \$3}'); do sudo umount \"\$i\"; done && sudo rm -rf ${workspace}/origin"
sh "sudo rm -rf ${workspace}/origin/"
}
def clearImages() {
sh "docker rmi -f \$(docker images -q) 2>/dev/null || echo 'No more images to remove.'"
}
def buildStrimziImages() {
sh "make docker_build"
sh "make docker_tag"
}
def runSystemTests(String workspace, String testCases, String testProfile) {
withMaven(mavenOpts: '-Djansi.force=true') {
sh "mvn -f ${workspace}/systemtest/pom.xml -P all verify -Dgroups=${testProfile} -Dit.test=${testCases} -Djava.net.preferIPv4Stack=true -DtrimStackTrace=false -Dstyle.color=always --no-transfer-progress"
}
}
def postAction(String artifactDir, String prID, String prAuthor, String prTitle, String prUrl, String buildUrl, String workspace, String address) {
def status = currentBuild.result
//store test results from build and system tests
junit testResults: '**/TEST-*.xml', allowEmptyResults: true
//archive test results and openshift logs
archive '**/TEST-*.xml'
try {
archive "${artifactDir}/**"
} catch(all) {
echo "Archive failed"
} finally {
echo "Artifacts are stored"
}
if (status == null) {
currentBuild.result = 'SUCCESS'
sendMail(address, "succeeded", prID, prAuthor, prTitle, prUrl, buildUrl)
}
teardownEnvironment(workspace)
}
def sendMail(String address, String status, String prID, String prAuthor, String prTitle, String prUrl, String buildUrl) {
mail to:"${address}", subject:"Build of Strimzi PR#${prID} by ${prAuthor} - '${prTitle}' has ${status}", body:"PR link: ${prUrl}\nBuild link: ${buildUrl}"
}
def postGithubPrComment(def file) {
echo "Posting github comment"
echo "Going to run curl command"
withCredentials([string(credentialsId: 'strimzi-ci-github-token', variable: 'GITHUB_TOKEN')]) {
sh "curl -v -H \"Authorization: token ${GITHUB_TOKEN}\" -X POST -H \"Content-type: application/json\" -d \"@${file}\" \"https://api.github.com/repos/Strimzi/strimzi-kafka-operator/issues/${ghprbPullId}/comments\" > out.log 2> out.err"
def output=readFile("out.log").trim()
def output_err=readFile("out.err").trim()
echo "curl output=$output output_err=$output_err"
}
}
return this