Jakub Josef | d75c1d5 | 2017-03-15 18:06:03 +0100 | [diff] [blame] | 1 | /** |
| 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 | |
| 9 | gerrit = new com.mirantis.mk.Gerrit() |
| 10 | common = new com.mirantis.mk.Common() |
| 11 | |
Jakub Josef | e1407ac | 2017-03-30 14:10:19 +0200 | [diff] [blame] | 12 | def gerritRef |
| 13 | try { |
| 14 | gerritRef = GERRIT_REFSPEC |
| 15 | } catch (MissingPropertyException e) { |
| 16 | gerritRef = null |
| 17 | } |
| 18 | |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 19 | def defaultGitRef, defaultGitUrl |
| 20 | try { |
| 21 | defaultGitRef = DEFAULT_GIT_REF |
| 22 | defaultGitUrl = DEFAULT_GIT_URL |
| 23 | } catch (MissingPropertyException e) { |
| 24 | defaultGitRef = null |
| 25 | defaultGitUrl = null |
| 26 | } |
| 27 | def checkouted = false |
| 28 | |
Jakub Josef | d75c1d5 | 2017-03-15 18:06:03 +0100 | [diff] [blame] | 29 | node("docker"){ |
| 30 | try { |
Jakub Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame^] | 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")); |
| 37 | } |
| 38 | } |
| 39 | } |
Jakub Josef | d75c1d5 | 2017-03-15 18:06:03 +0100 | [diff] [blame] | 40 | stage ('Checkout source code'){ |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 41 | if (gerritRef) { |
| 42 | // job is triggered by Gerrit |
| 43 | checkouted = gerrit.gerritPatchsetCheckout ([ |
| 44 | credentialsId : CREDENTIALS_ID |
Jakub Josef | d75c1d5 | 2017-03-15 18:06:03 +0100 | [diff] [blame] | 45 | ]) |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 46 | } else if(defaultGitRef && defaultGitUrl) { |
Jakub Josef | e1407ac | 2017-03-30 14:10:19 +0200 | [diff] [blame] | 47 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 48 | } |
| 49 | if(!checkouted){ |
Jakub Josef | 5ce6a36 | 2017-03-31 13:41:17 +0200 | [diff] [blame] | 50 | throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null") |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 51 | } |
Jakub Josef | d75c1d5 | 2017-03-15 18:06:03 +0100 | [diff] [blame] | 52 | } |
| 53 | stage ('Run Codenarc tests'){ |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 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 | } |
Jakub Josef | d75c1d5 | 2017-03-15 18:06:03 +0100 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | } catch (Throwable e) { |
| 69 | currentBuild.result = 'FAILURE' |
Jakub Josef | 97e0957 | 2017-03-22 13:59:02 +0100 | [diff] [blame] | 70 | try{ |
| 71 | def errLog = readFile('build/reports/codenarc/main.txt') |
| 72 | if(errLog){ |
| 73 | common.errorMsg("Error log: ${errLog}") |
| 74 | } |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 75 | }catch(ex){ |
| 76 | common.errorMsg("Exception occured while reading codenarc output file", ex) |
| 77 | } |
Jakub Josef | d75c1d5 | 2017-03-15 18:06:03 +0100 | [diff] [blame] | 78 | throw e |
| 79 | } finally { |
| 80 | // send notification |
| 81 | common.sendNotification(currentBuild.result, "" ,["slack"]) |
| 82 | } |
| 83 | } |
Jakub Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame^] | 84 | @NonCPS |
| 85 | def _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 | } |