blob: d00d646bdcba3c840d54400120d202c45cba4705 [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
Jakub Josefa63f9862018-01-11 17:58:38 +010028timeout(time: 12, unit: 'HOURS') {
29 node("docker"){
30 try {
31 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"));
Jakub Josef83379312017-03-29 18:12:34 +020037 }
Jakub Josefd75c1d52017-03-15 18:06:03 +010038 }
Jakub Josefa63f9862018-01-11 17:58:38 +010039 }
40 stage ('Checkout source code'){
41 if (gerritRef) {
42 // job is triggered by Gerrit
43 checkouted = gerrit.gerritPatchsetCheckout ([
44 credentialsId : CREDENTIALS_ID
45 ])
46 } else if(defaultGitRef && defaultGitUrl) {
47 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Jakub Josef97e09572017-03-22 13:59:02 +010048 }
Jakub Josefa63f9862018-01-11 17:58:38 +010049 if(!checkouted){
50 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
51 }
52 }
53 stage ('Run Codenarc tests'){
54 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 }
65 }
66 }
67
68 } catch (Throwable e) {
69 currentBuild.result = 'FAILURE'
70 try{
71 def errLog = readFile('build/reports/codenarc/main.txt')
72 if(errLog){
73 common.errorMsg("Error log: ${errLog}")
74 }
75 }catch(ex){
76 common.errorMsg("Exception occured while reading codenarc output file", ex)
77 }
78 throw e
79 } finally {
80 // send notification
81 common.sendNotification(currentBuild.result, "" ,["slack"])
82 }
83 }
Jakub Josefd75c1d52017-03-15 18:06:03 +010084}
Jakub Josef27424bc2017-05-22 16:56:27 +020085@NonCPS
86def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){
87 def gerrit = new com.mirantis.mk.Gerrit()
88 def jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
89 def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber)
90 def buildNums =[]
91 for(int i=0;i<triggeredBuilds.size();i++){
92 buildNums.add(triggeredBuilds[i].number)
93 }
94 return buildNums
95}