blob: aeaee9a3cacd090681c390d4f4474cbb57a360d4 [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 *
azvyagintsev5173bc52018-09-21 13:24:59 +03006 **/
Jakub Joseff2a66e92017-03-08 17:21:07 +01007
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()
azvyagintsev5173bc52018-09-21 13:24:59 +030011
azvyagintsev3f678fd2018-09-28 19:21:45 +030012slaveNode = env.SLAVE_NODE ?: 'docker'
13giveVerify = false
Jakub Josef4251feb2017-03-10 16:10:53 +010014
15@NonCPS
azvyagintsev5173bc52018-09-21 13:24:59 +030016def isJobExists(jobName) {
17 return Jenkins.instance.items.find { it -> it.name.equals(jobName) }
Mikhail Ivanovf5cd07f2017-04-11 17:09:54 +040018}
azvyagintsev5173bc52018-09-21 13:24:59 +030019
Denis Egorenkoc33cb5a2018-09-27 18:33:12 +040020def callJobWithExtraVars(String jobName) {
21 def gerritVars = '\n---'
22 for (envVar in env.getEnvironment()) {
23 if (envVar.key.startsWith("GERRIT_")) {
24 gerritVars += "\n${envVar.key}: '${envVar.value}'"
25 }
26 }
azvyagintsev3f678fd2018-09-28 19:21:45 +030027 testJob = build job: jobName, parameters: [
28 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: gerritVars]
Denis Egorenkoc33cb5a2018-09-27 18:33:12 +040029 ]
azvyagintsev3f678fd2018-09-28 19:21:45 +030030 if (testJob.getResult() != 'SUCCESS') {
31 error("Gate job ${testJob.getBuildUrl().toString()} finished with ${testJob.getResult()} !")
32 }
33 giveVerify = true
Denis Egorenkoc33cb5a2018-09-27 18:33:12 +040034}
35
azvyagintsev5173bc52018-09-21 13:24:59 +030036
37timeout(time: 12, unit: 'HOURS') {
38 node(slaveNode) {
39 try {
40 // test if change is not already merged
41 ssh.prepareSshAgentKey(CREDENTIALS_ID)
42 ssh.ensureKnownHosts(GERRIT_HOST)
43 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
44 def doSubmit = false
azvyagintsev5173bc52018-09-21 13:24:59 +030045 stage("test") {
46 if (gerritChange.status != "MERGED" && !SKIP_TEST.equals("true")) {
47 // test max CodeReview
48 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Code-Review", "+")) {
49 doSubmit = true
50 def gerritProjectArray = GERRIT_PROJECT.tokenize("/")
51 def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1]
52 def jobsNamespace = JOBS_NAMESPACE
53 def plural_namespaces = ['salt-formulas', 'salt-models']
54 // remove plural s on the end of job namespace
55 if (JOBS_NAMESPACE in plural_namespaces) {
56 jobsNamespace = JOBS_NAMESPACE.substring(0, JOBS_NAMESPACE.length() - 1)
57 }
58 // salt-formulas tests have -latest on end of the name
59 if (JOBS_NAMESPACE.equals("salt-formulas")) {
60 gerritProject = gerritProject + "-latest"
61 }
62 def testJob = String.format("test-%s-%s", jobsNamespace, gerritProject)
azvyagintsev3f678fd2018-09-28 19:21:45 +030063 if (env.GERRIT_PROJECT == 'mk/cookiecutter-templates') {
64 callJobWithExtraVars('test-mk-cookiecutter-templates')
65 } else if (env.GERRIT_PROJECT == 'salt-models/reclass-system') {
66 callJobWithExtraVars('test-salt-model-reclass-system')
azvyagintsev5173bc52018-09-21 13:24:59 +030067 } else {
Denis Egorenkoc33cb5a2018-09-27 18:33:12 +040068 if (isJobExists(testJob)) {
69 common.infoMsg("Test job ${testJob} found, running")
70 def patchsetVerified = gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")
71 build job: testJob, parameters: [
72 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"],
73 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC]
74 ]
75 giveVerify = true
76 } else {
77 common.infoMsg("Test job ${testJob} not found")
78 }
azvyagintsev5173bc52018-09-21 13:24:59 +030079 }
80 } else {
81 common.errorMsg("Change don't have a CodeReview, skipping gate")
82 }
83 } else {
84 common.infoMsg("Test job skipped")
85 }
86 }
87 stage("submit review") {
88 if (gerritChange.status == "MERGED") {
89 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
90 } else if (doSubmit) {
91 if (giveVerify) {
92 common.warningMsg("Change ${GERRIT_CHANGE_NUMBER} don't have a Verified, but tests were successful, so adding Verified and submitting")
93 ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --verified +1 --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
94 } else {
95 ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
96 }
97 common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
98 }
99 }
100 } catch (Throwable e) {
101 // If there was an error or exception thrown, the build failed
102 currentBuild.result = "FAILURE"
103 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
104 throw e
105 }
106 }
azvyagintsev3f678fd2018-09-28 19:21:45 +0300107}