blob: 3ac493cd13fa999517c81841dfc3befd9fb8070e [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**/
7
8def common = new com.mirantis.mk.Common()
9def gerrit = new com.mirantis.mk.Gerrit()
Jakub Josefeda07682017-03-09 14:50:54 +010010def ssh = new com.mirantis.mk.Ssh()
Jakub Joseff2a66e92017-03-08 17:21:07 +010011node("python") {
12 try{
Jakub Josef09d9d032017-04-04 17:51:03 +020013 // test if change is not already merged
14 ssh.prepareSshAgentKey(CREDENTIALS_ID)
15 ssh.ensureKnownHosts(GERRIT_HOST)
Jakub Joseffcb615e2017-04-10 14:34:40 +020016 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
Jakub Joseff2a66e92017-03-08 17:21:07 +010017 stage("test") {
Jakub Josef841aac22017-04-05 14:00:35 +020018 if (gerritChange.status != "MERGED" && !SKIP_TEST.equals("true")){
Jakub Josef617e1162017-03-21 15:58:27 +010019 wrap([$class: 'AnsiColorBuildWrapper']) {
20 def gerritProjectArray = GERRIT_PROJECT.tokenize("/")
21 def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1]
22 def jobsNamespace = JOBS_NAMESPACE
Mikhail Ivanovf5cd07f2017-04-11 17:09:54 +040023 def plural_namespaces = ['salt-formulas', 'salt-models']
Jakub Josef617e1162017-03-21 15:58:27 +010024 // remove plural s on the end of job namespace
Mikhail Ivanovf5cd07f2017-04-11 17:09:54 +040025 if (JOBS_NAMESPACE in plural_namespaces){
Jakub Josef617e1162017-03-21 15:58:27 +010026 jobsNamespace = JOBS_NAMESPACE.substring(0, JOBS_NAMESPACE.length() - 1)
27 }
Jakub Josef274abb02017-05-16 11:55:14 +020028 // salt-formulas tests have -latest on end of the name
29 if(JOBS_NAMESPACE.equals("salt-formulas")){
30 gerritProject=gerritProject+"-latest"
31 }
Jakub Joseff64c3402017-03-21 18:58:40 +010032 def testJob = String.format("test-%s-%s", jobsNamespace, gerritProject)
Jakub Josef617e1162017-03-21 15:58:27 +010033 if (_jobExists(testJob)) {
34 common.infoMsg("Test job ${testJob} found, running")
35 build job: testJob, parameters: [
Jakub Josefc1c2ec22017-03-31 18:59:03 +020036 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"],
Jakub Josef83379312017-03-29 18:12:34 +020037 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC]
Jakub Josef617e1162017-03-21 15:58:27 +010038 ]
39 } else {
40 common.infoMsg("Test job ${testJob} not found")
41 }
Jakub Josefa722d432017-03-10 12:43:00 +010042 }
Jakub Josef617e1162017-03-21 15:58:27 +010043 } else {
44 common.infoMsg("Test job skipped")
Jakub Josefa722d432017-03-10 12:43:00 +010045 }
Jakub Joseff2a66e92017-03-08 17:21:07 +010046 }
47 stage("submit review"){
Jakub Josef841aac22017-04-05 14:00:35 +020048 if(gerritChange.status == "MERGED"){
Jakub Josef09d9d032017-04-04 17:51:03 +020049 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
50 }else{
51 ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
52 common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
53 }
Jakub Joseffd91d1f2017-03-08 17:27:41 +010054 }
Jakub Joseff2a66e92017-03-08 17:21:07 +010055 } catch (Throwable e) {
56 // If there was an error or exception thrown, the build failed
57 currentBuild.result = "FAILURE"
58 throw e
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)}
Mikhail Ivanovf5cd07f2017-04-11 17:09:54 +040065}