blob: 61dfae70b46c7ff44e773696f8d9a4a62b53ff2c [file] [log] [blame]
Jakub Josefd75c1d52017-03-15 18:06:03 +01001/**
2* Groovy code testing pipeline
3* CREDENTIALS_ID - gerrit credentials id
4* GRADLE_IMAGE - gradle image name
5* GRADLE_CMD - command(s) for gradle
6*
7**/
8
9gerrit = new com.mirantis.mk.Gerrit()
10common = new com.mirantis.mk.Common()
11
Jakub Josefe1407ac2017-03-30 14:10:19 +020012def gerritRef
13try {
14 gerritRef = GERRIT_REFSPEC
15} catch (MissingPropertyException e) {
16 gerritRef = null
17}
18
Jakub Josef83379312017-03-29 18:12:34 +020019def defaultGitRef, defaultGitUrl
20try {
21 defaultGitRef = DEFAULT_GIT_REF
22 defaultGitUrl = DEFAULT_GIT_URL
23} catch (MissingPropertyException e) {
24 defaultGitRef = null
25 defaultGitUrl = null
26}
27def checkouted = false
28
Jakub Josefd75c1d52017-03-15 18:06:03 +010029node("docker"){
30 try {
Jakub Josef27424bc2017-05-22 16:56:27 +020031 stage("stop old tests"){
32 if (gerritRef) {
33 def runningTestBuildNums = _getRunningTriggeredTestsBuildNumbers(env["JOB_NAME"], GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER)
34 for(int i=0; i<runningTestBuildNums.size(); i++){
35 common.infoMsg("Old test with run number ${runningTestBuildNums[i]} found, stopping")
36 Jenkins.instance.getItemByFullName(env["JOB_NAME"]).getBuildByNumber(runningTestBuildNums[i]).finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build"));
37 }
38 }
39 }
Jakub Josefd75c1d52017-03-15 18:06:03 +010040 stage ('Checkout source code'){
Jakub Josef83379312017-03-29 18:12:34 +020041 if (gerritRef) {
42 // job is triggered by Gerrit
43 checkouted = gerrit.gerritPatchsetCheckout ([
44 credentialsId : CREDENTIALS_ID
Jakub Josefd75c1d52017-03-15 18:06:03 +010045 ])
Jakub Josef83379312017-03-29 18:12:34 +020046 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020047 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Jakub Josef83379312017-03-29 18:12:34 +020048 }
49 if(!checkouted){
Jakub Josef5ce6a362017-03-31 13:41:17 +020050 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Jakub Josef83379312017-03-29 18:12:34 +020051 }
Jakub Josefd75c1d52017-03-15 18:06:03 +010052 }
53 stage ('Run Codenarc tests'){
Jakub Josef83379312017-03-29 18:12:34 +020054 if(checkouted){
55 def workspace = common.getWorkspace()
56 def jenkinsUID = common.getJenkinsUid()
57 def jenkinsGID = common.getJenkinsGid()
58 def gradle_report = sh (script: "docker run --rm -v ${workspace}:/usr/bin/app:rw -u ${jenkinsUID}:${jenkinsGID} ${GRADLE_IMAGE} ${GRADLE_CMD}", returnStdout: true).trim()
59 // Compilation failure doesn't fail the build
60 // Check gradle output explicitly
61 common.infoMsg(gradle_report)
62 if ( gradle_report =~ /Compilation failed/ ) {
63 throw new Exception("COMPILATION FAILED!")
64 }
Jakub Josefd75c1d52017-03-15 18:06:03 +010065 }
66 }
67
68 } catch (Throwable e) {
69 currentBuild.result = 'FAILURE'
Jakub Josef97e09572017-03-22 13:59:02 +010070 try{
71 def errLog = readFile('build/reports/codenarc/main.txt')
72 if(errLog){
73 common.errorMsg("Error log: ${errLog}")
74 }
Jakub Joseffcb615e2017-04-10 14:34:40 +020075 }catch(ex){
76 common.errorMsg("Exception occured while reading codenarc output file", ex)
77 }
Jakub Josefd75c1d52017-03-15 18:06:03 +010078 throw e
79 } finally {
80 // send notification
81 common.sendNotification(currentBuild.result, "" ,["slack"])
82 }
83}
Jakub Josef27424bc2017-05-22 16:56:27 +020084@NonCPS
85def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){
86 def gerrit = new com.mirantis.mk.Gerrit()
87 def jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
88 def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber)
89 def buildNums =[]
90 for(int i=0;i<triggeredBuilds.size();i++){
91 buildNums.add(triggeredBuilds[i].number)
92 }
93 return buildNums
94}