blob: a8e10b7a63b250cfa67ea6a29a52e987d3aaad3a [file] [log] [blame]
Alexander Evseev02fe5eb2018-11-15 13:58:36 +01001import groovy.json.JsonOutput
2
Filip Pytlounfcce97c2017-03-07 14:06:07 +01003def gerrit = new com.mirantis.mk.Gerrit()
Filip Pytloun6a057302017-03-07 16:33:30 +01004def common = new com.mirantis.mk.Common()
Filip Pytloun19376a82017-03-07 12:29:00 +01005
Denis Egorenko30618012018-09-28 11:30:28 +04006// extraVarsYaml contains GERRIT_ vars from gate job
7// or will contain GERRIT_ vars from reclass-system patch
8def extraVarsYaml = env.EXTRA_VARIABLES_YAML ?: ''
9if (extraVarsYaml != '') {
10 common.mergeEnv(env, extraVarsYaml)
11} else {
Alexander Evseev02fe5eb2018-11-15 13:58:36 +010012 extraVarsYaml = JsonOutput.toJson(env.getEnvironment().findAll{ it.key.startsWith('GERRIT_') })
Denis Egorenko30618012018-09-28 11:30:28 +040013}
azvyagintsevb266ad22018-09-11 12:11:11 +030014
azvyagintsev2f10cf52018-09-20 11:30:47 +030015def slaveNode = env.SLAVE_NODE ?: 'python&&docker'
16def gerritCredentials = env.CREDENTIALS_ID ?: 'gerrit'
azvyagintsevb266ad22018-09-11 12:11:11 +030017
azvyagintsev2f10cf52018-09-20 11:30:47 +030018def gerritRef = env.GERRIT_REFSPEC ?: null
Denis Egorenko7feef6f2018-09-27 17:49:55 +040019def defaultGitRef = env.DEFAULT_GIT_REF ?: null
20def defaultGitUrl = env.DEFAULT_GIT_URL ?: null
Filip Pytlounfcce97c2017-03-07 14:06:07 +010021
Jakub Josef4612c5d2017-03-30 16:04:26 +020022def checkouted = false
Jakub Joseffcb615e2017-04-10 14:34:40 +020023def merged = false
chnydad66d6fa2017-06-22 09:34:43 +020024def systemRefspec = "HEAD"
azvyagintsev2f10cf52018-09-20 11:30:47 +030025
Jakub Josefa63f9862018-01-11 17:58:38 +010026timeout(time: 12, unit: 'HOURS') {
azvyagintsevb266ad22018-09-11 12:11:11 +030027 node(slaveNode) {
azvyagintsev0c97ffc2018-09-11 11:55:58 +030028 try {
29 stage("Checkout") {
30 if (gerritRef) {
31 // job is triggered by Gerrit
32 // test if change aren't already merged
33 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, gerritCredentials)
34 merged = gerritChange.status == "MERGED"
35 if (!merged) {
36 checkouted = gerrit.gerritPatchsetCheckout([
37 credentialsId: gerritCredentials
38 ])
39 systemRefspec = GERRIT_REFSPEC
40 }
Denis Egorenko7feef6f2018-09-27 17:49:55 +040041 // change defaultGit variables if job triggered from Gerrit
42 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
43 } else if (defaultGitRef && defaultGitUrl) {
44 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", gerritCredentials)
azvyagintsev0c97ffc2018-09-11 11:55:58 +030045 }
46 }
47
48 stage("Test") {
49 if (merged) {
50 common.successMsg("Gerrit change is already merged, no need to test them")
51 } else {
52 if (checkouted) {
53
54 def documentationOnly = false
55 if (gerritRef) {
56 documentationOnly = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep -v .releasenotes", returnStatus: true) == 1
57 }
58
59 sh("git diff-tree --no-commit-id --diff-filter=d --name-only -r HEAD | grep .yml | xargs -I {} python -c \"import yaml; yaml.load(open('{}', 'r'))\" \\;")
60
61 def branches = [:]
62 def testModels = documentationOnly ? [] : TEST_MODELS.split(',')
azvyagintsev2f10cf52018-09-20 11:30:47 +030063 if (['master'].contains(env.GERRIT_BRANCH)) {
64 for (int i = 0; i < testModels.size(); i++) {
65 def cluster = testModels[i]
66 def clusterGitUrl = defaultGitUrl.substring(0, defaultGitUrl.lastIndexOf("/") + 1) + cluster
67 branches["${cluster}"] = {
68 build job: "test-salt-model-${cluster}", parameters: [
69 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: clusterGitUrl],
70 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: "HEAD"],
71 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: defaultGitUrl],
azvyagintsevb6ca9352018-09-20 13:10:55 +030072 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: systemRefspec]
azvyagintsev2f10cf52018-09-20 11:30:47 +030073 ]
74 }
azvyagintsev0c97ffc2018-09-11 11:55:58 +030075 }
azvyagintsev2f10cf52018-09-20 11:30:47 +030076 } else {
77 common.warningMsg("Tests for ${testModels} skipped!")
azvyagintsev0c97ffc2018-09-11 11:55:58 +030078 }
79 branches["cookiecutter"] = {
80 build job: "test-mk-cookiecutter-templates", parameters: [
azvyagintsevb266ad22018-09-11 12:11:11 +030081 [$class: 'StringParameterValue', name: 'RECLASS_SYSTEM_URL', value: defaultGitUrl],
Denis Egorenko30618012018-09-28 11:30:28 +040082 [$class: 'StringParameterValue', name: 'RECLASS_SYSTEM_GIT_REF', value: systemRefspec],
83 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: extraVarsYaml ]
azvyagintsev0c97ffc2018-09-11 11:55:58 +030084 ]
85 }
86 parallel branches
87 } else {
azvyagintsev2f10cf52018-09-20 11:30:47 +030088 error("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
azvyagintsev0c97ffc2018-09-11 11:55:58 +030089 }
90 }
91 }
92 } catch (Throwable e) {
azvyagintsev0c97ffc2018-09-11 11:55:58 +030093 currentBuild.result = "FAILURE"
94 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
95 throw e
96 } finally {
97 common.sendNotification(currentBuild.result, "", ["slack"])
Jakub Josefa63f9862018-01-11 17:58:38 +010098 }
Filip Pytlounfcce97c2017-03-07 14:06:07 +010099 }
Filip Pytloun19376a82017-03-07 12:29:00 +0100100}