Alexander Evseev | 02fe5eb | 2018-11-15 13:58:36 +0100 | [diff] [blame] | 1 | import groovy.json.JsonOutput |
| 2 | |
Filip Pytloun | fcce97c | 2017-03-07 14:06:07 +0100 | [diff] [blame] | 3 | def gerrit = new com.mirantis.mk.Gerrit() |
Filip Pytloun | 6a05730 | 2017-03-07 16:33:30 +0100 | [diff] [blame] | 4 | def common = new com.mirantis.mk.Common() |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 5 | |
Denis Egorenko | 3061801 | 2018-09-28 11:30:28 +0400 | [diff] [blame] | 6 | // extraVarsYaml contains GERRIT_ vars from gate job |
| 7 | // or will contain GERRIT_ vars from reclass-system patch |
| 8 | def extraVarsYaml = env.EXTRA_VARIABLES_YAML ?: '' |
| 9 | if (extraVarsYaml != '') { |
| 10 | common.mergeEnv(env, extraVarsYaml) |
| 11 | } else { |
Alexander Evseev | 02fe5eb | 2018-11-15 13:58:36 +0100 | [diff] [blame] | 12 | extraVarsYaml = JsonOutput.toJson(env.getEnvironment().findAll{ it.key.startsWith('GERRIT_') }) |
Denis Egorenko | 3061801 | 2018-09-28 11:30:28 +0400 | [diff] [blame] | 13 | } |
azvyagintsev | b266ad2 | 2018-09-11 12:11:11 +0300 | [diff] [blame] | 14 | |
azvyagintsev | 2f10cf5 | 2018-09-20 11:30:47 +0300 | [diff] [blame] | 15 | def slaveNode = env.SLAVE_NODE ?: 'python&&docker' |
| 16 | def gerritCredentials = env.CREDENTIALS_ID ?: 'gerrit' |
azvyagintsev | b266ad2 | 2018-09-11 12:11:11 +0300 | [diff] [blame] | 17 | |
azvyagintsev | 2f10cf5 | 2018-09-20 11:30:47 +0300 | [diff] [blame] | 18 | def gerritRef = env.GERRIT_REFSPEC ?: null |
Denis Egorenko | 7feef6f | 2018-09-27 17:49:55 +0400 | [diff] [blame] | 19 | def defaultGitRef = env.DEFAULT_GIT_REF ?: null |
| 20 | def defaultGitUrl = env.DEFAULT_GIT_URL ?: null |
Filip Pytloun | fcce97c | 2017-03-07 14:06:07 +0100 | [diff] [blame] | 21 | |
Jakub Josef | 4612c5d | 2017-03-30 16:04:26 +0200 | [diff] [blame] | 22 | def checkouted = false |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 23 | def merged = false |
chnyda | d66d6fa | 2017-06-22 09:34:43 +0200 | [diff] [blame] | 24 | def systemRefspec = "HEAD" |
azvyagintsev | 2f10cf5 | 2018-09-20 11:30:47 +0300 | [diff] [blame] | 25 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 26 | timeout(time: 12, unit: 'HOURS') { |
azvyagintsev | b266ad2 | 2018-09-11 12:11:11 +0300 | [diff] [blame] | 27 | node(slaveNode) { |
azvyagintsev | 0c97ffc | 2018-09-11 11:55:58 +0300 | [diff] [blame] | 28 | 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 Egorenko | 7feef6f | 2018-09-27 17:49:55 +0400 | [diff] [blame] | 41 | // 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) |
azvyagintsev | 0c97ffc | 2018-09-11 11:55:58 +0300 | [diff] [blame] | 45 | } |
| 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(',') |
azvyagintsev | 2f10cf5 | 2018-09-20 11:30:47 +0300 | [diff] [blame] | 63 | 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], |
azvyagintsev | b6ca935 | 2018-09-20 13:10:55 +0300 | [diff] [blame] | 72 | [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: systemRefspec] |
azvyagintsev | 2f10cf5 | 2018-09-20 11:30:47 +0300 | [diff] [blame] | 73 | ] |
| 74 | } |
azvyagintsev | 0c97ffc | 2018-09-11 11:55:58 +0300 | [diff] [blame] | 75 | } |
azvyagintsev | 2f10cf5 | 2018-09-20 11:30:47 +0300 | [diff] [blame] | 76 | } else { |
| 77 | common.warningMsg("Tests for ${testModels} skipped!") |
azvyagintsev | 0c97ffc | 2018-09-11 11:55:58 +0300 | [diff] [blame] | 78 | } |
| 79 | branches["cookiecutter"] = { |
| 80 | build job: "test-mk-cookiecutter-templates", parameters: [ |
azvyagintsev | b266ad2 | 2018-09-11 12:11:11 +0300 | [diff] [blame] | 81 | [$class: 'StringParameterValue', name: 'RECLASS_SYSTEM_URL', value: defaultGitUrl], |
Denis Egorenko | 3061801 | 2018-09-28 11:30:28 +0400 | [diff] [blame] | 82 | [$class: 'StringParameterValue', name: 'RECLASS_SYSTEM_GIT_REF', value: systemRefspec], |
| 83 | [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: extraVarsYaml ] |
azvyagintsev | 0c97ffc | 2018-09-11 11:55:58 +0300 | [diff] [blame] | 84 | ] |
| 85 | } |
| 86 | parallel branches |
| 87 | } else { |
azvyagintsev | 2f10cf5 | 2018-09-20 11:30:47 +0300 | [diff] [blame] | 88 | error("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null") |
azvyagintsev | 0c97ffc | 2018-09-11 11:55:58 +0300 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | } catch (Throwable e) { |
azvyagintsev | 0c97ffc | 2018-09-11 11:55:58 +0300 | [diff] [blame] | 93 | 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 Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 98 | } |
Filip Pytloun | fcce97c | 2017-03-07 14:06:07 +0100 | [diff] [blame] | 99 | } |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 100 | } |