Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 1 | /* |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 2 | Global CI wrapper for testing next projects: |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 3 | - salt-models/reclass-system |
| 4 | - mk/cookiecutter-templates |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | import groovy.json.JsonOutput |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame] | 8 | gerrit = new com.mirantis.mk.Gerrit() |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 9 | |
| 10 | cookiecutterTemplatesRepo='mk/cookiecutter-templates' |
| 11 | reclassSystemRepo='salt-models/reclass-system' |
| 12 | slaveNode = env.getProperty('SLAVE_NODE') ?: 'python&&docker' |
| 13 | |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 14 | voteMatrix = [ |
| 15 | 'test-mk-cookiecutter-templates': true, |
| 16 | 'test-drivetrain': true, |
| 17 | 'oscore-test-cookiecutter-models': false, |
| 18 | 'test-salt-model-infra': true, |
| 19 | 'test-salt-model-mcp-virtual-lab': true, |
| 20 | ] |
| 21 | |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame] | 22 | baseGerritConfig = [:] |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 23 | jobResultComments = [:] |
| 24 | commentLock = false |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame] | 25 | |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 26 | // post Gerrit review comment to patch |
| 27 | def setGerritReviewComment() { |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame] | 28 | if (baseGerritConfig) { |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 29 | while(commentLock) { |
| 30 | sleep 5 |
| 31 | } |
| 32 | commentLock = true |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame] | 33 | LinkedHashMap config = baseGerritConfig.clone() |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 34 | String jobResultComment = '' |
| 35 | jobResultComments.each { job, info -> |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 36 | String skipped = voteMatrix.get(job, 'true') ? '' : '(non-voting)' |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 37 | jobResultComment += "- ${job} ${info.url}console : ${info.status} ${skipped}".trim() + '\n' |
| 38 | } |
| 39 | config['message'] = sh(script: "echo '${jobResultComment}'", returnStdout: true).trim() |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame] | 40 | gerrit.postGerritComment(config) |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 41 | commentLock = false |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame] | 42 | } |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 43 | } |
| 44 | |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 45 | // get job parameters for YAML-based job parametrization |
| 46 | def yamlJobParameters(LinkedHashMap jobParams) { |
| 47 | return [ |
| 48 | [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: JsonOutput.toJson(jobParams) ] |
| 49 | ] |
| 50 | } |
| 51 | |
| 52 | // run needed job with params |
| 53 | def runTests(String jobName, ArrayList jobParams) { |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 54 | def propagateStatus = voteMatrix.get(jobName, true) |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 55 | return { |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 56 | def jobBuild = build job: jobName, propagate: false, parameters: jobParams |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 57 | jobResultComments[jobName] = [ 'url': jobBuild.absoluteUrl, 'status': jobBuild.result ] |
| 58 | setGerritReviewComment() |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 59 | if (propagateStatus && jobBuild.result == 'FAILURE') { |
| 60 | throw new Exception("Build ${jobName} is failed!") |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 65 | timeout(time: 12, unit: 'HOURS') { |
| 66 | node(slaveNode) { |
| 67 | def common = new com.mirantis.mk.Common() |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 68 | def git = new com.mirantis.mk.Git() |
| 69 | def python = new com.mirantis.mk.Python() |
| 70 | |
Denis Egorenko | 28f8c81 | 2018-12-10 16:06:37 +0400 | [diff] [blame] | 71 | // Var EXTRA_VARIABLES_YAML contains any additional parameters for tests, |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 72 | // like manually specified Gerrit Refs/URLs, additional parameters and so on |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 73 | def buildTestParams = [:] |
Denis Egorenko | 28f8c81 | 2018-12-10 16:06:37 +0400 | [diff] [blame] | 74 | def buildTestParamsYaml = env.getProperty('EXTRA_VARIABLES_YAML') |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 75 | if (buildTestParamsYaml) { |
| 76 | common.mergeEnv(env, buildTestParamsYaml) |
| 77 | buildTestParams = readYaml text: buildTestParamsYaml |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // init required job variables |
| 81 | LinkedHashMap job_env = env.getEnvironment().findAll { k, v -> v } |
| 82 | |
| 83 | // Gerrit parameters |
| 84 | String gerritCredentials = job_env.get('CREDENTIALS_ID', 'gerrit') |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 85 | String gerritRef = job_env.get('GERRIT_REFSPEC') |
| 86 | String gerritProject = job_env.get('GERRIT_PROJECT') |
| 87 | String gerritName = job_env.get('GERRIT_NAME') |
| 88 | String gerritScheme = job_env.get('GERRIT_SCHEME') |
| 89 | String gerritHost = job_env.get('GERRIT_HOST') |
| 90 | String gerritPort = job_env.get('GERRIT_PORT') |
| 91 | String gerritChangeNumber = job_env.get('GERRIT_CHANGE_NUMBER') |
| 92 | String gerritPatchSetNumber = job_env.get('GERRIT_PATCHSET_NUMBER') |
| 93 | String gerritBranch = job_env.get('GERRIT_BRANCH') |
Denis Egorenko | e581f9e | 2018-12-11 18:56:14 +0400 | [diff] [blame] | 94 | Boolean gateMode = job_env.get('GERRIT_CI_MERGE_TRIGGER', false).toBoolean() |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 95 | |
| 96 | // Common and manual build parameters |
| 97 | LinkedHashMap projectsMap = [:] |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 98 | String distribRevision = 'nightly' |
| 99 | //checking if the branch is from release |
| 100 | if (gerritBranch.startsWith('release')) { |
| 101 | def distribRevisionRelease = gerritBranch.tokenize('/')[-1] |
| 102 | if (!common.checkRemoteBinary([apt_mk_version: distribRevisionRelease]).linux_system_repo_url) { |
| 103 | common.infoMsg("Binary release ${distribRevisionRelease} does not exist on http://mirror.mirantis.com. Falling back to 'proposed'.") |
| 104 | distribRevision = 'proposed' |
| 105 | } |
| 106 | distribRevision = distribRevisionRelease |
| 107 | } |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 108 | ArrayList testModels = job_env.get('TEST_MODELS', 'mcp-virtual-lab,infra').split(',') |
| 109 | |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 110 | stage('Gerrit prepare') { |
| 111 | // check if change aren't already merged |
| 112 | def gerritChange = gerrit.getGerritChange(gerritName, gerritHost, gerritChangeNumber, gerritCredentials) |
| 113 | if (gerritChange.status == "MERGED") { |
| 114 | common.successMsg('Patch set is alredy merged, no need to test it') |
| 115 | currentBuild.result = 'SUCCESS' |
| 116 | return |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 117 | } |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 118 | def defaultURL = "${gerritScheme}://${gerritName}@${gerritHost}:${gerritPort}" |
| 119 | projectsMap[gerritProject] = [ |
| 120 | 'url': "${defaultURL}/${gerritProject}", |
| 121 | 'ref': gerritRef, |
| 122 | 'branch': gerritBranch, |
| 123 | ] |
| 124 | buildType = 'Gerrit Trigger' |
| 125 | buildTestParams << job_env.findAll { k,v -> k ==~ /GERRIT_.+/ } |
| 126 | baseGerritConfig = [ |
| 127 | 'gerritName': gerritName, |
| 128 | 'gerritHost': gerritHost, |
| 129 | 'gerritChangeNumber': gerritChangeNumber, |
| 130 | 'credentialsId': gerritCredentials, |
| 131 | 'gerritPatchSetNumber': gerritPatchSetNumber, |
| 132 | ] |
| 133 | ArrayList descriptionMsgs = [ "Running with next parameters:" ] |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 134 | for(String project in projectsMap.keySet()) { |
Denis Egorenko | 7a806bb | 2018-12-03 14:37:05 +0400 | [diff] [blame] | 135 | descriptionMsgs.add("Ref for ${project} => ${projectsMap[project]['ref']}") |
| 136 | descriptionMsgs.add("Branch for ${project} => ${projectsMap[project]['branch']}") |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 137 | } |
| 138 | descriptionMsgs.add("Distrib revision => ${distribRevision}") |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 139 | currentBuild.description = descriptionMsgs.join('<br/>') |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 140 | |
| 141 | gerrit.gerritPatchsetCheckout([ |
| 142 | credentialsId: gerritCredentials |
| 143 | ]) |
| 144 | } |
| 145 | |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 146 | stage("Run tests") { |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 147 | def documentationOnly = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep -v .releasenotes", returnStatus: true) == 1 |
| 148 | if (documentationOnly) { |
| 149 | common.infoMsg("Tests skipped, documenation only changed!") |
| 150 | currentBuild.result = 'SUCCESS' |
| 151 | return |
| 152 | } |
| 153 | |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 154 | def branches = [:] |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 155 | String branchJobName = '' |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 156 | |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 157 | if (gerritProject == reclassSystemRepo && gerritBranch == 'master') { |
Denis Egorenko | bbc1268 | 2018-12-11 18:38:43 +0400 | [diff] [blame] | 158 | 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'))\" \\;") |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 159 | for (int i = 0; i < testModels.size(); i++) { |
| 160 | def cluster = testModels[i] |
| 161 | def clusterGitUrl = projectsMap[reclassSystemRepo]['url'].substring(0, projectsMap[reclassSystemRepo]['url'].lastIndexOf("/") + 1) + cluster |
| 162 | branchJobName = "test-salt-model-${cluster}" |
| 163 | def jobParams = [ |
| 164 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: clusterGitUrl], |
| 165 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: "HEAD"], |
| 166 | [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: projectsMap[reclassSystemRepo]['url']], |
| 167 | [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: projectsMap[reclassSystemRepo]['ref'] ], |
| 168 | ] |
| 169 | branches[branchJobName] = runTests(branchJobName, jobParams) |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 170 | } |
| 171 | } |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 172 | if (gerritProject == reclassSystemRepo || gerritProject == cookiecutterTemplatesRepo) { |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 173 | branchJobName = 'test-mk-cookiecutter-templates' |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 174 | branches[branchJobName] = runTests(branchJobName, yamlJobParameters(buildTestParams)) |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 175 | } |
Denis Egorenko | 28f8c81 | 2018-12-10 16:06:37 +0400 | [diff] [blame] | 176 | |
| 177 | if (!gateMode) { |
| 178 | if (gerritProject == cookiecutterTemplatesRepo) { |
| 179 | branchJobName = 'test-drivetrain' |
| 180 | branches[branchJobName] = runTests(branchJobName, yamlJobParameters(buildTestParams)) |
| 181 | branchJobName = 'oscore-test-cookiecutter-models' |
| 182 | branches[branchJobName] = runTests(branchJobName, yamlJobParameters(buildTestParams)) |
| 183 | } |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 184 | } |
| 185 | |
Denis Egorenko | f4d3f52 | 2018-12-06 16:33:11 +0400 | [diff] [blame] | 186 | branches.keySet().each { key -> |
| 187 | if (branches[key] instanceof Closure) { |
| 188 | jobResultComments[key] = [ 'url': job_env.get('BUILD_URL'), 'status': 'WAITING' ] |
| 189 | } |
| 190 | } |
Denis Egorenko | 8616487 | 2018-12-10 17:34:46 +0400 | [diff] [blame] | 191 | setGerritReviewComment() |
Denis Egorenko | d8d13e8 | 2018-12-07 16:16:31 +0400 | [diff] [blame] | 192 | parallel branches |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |