-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathJenkinsfile
183 lines (151 loc) · 5.02 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
properties([
buildDiscarder(logRotator(numToKeepStr: '30')),
])
// NOTE(Dan): We should definitely not attempt to run these builds on untrusted builds. The Kubernetes cluster we start
// is capable of doing quite a lot of damage. We should at the very least not run Kubernetes based tests on untrusted
// builds.
node {
sh label: '', script: 'java -version'
def jobName = "t"+currentBuild.startTimeInMillis
def compileFail = false
def testFail = false
def branchName = ""
if (env.BRANCH_NAME.startsWith("PR-")) {
branchName = env.CHANGE_BRANCH
} else {
branchName = env.BRANCH_NAME
}
echo branchName
echo (jobName)
echo "chmods"
//Make check on PR creator and specific branches. master, staging, PRs
stage('Checkout') {
checkout(
[
$class : 'GitSCM',
branches : [
[name: branchName]
],
doGenerateSubmoduleConfigurations: false,
extensions : [],
submoduleCfg : [],
userRemoteConfigs : [
[
credentialsId: 'github bot',
url : 'https://github.com/SDU-eScience/UCloud.git'
]
]
]
)
}
//Delete current environment if any
cleanDocker()
//Create new environment with providers installed
try {
sh script: 'DEBUG_COMMANDS=true ; ./launcher init --all-providers'
def log = getLog()
if (log.contains("BUILD FAILED")) {
sendAlert("""\
:warning: Launcher init on ${env.BRANCH_NAME} failed :warning:
""".stripIndent()
)
currentBuild.result = "FAILURE"
cleanDocker()
return
}
}
catch(Exception e) {
sendAlert("""\
:warning: Unknown exception in launcher init :warning:
""".stripIndent()
)
currentBuild.result = "FAILURE"
cleanDocker()
throw e
}
//Create Snapshot of DB to test purpose. Use "t"+timestamp for UNIQUE ID
sh script: """
./launcher snapshot ${jobName}
"""
//run test
try {
sh script: """
export UCLOUD_LAUNCHER=\$PWD/launcher
export UCLOUD_TEST_SNAPSHOT=${jobName}
cd integration-test
./gradlew integrationtest
"""
} catch(Throwable e) {
def log = getLog()
def startIndex = log.indexOf("FAILURE: Build failed with an exception")
def endIndex = log.indexOf("* Try:")
if (startIndex == -1) {
startIndex = 0
}
if (endIndex == -1) {
endIndex = log.length()-1
}
sendAlert("""\
:warning: Integration Test on ${env.BRANCH_NAME} failed :warning:
${log.substring(startIndex, endIndex)}
""".stripIndent()
)
if(log.substring(startIndex, endIndex).contains("Compilation error")) {
compileFail = true
} else {
testFail = true
}
} finally {
junit '**/build/test-results/**/*.xml'
env.WORKSPACE = pwd()
def workspace = readFile "${env.WORKSPACE}/.compose/current.txt"
sh script: """
mkdir -p ./tmp
docker cp ${workspace}-backend-1:/tmp/service.log ./tmp/service.log
docker cp ${workspace}-backend-1:/var/log ./tmp/
"""
archiveArtifacts artifacts: 'tmp/service.log', allowEmptyArchive: true
archiveArtifacts artifacts: 'tmp/log/ucloud/*.log', allowEmptyArchive: true
cleanDocker()
if(compileFail) {
currentBuild.result = "FAILURE"
}
if(testFail) {
currentBuild.result = "UNSTABLE"
}
}
}
def cleanDocker() {
sh script: """
docker rm -f \$(docker ps -qa) || true
docker volume rm -f \$(docker volume ls -q) || true
docker network rm \$(docker network ls -q) || true
docker rm -f \$(docker ps -qa) || true
docker volume rm -f \$(docker volume ls -q) || true
docker network rm \$(docker network ls -q) || true
docker volume prune || true
docker network prune || true
docker run --rm -v \$PWD:/mnt/folder ubuntu:22.04 bash -c 'rm -rf /mnt/folder/.compose/*'
rm -rf ./tmp
"""
}
String getLog() {
def logArray = currentBuild.rawBuild.getLog(50)
def log = ""
for (String s : logArray)
{
log += s + " ";
}
return log
}
String runBuild(String item) {
def loaded = load(item)
return loaded.initialize()
}
def sendAlert(String alertMessage) {
withCredentials(
[string(credentialsId: "slackToken", variable: "slackToken")]
) {
slackSend(channel: "devalerts", message: alertMessage, tokenCredentialId: 'slackToken')
}
}