blob: bd94f63fce2590c27adaed9c37c51d3fea96f00d [file] [log] [blame]
Jakub Joseff2a66e92017-03-08 17:21:07 +01001/**
2 * Gerrit gating pipeline
3 * CREDENTIALS_ID - Gerrit credentails ID
Jakub Josefb62d00a2017-03-13 12:21:40 +01004 * JOBS_NAMESPACE - Gerrit gating jobs namespace (mk, contrail, ...)
Jakub Joseff2a66e92017-03-08 17:21:07 +01005 *
6**/
Jakub Josef09d9d032017-04-04 17:51:03 +02007import groovy.json.JsonSlurper
Jakub Joseff2a66e92017-03-08 17:21:07 +01008
9def common = new com.mirantis.mk.Common()
10def gerrit = new com.mirantis.mk.Gerrit()
Jakub Josefeda07682017-03-09 14:50:54 +010011def ssh = new com.mirantis.mk.Ssh()
Jakub Joseff2a66e92017-03-08 17:21:07 +010012node("python") {
13 try{
Jakub Josef09d9d032017-04-04 17:51:03 +020014 // test if change is not already merged
15 ssh.prepareSshAgentKey(CREDENTIALS_ID)
16 ssh.ensureKnownHosts(GERRIT_HOST)
17 def output = ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit query --format=JSON change:%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER))
18 def jsonSlurper = new JsonSlurper()
19 def gerritChange = jsonSlurper.parseText(output)
Jakub Joseff2a66e92017-03-08 17:21:07 +010020 stage("test") {
Jakub Josef09d9d032017-04-04 17:51:03 +020021 if (gerritChange.status != "MERGED" && !SKIP_TEST.equals("true")){
Jakub Josef617e1162017-03-21 15:58:27 +010022 wrap([$class: 'AnsiColorBuildWrapper']) {
23 def gerritProjectArray = GERRIT_PROJECT.tokenize("/")
24 def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1]
25 def jobsNamespace = JOBS_NAMESPACE
26 // remove plural s on the end of job namespace
27 if (JOBS_NAMESPACE[JOBS_NAMESPACE.length() - 1].equals("s")){
28 jobsNamespace = JOBS_NAMESPACE.substring(0, JOBS_NAMESPACE.length() - 1)
29 }
Jakub Joseff64c3402017-03-21 18:58:40 +010030 def testJob = String.format("test-%s-%s", jobsNamespace, gerritProject)
Jakub Josef617e1162017-03-21 15:58:27 +010031 if (_jobExists(testJob)) {
32 common.infoMsg("Test job ${testJob} found, running")
33 build job: testJob, parameters: [
Jakub Josefc1c2ec22017-03-31 18:59:03 +020034 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"],
Jakub Josef83379312017-03-29 18:12:34 +020035 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC]
Jakub Josef617e1162017-03-21 15:58:27 +010036 ]
37 } else {
38 common.infoMsg("Test job ${testJob} not found")
39 }
Jakub Josefa722d432017-03-10 12:43:00 +010040 }
Jakub Josef617e1162017-03-21 15:58:27 +010041 } else {
42 common.infoMsg("Test job skipped")
Jakub Josefa722d432017-03-10 12:43:00 +010043 }
Jakub Joseff2a66e92017-03-08 17:21:07 +010044 }
45 stage("submit review"){
Jakub Josef09d9d032017-04-04 17:51:03 +020046 if(gerritChange.status == "MERGED"){
47 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
48 }else{
49 ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
50 common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
51 }
Jakub Joseffd91d1f2017-03-08 17:27:41 +010052 }
Jakub Joseff2a66e92017-03-08 17:21:07 +010053 } catch (Throwable e) {
54 // If there was an error or exception thrown, the build failed
55 currentBuild.result = "FAILURE"
56 throw e
57 } finally {
Jakub Josef83379312017-03-29 18:12:34 +020058 //common.sendNotification(currentBuild.result,"",["slack"])
Jakub Joseff2a66e92017-03-08 17:21:07 +010059 }
Jakub Josef4251feb2017-03-10 16:10:53 +010060}
61
62@NonCPS
63def _jobExists(jobName){
Jakub Josef253d4482017-03-13 14:04:46 +010064 return Jenkins.instance.items.find{it -> it.name.equals(jobName)}
65}