blob: 918fc98fe94f1cd38d89a2f2683c75779c8df490 [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 {
31 stage ('Checkout source code'){
Jakub Josef83379312017-03-29 18:12:34 +020032 if (gerritRef) {
33 // job is triggered by Gerrit
34 checkouted = gerrit.gerritPatchsetCheckout ([
35 credentialsId : CREDENTIALS_ID
Jakub Josefd75c1d52017-03-15 18:06:03 +010036 ])
Jakub Josef83379312017-03-29 18:12:34 +020037 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020038 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Jakub Josef83379312017-03-29 18:12:34 +020039 }
40 if(!checkouted){
Jakub Josef5ce6a362017-03-31 13:41:17 +020041 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Jakub Josef83379312017-03-29 18:12:34 +020042 }
Jakub Josefd75c1d52017-03-15 18:06:03 +010043 }
44 stage ('Run Codenarc tests'){
Jakub Josef83379312017-03-29 18:12:34 +020045 if(checkouted){
46 def workspace = common.getWorkspace()
47 def jenkinsUID = common.getJenkinsUid()
48 def jenkinsGID = common.getJenkinsGid()
49 def gradle_report = sh (script: "docker run --rm -v ${workspace}:/usr/bin/app:rw -u ${jenkinsUID}:${jenkinsGID} ${GRADLE_IMAGE} ${GRADLE_CMD}", returnStdout: true).trim()
50 // Compilation failure doesn't fail the build
51 // Check gradle output explicitly
52 common.infoMsg(gradle_report)
53 if ( gradle_report =~ /Compilation failed/ ) {
54 throw new Exception("COMPILATION FAILED!")
55 }
Jakub Josefd75c1d52017-03-15 18:06:03 +010056 }
57 }
58
59 } catch (Throwable e) {
60 currentBuild.result = 'FAILURE'
Jakub Josef97e09572017-03-22 13:59:02 +010061 try{
62 def errLog = readFile('build/reports/codenarc/main.txt')
63 if(errLog){
64 common.errorMsg("Error log: ${errLog}")
65 }
Jakub Joseffcb615e2017-04-10 14:34:40 +020066 }catch(ex){
67 common.errorMsg("Exception occured while reading codenarc output file", ex)
68 }
Jakub Josefd75c1d52017-03-15 18:06:03 +010069 throw e
70 } finally {
71 // send notification
72 common.sendNotification(currentBuild.result, "" ,["slack"])
73 }
74}