blob: e1c80f05b284ecb70fa50f7f29542b3cb8f4c5fd [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 **/
Alexander Evseev02fe5eb2018-11-15 13:58:36 +01007import groovy.json.JsonOutput
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()
azvyagintsev5173bc52018-09-21 13:24:59 +030012
azvyagintsev003090f2018-12-14 12:32:27 +020013slaveNode = env.SLAVE_NODE ?: 'virtual'
azvyagintsev3f678fd2018-09-28 19:21:45 +030014giveVerify = false
Jakub Josef4251feb2017-03-10 16:10:53 +010015
16@NonCPS
azvyagintsev5173bc52018-09-21 13:24:59 +030017def isJobExists(jobName) {
18 return Jenkins.instance.items.find { it -> it.name.equals(jobName) }
Mikhail Ivanovf5cd07f2017-04-11 17:09:54 +040019}
azvyagintsev5173bc52018-09-21 13:24:59 +030020
Denis Egorenkoc33cb5a2018-09-27 18:33:12 +040021def callJobWithExtraVars(String jobName) {
Denis Egorenko28f8c812018-12-10 16:06:37 +040022 def gerritVars = env.getEnvironment().findAll{ it.key.startsWith('GERRIT_') }
23 gerritVars['GERRIT_CI_MERGE_TRIGGER'] = true
azvyagintsev3f678fd2018-09-28 19:21:45 +030024 testJob = build job: jobName, parameters: [
Denis Egorenko28f8c812018-12-10 16:06:37 +040025 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: JsonOutput.toJson(gerritVars) ]
Denis Egorenkoc33cb5a2018-09-27 18:33:12 +040026 ]
azvyagintsev3f678fd2018-09-28 19:21:45 +030027 if (testJob.getResult() != 'SUCCESS') {
28 error("Gate job ${testJob.getBuildUrl().toString()} finished with ${testJob.getResult()} !")
29 }
30 giveVerify = true
Denis Egorenkoc33cb5a2018-09-27 18:33:12 +040031}
32
azvyagintsev5173bc52018-09-21 13:24:59 +030033
34timeout(time: 12, unit: 'HOURS') {
35 node(slaveNode) {
36 try {
37 // test if change is not already merged
38 ssh.prepareSshAgentKey(CREDENTIALS_ID)
39 ssh.ensureKnownHosts(GERRIT_HOST)
40 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
41 def doSubmit = false
azvyagintsev5173bc52018-09-21 13:24:59 +030042 stage("test") {
43 if (gerritChange.status != "MERGED" && !SKIP_TEST.equals("true")) {
44 // test max CodeReview
45 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Code-Review", "+")) {
46 doSubmit = true
47 def gerritProjectArray = GERRIT_PROJECT.tokenize("/")
48 def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1]
49 def jobsNamespace = JOBS_NAMESPACE
50 def plural_namespaces = ['salt-formulas', 'salt-models']
51 // remove plural s on the end of job namespace
52 if (JOBS_NAMESPACE in plural_namespaces) {
53 jobsNamespace = JOBS_NAMESPACE.substring(0, JOBS_NAMESPACE.length() - 1)
54 }
55 // salt-formulas tests have -latest on end of the name
56 if (JOBS_NAMESPACE.equals("salt-formulas")) {
57 gerritProject = gerritProject + "-latest"
58 }
59 def testJob = String.format("test-%s-%s", jobsNamespace, gerritProject)
Denis Egorenko28f8c812018-12-10 16:06:37 +040060 if (env.GERRIT_PROJECT == 'mk/cookiecutter-templates' || env.GERRIT_PROJECT == 'salt-models/reclass-system') {
61 callJobWithExtraVars('test-salt-model-ci-wrapper')
azvyagintsev5173bc52018-09-21 13:24:59 +030062 } else {
Denis Egorenkoc33cb5a2018-09-27 18:33:12 +040063 if (isJobExists(testJob)) {
64 common.infoMsg("Test job ${testJob} found, running")
65 def patchsetVerified = gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")
66 build job: testJob, parameters: [
67 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"],
68 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC]
69 ]
70 giveVerify = true
71 } else {
72 common.infoMsg("Test job ${testJob} not found")
73 }
azvyagintsev5173bc52018-09-21 13:24:59 +030074 }
75 } else {
76 common.errorMsg("Change don't have a CodeReview, skipping gate")
77 }
78 } else {
79 common.infoMsg("Test job skipped")
80 }
81 }
82 stage("submit review") {
83 if (gerritChange.status == "MERGED") {
84 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
85 } else if (doSubmit) {
86 if (giveVerify) {
87 common.warningMsg("Change ${GERRIT_CHANGE_NUMBER} don't have a Verified, but tests were successful, so adding Verified and submitting")
88 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))
89 } else {
90 ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
91 }
92 common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
93 }
94 }
95 } catch (Throwable e) {
96 // If there was an error or exception thrown, the build failed
97 currentBuild.result = "FAILURE"
98 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
99 throw e
100 }
101 }
azvyagintsev3f678fd2018-09-28 19:21:45 +0300102}