-
Notifications
You must be signed in to change notification settings - Fork 6
/
Jenkinsfile.old
201 lines (192 loc) · 7.35 KB
/
Jenkinsfile.old
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
import groovy.json.JsonBuilder
import groovy.json.JsonSlurperClassic
env.UPLOAD_DIR = "/mnt/lc"
env.BUILDER_CONTAINER = "deforce/lc-ubuntu-builder"
env.HOME = "."
def secrets_file = "http://repo.intra.czt.lv/lalkachat/secrets.yaml"
def UploadPath = "[email protected]:/usr/local/nginx/html/czt.lv/lalkachat/"
def doRunTests = false
def stage = { String stageName, Closure body ->
// It's stage that prints its name
stage(stageName) {
echo "Stage: ${stageName}"
body.call()
}
}
def buildThemes() {
// Creates themes.json
sh 'python src/jenkins/get_themes.py'
def ThemesJson = readFile('themes.json')
def ThemesList = new JsonSlurperClassic().parseText(ThemesJson)
echo "${ThemesList}"
for (def Theme : ThemesList) {
//sh "/bin/sh src/jenkins/test_theme.sh ${Theme}"
sh "/bin/sh src/jenkins/build_theme.sh ${Theme}"
}
//junit 'results/javascript-tests/*.xml'
}
def runTests(folder, name, skip) {
sh "python src/jenkins/get_folder_tests.py ${folder} ${name}"
def TestsList
try {
def TestJson = readFile("${name}_tests.json")
TestsList = new JsonSlurperClassic().parseText(TestJson)
}
catch (exc) {
echo "No json file, exiting"
return
}
def TestResults = [:]
for (def Test : TestsList) {
echo "Running ${Test} test"
def result = false
try {
def Test_Name = Test.split('/').last().split("\\.").first()
if(Test.endsWith('.py')) {
sh "set -o pipefail && python ${Test} 2>&1 | tee results/${name}_${Test_Name}_results.txt"
} else {
sh "set -o pipefail && /bin/bash ${Test} 2>&1 | tee results/${name}_${Test_Name}_results.txt"
}
result = true
} catch(exc) {
if(!skip) {
echo "Exception: $exc"
error("Test didn't pass")
}
}
finally {
TestResults[Test] = result
}
}
writeFile(file: "results/${name}_test.txt", text: new JsonBuilder(TestResults).toPrettyString())
}
def buildDockerImage(archName, image, buildName) {
sh "docker build -t ${buildName} -f docker/dockerfiles/${archName}/${image}/Dockerfile ."
}
@NonCPS
static def mapToList(depmap) {
def dlist = []
for (def entry2 in depmap) {
dlist.add(new java.util.AbstractMap.SimpleImmutableEntry(entry2.key, entry2.value))
}
dlist
}
node('docker-host') {
stage('Checkout') {
cleanWs()
checkout scm
sh 'mkdir -p results'
sh 'rsync -avz src/jenkins/root/ ./'
sh 'echo ${BRANCH_NAME},${BUILD_NUMBER} > default_branch'
// This comment is needed until I refactor Jenkinsfile to support proper error throwing
// def deps = 'asdas/asdasddas/asdasda/asdasd'.split('/').last().split("\\.").first()
}
def stable = true
env.PYTHONPATH = pwd()
try {
def containersToBuild = []
stage('Prepare Docker containers') {
sh 'python src/scripts/docker_build.py'
sh "wget ${secrets_file}"
def ContainerFile = readFile('docker/build_order.json')
def ContainerMap = mapToList(new JsonSlurperClassic().parseText(ContainerFile))
for (architecture in ContainerMap) {
def archName = architecture.getKey()
def archData = architecture.getValue()
echo "Running builds for ${archName}"
stage(archName) {
for (image in archData) {
echo "Building ${image} from ${archName}"
def buildName = "deforce/lc-${archName}-${image}"
buildDockerImage(archName, image, buildName)
if (image.equals('testing')) {
containersToBuild.add(buildName)
}
}
}
}
}
stage('PreBuild') {
def container = containersToBuild[0]
stage(container) {
echo "Running Build for ${container}"
def docker_image = docker.image(container)
docker_image.inside {
stage('Themes') {
buildThemes()
}
stage('Configuration') {
sh '/bin/sh src/jenkins/prep_config.sh'
}
}
}
}
stage('Testing') {
def lintRun = false
if(doRunTests) {
for (container in containersToBuild) {
stage(container) {
echo "Running Build for ${container}"
def docker_image = docker.image(container)
docker_image.inside {
try {
stage('Run Chat') {
sh '/bin/sh src/jenkins/run_chat.sh'
}
stage('Run Tests') {
stage('Chat Tests') {
runTests('src/jenkins/chat_tests', 'chat', false)
}
stage('Module Tests') {
echo 'Module Tests'
}
}
stage('Lint Tests') {
try {
if(!lintRun) {
runTests('src/jenkins/lint_tests', 'lint', true)
lintRun = true
}
} catch(exc) {
stable = false
}
}
} catch(exc) {
echo "${exc}"
throw(exc)
}
finally {
echo "Chat logs"
sh 'cat chat.log'
try {
sh "python src/jenkins/tests_to_xml.py ${container}"
junit 'results/chat_tests.xml'
archive 'results/**'
} catch (exc) {
echo "Got Exception, skipping"
}
}
}
}
}
}
}
stage('Publish') {
env.ZIP_NAME = "LalkaChat-${BRANCH_NAME}-${BUILD_NUMBER}"
sh "sh src/jenkins/publish_chat.sh"
sh """
tar -zcvf themes-${BRANCH_NAME.replace('/', '-')}.tar.gz http/
cp ./themes-${BRANCH_NAME.replace('/', '-')}.tar.gz ${env.UPLOAD_DIR}/
""".stripIndent()
}
}
finally {
stage('Cleanup') {
if(!stable) {
currentBuild.result = 'UNSTABLE'
}
// sh 'docker rm $(docker ps -aq) || true'
// sh 'docker rmi -f $(docker images -a | grep \'^<none>\' | awk \'{print \$3}\') || true'
}
}
}