Jenkinsfile2 17.7 KB
Newer Older
L
liuyq-617 已提交
1 2 3 4 5 6 7
import hudson.model.Result
import hudson.model.*;
import jenkins.model.CauseOfInterruption
node {
}

def abortPreviousBuilds() {
T
tangfangzhi 已提交
8 9 10 11
    def currentJobName = env.JOB_NAME
    def currentBuildNumber = env.BUILD_NUMBER.toInteger()
    def jobs = Jenkins.instance.getItemByFullName(currentJobName)
    def builds = jobs.getBuilds()
L
liuyq-617 已提交
12

T
tangfangzhi 已提交
13 14 15 16
    for (build in builds) {
        if (!build.isBuilding()) {
            continue;
        }
L
liuyq-617 已提交
17

T
tangfangzhi 已提交
18 19 20
        if (currentBuildNumber == build.getNumber().toInteger()) {
            continue;
        }
L
liuyq-617 已提交
21

T
tangfangzhi 已提交
22 23
        build.doKill()    //doTerm(),doKill(),doTerm()
    }
L
liuyq-617 已提交
24 25 26 27
}
//  abort previous build
abortPreviousBuilds()
def abort_previous(){
T
tangfangzhi 已提交
28 29 30
    def buildNumber = env.BUILD_NUMBER as int
    if (buildNumber > 1) milestone(buildNumber - 1)
    milestone(buildNumber)
L
liuyq-617 已提交
31 32
}
def pre_test(){
33 34 35
    sh '''
        hostname
        date
36
        env
37 38 39 40 41 42 43
    '''
    sh '''
        cd ${WK}
        git reset --hard
        cd ${WKC}
        git reset --hard
    '''
L
liuyq-617 已提交
44
    script {
T
tangfangzhi 已提交
45 46
        if (env.CHANGE_TARGET == 'master') {
            sh '''
T
tangfangzhi 已提交
47 48
                cd ${WK}
                git checkout master
T
tangfangzhi 已提交
49 50 51 52 53
                cd ${WKC}
                git checkout master
            '''
        } else if(env.CHANGE_TARGET == '2.0') {
            sh '''
T
tangfangzhi 已提交
54 55
                cd ${WK}
                git checkout 2.0
T
tangfangzhi 已提交
56 57 58 59 60
                cd ${WKC}
                git checkout 2.0
            '''
        } else if(env.CHANGE_TARGET == '3.0') {
            sh '''
T
tangfangzhi 已提交
61 62
                cd ${WK}
                git checkout 3.0
T
tangfangzhi 已提交
63 64 65 66 67
                cd ${WKC}
                git checkout 3.0
            '''
        } else {
            sh '''
T
tangfangzhi 已提交
68 69
                cd ${WK}
                git checkout develop
T
tangfangzhi 已提交
70 71 72
                cd ${WKC}
                git checkout develop
            '''
L
liuyq-617 已提交
73
        }
T
tangfangzhi 已提交
74
    }
T
tangfangzhi 已提交
75 76 77
    if (env.CHANGE_URL =~ /\/TDengine\//) {
        sh '''
            cd ${WKC}
T
tangfangzhi 已提交
78
            git remote prune origin
T
tangfangzhi 已提交
79
            git pull >/dev/null
80
            git log -5
81
            echo "`date "+%Y%m%d-%H%M%S"` ${JOB_NAME}:${BRANCH_NAME}:${BUILD_ID}:${CHANGE_TARGET}" >>${WKDIR}/jenkins.log
T
tangfangzhi 已提交
82
            echo "CHANGE_BRANCH:${CHANGE_BRANCH}" >>${WKDIR}/jenkins.log
83
            echo "community log: `git log -5`" >>${WKDIR}/jenkins.log
T
tangfangzhi 已提交
84 85
            git fetch origin +refs/pull/${CHANGE_ID}/merge
            git checkout -qf FETCH_HEAD
86
            git log -5
87
            echo "community log merged: `git log -5`" >>${WKDIR}/jenkins.log
T
tangfangzhi 已提交
88 89
            cd ${WK}
            git pull >/dev/null
90
            git log -5
91
            echo "tdinternal log: `git log -5`" >>${WKDIR}/jenkins.log
T
tangfangzhi 已提交
92 93 94 95 96
        '''
    } else if (env.CHANGE_URL =~ /\/TDinternal\//) {
        sh '''
            cd ${WK}
            git pull >/dev/null
97
            git log -5
98
            echo "`date "+%Y%m%d-%H%M%S"` ${JOB_NAME}:${BRANCH_NAME}:${BUILD_ID}:${CHANGE_TARGET}" >>${WKDIR}/jenkins.log
T
tangfangzhi 已提交
99
            echo "CHANGE_BRANCH:${CHANGE_BRANCH}" >>${WKDIR}/jenkins.log
100
            echo "tdinternal log: `git log -5`" >>${WKDIR}/jenkins.log
T
tangfangzhi 已提交
101 102
            git fetch origin +refs/pull/${CHANGE_ID}/merge
            git checkout -qf FETCH_HEAD
103
            git log -5
104
            echo "tdinternal log merged: `git log -5`" >>${WKDIR}/jenkins.log
T
tangfangzhi 已提交
105
            cd ${WKC}
T
tangfangzhi 已提交
106
            git remote prune origin
T
tangfangzhi 已提交
107
            git pull >/dev/null
108
            git log -5
109
            echo "community log: `git log -5`" >>${WKDIR}/jenkins.log
T
tangfangzhi 已提交
110 111 112 113 114 115
        '''
    } else {
        sh '''
            echo "unmatched reposiotry ${CHANGE_URL}"
        '''
    }
T
tangfangzhi 已提交
116
    sh '''
L
liuyq-617 已提交
117
        cd ${WKC}
T
tangfangzhi 已提交
118 119 120 121 122 123
        git submodule update --init --recursive
    '''
    sh '''
        cd ${WKPY}
        git reset --hard
        git pull
124
        git log -5
125 126
        echo "python connector log: `git log -5`" >>${WKDIR}/jenkins.log
        echo >>${WKDIR}/jenkins.log
L
liuyq-617 已提交
127
    '''
T
tangfangzhi 已提交
128 129 130 131 132
    return 1
}
def pre_test_win(){
    bat '''
        hostname
T
tangfangzhi 已提交
133 134
        ipconfig
        set
T
tangfangzhi 已提交
135 136
        date /t
        time /t
T
tangfangzhi 已提交
137
        rd /s /Q %WIN_INTERNAL_ROOT%\\debug || exit 0
T
tangfangzhi 已提交
138 139
    '''
    bat '''
T
tangfangzhi 已提交
140
        cd %WIN_INTERNAL_ROOT%
141
        git reset --hard
T
tangfangzhi 已提交
142 143
    '''
    bat '''
T
tangfangzhi 已提交
144
        cd %WIN_COMMUNITY_ROOT%
T
tangfangzhi 已提交
145 146 147 148 149
        git reset --hard
    '''
    script {
        if (env.CHANGE_TARGET == 'master') {
            bat '''
T
tangfangzhi 已提交
150
                cd %WIN_INTERNAL_ROOT%
151
                git checkout master
T
tangfangzhi 已提交
152 153
            '''
            bat '''
T
tangfangzhi 已提交
154
                cd %WIN_COMMUNITY_ROOT%
T
tangfangzhi 已提交
155 156 157 158
                git checkout master
            '''
        } else if(env.CHANGE_TARGET == '2.0') {
            bat '''
T
tangfangzhi 已提交
159
                cd %WIN_INTERNAL_ROOT%
160
                git checkout 2.0
T
tangfangzhi 已提交
161 162
            '''
            bat '''
T
tangfangzhi 已提交
163
                cd %WIN_COMMUNITY_ROOT%
T
tangfangzhi 已提交
164 165 166 167
                git checkout 2.0
            '''
        } else if(env.CHANGE_TARGET == '3.0') {
            bat '''
T
tangfangzhi 已提交
168
                cd %WIN_INTERNAL_ROOT%
169
                git checkout 3.0
T
tangfangzhi 已提交
170 171
            '''
            bat '''
T
tangfangzhi 已提交
172
                cd %WIN_COMMUNITY_ROOT%
T
tangfangzhi 已提交
173 174 175 176
                git checkout 3.0
            '''
        } else {
            bat '''
T
tangfangzhi 已提交
177
                cd %WIN_INTERNAL_ROOT%
178
                git checkout develop
T
tangfangzhi 已提交
179 180
            '''
            bat '''
T
tangfangzhi 已提交
181
                cd %WIN_COMMUNITY_ROOT%
T
tangfangzhi 已提交
182 183 184 185
                git checkout develop
            '''
        }
    }
186 187 188
    script {
        if (env.CHANGE_URL =~ /\/TDengine\//) {
            bat '''
T
tangfangzhi 已提交
189
                cd %WIN_INTERNAL_ROOT%
190
                git pull
T
tangfangzhi 已提交
191 192
            '''
            bat '''
T
tangfangzhi 已提交
193 194
                cd %WIN_COMMUNITY_ROOT%
                git remote prune origin
195
                git pull
T
tangfangzhi 已提交
196 197
            '''
            bat '''
T
tangfangzhi 已提交
198
                cd %WIN_COMMUNITY_ROOT%
T
tangfangzhi 已提交
199 200 201
                git fetch origin +refs/pull/%CHANGE_ID%/merge
            '''
            bat '''
T
tangfangzhi 已提交
202
                cd %WIN_COMMUNITY_ROOT%
203 204 205 206
                git checkout -qf FETCH_HEAD
            '''
        } else if (env.CHANGE_URL =~ /\/TDinternal\//) {
            bat '''
T
tangfangzhi 已提交
207
                cd %WIN_INTERNAL_ROOT%
208
                git pull
T
tangfangzhi 已提交
209 210
            '''
            bat '''
T
tangfangzhi 已提交
211
                cd %WIN_INTERNAL_ROOT%
T
tangfangzhi 已提交
212 213 214
                git fetch origin +refs/pull/%CHANGE_ID%/merge
            '''
            bat '''
T
tangfangzhi 已提交
215
                cd %WIN_INTERNAL_ROOT%
216
                git checkout -qf FETCH_HEAD
T
tangfangzhi 已提交
217 218
            '''
            bat '''
T
tangfangzhi 已提交
219 220
                cd %WIN_COMMUNITY_ROOT%
                git remote prune origin
221 222 223
                git pull
            '''
        } else {
T
tangfangzhi 已提交
224 225
            bat '''
                echo "unmatched reposiotry %CHANGE_URL%"
226 227 228
            '''
        }
    }
T
tangfangzhi 已提交
229
    bat '''
T
tangfangzhi 已提交
230
        cd %WIN_INTERNAL_ROOT%
T
tangfangzhi 已提交
231
        git branch
T
tangfangzhi 已提交
232 233 234
        git log -5
    '''
    bat '''
T
tangfangzhi 已提交
235
        cd %WIN_COMMUNITY_ROOT%
T
tangfangzhi 已提交
236
        git branch
T
tangfangzhi 已提交
237 238
        git log -5
    '''
T
tangfangzhi 已提交
239
    bat '''
T
tangfangzhi 已提交
240
        cd %WIN_COMMUNITY_ROOT%
241
        git submodule update --init --recursive
T
tangfangzhi 已提交
242
    '''
243
    bat '''
T
tangfangzhi 已提交
244
        cd %WIN_CONNECTOR_ROOT%
245 246 247 248 249
        git branch
        git reset --hard
        git pull
    '''
    bat '''
T
tangfangzhi 已提交
250
        cd %WIN_CONNECTOR_ROOT%
251 252
        git log -5
    '''
T
tangfangzhi 已提交
253 254 255 256 257
}
def pre_test_build_win() {
    bat '''
        echo "building ..."
        time /t
T
tangfangzhi 已提交
258
        cd %WIN_INTERNAL_ROOT%
T
tangfangzhi 已提交
259 260
        mkdir debug
        cd debug
T
tangfangzhi 已提交
261
        time /t
T
tangfangzhi 已提交
262 263
        call "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvarsall.bat" x64
        set CL=/MP8
T
tangfangzhi 已提交
264 265
        echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> cmake"
        time /t
T
tangfangzhi 已提交
266
        cmake .. -G "NMake Makefiles JOM" || exit 7
T
tangfangzhi 已提交
267
        echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> jom -j 6"
268
        time /t
T
tangfangzhi 已提交
269
        jom -j 6 || exit 8
T
tangfangzhi 已提交
270
        time /t
T
tangfangzhi 已提交
271
    '''
272
    bat '''
T
tangfangzhi 已提交
273
        cd %WIN_CONNECTOR_ROOT%
274
        python -m pip install .
T
tangfangzhi 已提交
275
        xcopy /e/y/i/f %WIN_INTERNAL_ROOT%\\debug\\build\\lib\\taos.dll C:\\Windows\\System32
276
    '''
L
liuyq-617 已提交
277 278
    return 1
}
T
tangfangzhi 已提交
279
def run_win_ctest() {
T
tangfangzhi 已提交
280 281 282
    bat '''
        echo "windows ctest ..."
        time /t
T
tangfangzhi 已提交
283
        cd %WIN_INTERNAL_ROOT%\\debug
T
tangfangzhi 已提交
284
        ctest -j 1 || exit 7
T
tangfangzhi 已提交
285 286 287
        time /t
    '''
}
T
tangfangzhi 已提交
288 289 290
def run_win_test() {
    bat '''
        echo "windows test ..."
T
tangfangzhi 已提交
291
        cd %WIN_CONNECTOR_ROOT%
T
tangfangzhi 已提交
292
        python -m pip install .
T
tangfangzhi 已提交
293
        xcopy /e/y/i/f %WIN_INTERNAL_ROOT%\\debug\\build\\lib\\taos.dll C:\\Windows\\System32
T
tangfangzhi 已提交
294
        ls -l C:\\Windows\\System32\\taos.dll
T
tangfangzhi 已提交
295
        time /t
T
tangfangzhi 已提交
296
        cd %WIN_SYSTEM_TEST_ROOT%
T
tangfangzhi 已提交
297
        echo "testing ..."
298
        test-all.bat ci
T
tangfangzhi 已提交
299 300 301
        time /t
    '''
}
L
liuyq-617 已提交
302 303

pipeline {
T
tangfangzhi 已提交
304 305 306
    agent none
    options { skipDefaultCheckout() } 
    environment{
T
tangfangzhi 已提交
307
        WKDIR = '/var/lib/jenkins/workspace'
T
tangfangzhi 已提交
308
        WK = '/var/lib/jenkins/workspace/TDinternal'
T
tangfangzhi 已提交
309 310
        WKC = '/var/lib/jenkins/workspace/TDinternal/community'
        WKPY = '/var/lib/jenkins/workspace/taos-connector-python'
T
tangfangzhi 已提交
311 312 313 314
    }
    stages {
        stage('run test') {
            parallel {
T
tangfangzhi 已提交
315 316
                stage('windows test') {
                    agent{label " windows10_01 || windows10_02 || windows10_03 || windows10_04 "}
T
tangfangzhi 已提交
317 318 319 320 321 322
                    environment{
                        WIN_INTERNAL_ROOT="C:\\workspace\\${env.EXECUTOR_NUMBER}\\TDinternal"
                        WIN_COMMUNITY_ROOT="C:\\workspace\\${env.EXECUTOR_NUMBER}\\TDinternal\\community"
                        WIN_SYSTEM_TEST_ROOT="C:\\workspace\\${env.EXECUTOR_NUMBER}\\TDinternal\\community\\tests\\system-test"
                        WIN_CONNECTOR_ROOT="C:\\workspace\\${env.EXECUTOR_NUMBER}\\taos-connector-python"
                    }
T
tangfangzhi 已提交
323
                    steps {
T
tangfangzhi 已提交
324
                        catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
T
tangfangzhi 已提交
325
                            timeout(time: 55, unit: 'MINUTES'){
T
tangfangzhi 已提交
326 327 328 329 330 331
                                pre_test_win()
                                pre_test_build_win()
                                run_win_ctest()
                                run_win_test()
                            }
                        }
T
tangfangzhi 已提交
332 333
                    }
                }
T
tangfangzhi 已提交
334
                stage('linux test') {
T
tangfangzhi 已提交
335
                    agent{label " worker03 || slave215 || slave217 || slave219 "}
T
tangfangzhi 已提交
336 337 338 339 340
                    options { skipDefaultCheckout() } 
                    when {
                        changeRequest()
                    }
                    steps {
341 342 343 344 345 346 347 348
                        script {
                            def linux_node_ip = sh (
                                script: 'ip addr|grep 192|grep -v virbr|awk "{print \\\$2}"|sed "s/\\/.*//"',
                                returnStdout: true
                            ).trim()
                            echo "${linux_node_ip}"
                            echo "${WKDIR}/restore.sh -p ${BRANCH_NAME} -n ${BUILD_ID} -c {container name}"
                        }
T
tangfangzhi 已提交
349
                        catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
350
                            timeout(time: 120, unit: 'MINUTES'){
351
                                pre_test()
T
tangfangzhi 已提交
352 353 354 355
                                script {
                                    sh '''
                                        date
                                        rm -rf ${WKC}/debug
356
                                        cd ${WKC}/tests/parallel_test
T
tangfangzhi 已提交
357 358 359 360
                                        time ./container_build.sh -w ${WKDIR} -t 8 -e
                                        rm -f /tmp/cases.task
                                        ./collect_cases.sh -e
                                    '''
T
tangfangzhi 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
                                    def extra_param = ""
                                    def log_server_file = "/home/log_server.json"
                                    def timeout_cmd = ""
                                    if (fileExists(log_server_file)) {
                                        def log_server_enabled = sh (
                                            script: 'jq .enabled ' + log_server_file,
                                            returnStdout: true
                                        ).trim()
                                        def timeout_param = sh (
                                            script: 'jq .timeout ' + log_server_file,
                                            returnStdout: true
                                        ).trim()
                                        if (timeout_param != "null" &&  timeout_param != "0") {
                                            timeout_cmd = "timeout " + timeout_param
                                        }
                                        if (log_server_enabled == "1") {
                                            def log_server = sh (
                                                script: 'jq .server ' + log_server_file + ' | sed "s/\\\"//g"',
                                                returnStdout: true
                                            ).trim()
                                            if (log_server != "null" && log_server != "") { 
                                                extra_param = "-w " + log_server
                                            }
                                        }
                                    }
T
tangfangzhi 已提交
386 387 388 389
                                    sh '''
                                        cd ${WKC}/tests/parallel_test
                                        export DEFAULT_RETRY_TIME=2
                                        date
T
tangfangzhi 已提交
390
                                        ''' + timeout_cmd + ''' time ./run.sh -e -m /home/m.json -t /tmp/cases.task -b ${BRANCH_NAME}_${BUILD_ID} -l ${WKDIR}/log -o 480 ''' + extra_param + '''
T
tangfangzhi 已提交
391 392 393 394
                                    '''
                                }
                            }
                        }
T
tangfangzhi 已提交
395
                        catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
T
tangfangzhi 已提交
396
                            timeout(time: 15, unit: 'MINUTES'){
T
tangfangzhi 已提交
397 398 399 400
                                script {
                                    sh '''
                                        echo "packaging ..."
                                        date
401
                                        rm -rf ${WKC}/release/*
T
tangfangzhi 已提交
402 403
                                        cd ${WKC}/packaging
                                        ./release.sh -v cluster -n 3.0.0.100 -s static
T
tangfangzhi 已提交
404
                                    '''
405 406
                                }
                            }
T
tangfangzhi 已提交
407 408 409
                        }
                    }
                }
L
liuyq-617 已提交
410
            }
T
tangfangzhi 已提交
411 412 413
        }
    }
    post {  
L
liuyq-617 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
        success {
            emailext (
                subject: "PR-result: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' SUCCESS",
                body: """<!DOCTYPE html>
                <html>
                <head>
                <meta charset="UTF-8">
                </head>
                <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
                    <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 16pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
                        <tr>
                            <td><br />
                                <b><font color="#0B610B"><font size="6">构建信息</font></font></b>
                                <hr size="2" width="100%" align="center" /></td>
                        </tr>
                        <tr>
                            <td>
                                <ul>
                                <div style="font-size:18px">
                                    <li>构建名称>>分支:${env.BRANCH_NAME}</li>
                                    <li>构建结果:<span style="color:green"> Successful </span></li>
                                    <li>构建编号:${BUILD_NUMBER}</li>
                                    <li>触发用户:${env.CHANGE_AUTHOR}</li>
                                    <li>提交信息:${env.CHANGE_TITLE}</li>
                                    <li>构建地址:<a href=${BUILD_URL}>${BUILD_URL}</a></li>
                                    <li>构建日志:<a href=${BUILD_URL}console>${BUILD_URL}console</a></li>
                                    
                                </div>
                                </ul>
                            </td>
                        </tr>
                    </table></font>
                </body>
                </html>""",
                to: "${env.CHANGE_AUTHOR_EMAIL}",
                from: "support@taosdata.com"
            )
        }
        failure {
            emailext (
                subject: "PR-result: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' FAIL",
                body: """<!DOCTYPE html>
                <html>
                <head>
                <meta charset="UTF-8">
                </head>
                <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
                    <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 16pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
                        <tr>
                            <td><br />
                                <b><font color="#0B610B"><font size="6">构建信息</font></font></b>
                                <hr size="2" width="100%" align="center" /></td>
                        </tr>
                        <tr>
                            <td>
                                <ul>
                                <div style="font-size:18px">
                                    <li>构建名称>>分支:${env.BRANCH_NAME}</li>
                                    <li>构建结果:<span style="color:red"> Failure </span></li>
                                    <li>构建编号:${BUILD_NUMBER}</li>
                                    <li>触发用户:${env.CHANGE_AUTHOR}</li>
                                    <li>提交信息:${env.CHANGE_TITLE}</li>
                                    <li>构建地址:<a href=${BUILD_URL}>${BUILD_URL}</a></li>
                                    <li>构建日志:<a href=${BUILD_URL}console>${BUILD_URL}console</a></li>
                                    
                                </div>
                                </ul>
                            </td>
                        </tr>
                    </table></font>
                </body>
                </html>""",
                to: "${env.CHANGE_AUTHOR_EMAIL}",
                from: "support@taosdata.com"
            )
        }
    } 
}