Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * Run a simple workflow |
| 6 | * |
| 7 | * Function runScenario() executes a sequence of jobs, like |
| 8 | * - Parameters for the jobs are taken from the 'env' object |
| 9 | * - URLs of artifacts from completed jobs may be passed |
| 10 | * as parameters to the next jobs. |
| 11 | * |
| 12 | * No constants, environment specific logic or other conditional dependencies. |
| 13 | * All the logic should be placed in the workflow jobs, and perform necessary |
| 14 | * actions depending on the job parameters. |
| 15 | * The runScenario() function only provides the |
| 16 | * |
| 17 | */ |
| 18 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 19 | /** |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 20 | * Get Jenkins parameter names, values and types from jobName |
| 21 | * @param jobName job name |
| 22 | * @return Map with parameter names as keys and the following map as values: |
| 23 | * [ |
| 24 | * <str name1>: [type: <str cls1>, use_variable: <str name1>, defaultValue: <cls value1>], |
| 25 | * <str name2>: [type: <str cls2>, use_variable: <str name2>, defaultValue: <cls value2>], |
| 26 | * ] |
| 27 | */ |
| 28 | def getJobDefaultParameters(jobName) { |
| 29 | def jenkinsUtils = new com.mirantis.mk.JenkinsUtils() |
| 30 | def item = jenkinsUtils.getJobByName(env.JOB_NAME) |
| 31 | def parameters = [:] |
| 32 | def prop = item.getProperty(ParametersDefinitionProperty.class) |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 33 | if (prop != null) { |
| 34 | for (param in prop.getParameterDefinitions()) { |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 35 | def defaultParam = param.getDefaultParameterValue() |
| 36 | def cls = defaultParam.getClass().getName() |
| 37 | def value = defaultParam.getValue() |
| 38 | def name = defaultParam.getName() |
| 39 | parameters[name] = [type: cls, use_variable: name, defaultValue: value] |
| 40 | } |
| 41 | } |
| 42 | return parameters |
| 43 | } |
| 44 | |
| 45 | /** |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 46 | * Run a Jenkins job using the collected parameters |
| 47 | * |
| 48 | * @param job_name Name of the running job |
| 49 | * @param job_parameters Map that declares which values from global_variables should be used, in the following format: |
| 50 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'use_variable': <a key from global_variables>}, ...} |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 51 | * or |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 52 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'get_variable_from_url': <a key from global_variables which contains URL with required content>}, ...} |
| 53 | * or |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 54 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'use_template': <a GString multiline template with variables from global_variables>}, ...} |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame^] | 55 | * or |
| 56 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'get_variable_from_yaml': {'yaml_url': <URL with YAML content>, |
| 57 | * 'yaml_key': <a groovy-interpolating path to the key in the YAML, starting from dot '.'> } }, ...} |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 58 | * @param global_variables Map that keeps the artifact URLs and used 'env' objects: |
| 59 | * {'PARAM1_NAME': <param1 value>, 'PARAM2_NAME': 'http://.../artifacts/param2_value', ...} |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 60 | * @param propagate Boolean. If false: allows to collect artifacts after job is finished, even with FAILURE status |
| 61 | * If true: immediatelly fails the pipeline. DO NOT USE 'true' if you want to collect artifacts |
| 62 | * for 'finally' steps |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 63 | */ |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 64 | def runJob(job_name, job_parameters, global_variables, Boolean propagate = false) { |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 65 | def parameters = [] |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 66 | def http = new com.mirantis.mk.Http() |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 67 | def engine = new groovy.text.GStringTemplateEngine() |
| 68 | def template |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame^] | 69 | def yamls_from_urls = [:] |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 70 | def base = [:] |
| 71 | base["url"] = '' |
| 72 | def variable_content |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 73 | |
| 74 | // Collect required parameters from 'global_variables' or 'env' |
| 75 | for (param in job_parameters) { |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 76 | if (param.value.containsKey('use_variable')) { |
| 77 | if (!global_variables[param.value.use_variable]) { |
| 78 | global_variables[param.value.use_variable] = env[param.value.use_variable] ?: '' |
| 79 | } |
| 80 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: global_variables[param.value.use_variable]]) |
| 81 | println "${param.key}: <${param.value.type}> ${global_variables[param.value.use_variable]}" |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 82 | } else if (param.value.containsKey('get_variable_from_url')) { |
| 83 | if (!global_variables[param.value.get_variable_from_url]) { |
| 84 | global_variables[param.value.get_variable_from_url] = env[param.value.get_variable_from_url] ?: '' |
| 85 | } |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 86 | if (global_variables[param.value.get_variable_from_url]) { |
Dennis Dmitriev | 3782836 | 2019-11-11 18:06:49 +0200 | [diff] [blame] | 87 | variable_content = http.restGet(base, global_variables[param.value.get_variable_from_url]).trim() |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 88 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: variable_content]) |
| 89 | println "${param.key}: <${param.value.type}> ${variable_content}" |
| 90 | } else { |
| 91 | println "${param.key} is empty, skipping get_variable_from_url" |
| 92 | } |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame^] | 93 | } else if (param.value.containsKey('get_variable_from_yaml')) { |
| 94 | if (param.value.get_variable_from_yaml.containsKey('yaml_url') && param.value.get_variable_from_yaml.containsKey('yaml_key')) { |
| 95 | // YAML url is stored in an environment or a global variable (like 'SI_CONFIG_ARTIFACT') |
| 96 | yaml_url_var = param.value.get_variable_from_yaml.yaml_url |
| 97 | if (!global_variables[yaml_url_var]) { |
| 98 | global_variables[yaml_url_var] = env[yaml_url_var] ?: '' |
| 99 | } |
| 100 | yaml_url = global_variables[yaml_url_var] // Real YAML URL |
| 101 | yaml_key = param.value.get_variable_from_yaml.yaml_key // Key to get the data from YAML, to interpolate in the groovy, for example: |
| 102 | // <yaml_map_variable>.key.to.the[0].required.data , where yaml_key = '.key.to.the[0].required.data' |
| 103 | if (yaml_url) { |
| 104 | if (!yamls_from_urls[yaml_url]) { |
| 105 | println "Reading YAML from ${yaml_url} for ${param.key}" |
| 106 | yaml_content = http.restGet(base, yaml_url) |
| 107 | yamls_from_urls[yaml_url] = readYaml text: yaml_content |
| 108 | } |
| 109 | println "Getting key ${yaml_key} from YAML ${yaml_url} for ${param.key}" |
| 110 | template_variables = [ |
| 111 | 'yaml_data': yamls_from_urls[yaml_url] |
| 112 | ] |
| 113 | request = "\${yaml_data${yaml_key}}" |
| 114 | template = engine.createTemplate(request).make(template_variables) |
| 115 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: template.toString()]) |
| 116 | println "${param.key}: <${param.value.type}>\n${template.toString()}" |
| 117 | } else { |
| 118 | println "'yaml_url' in ${param.key} is empty, skipping get_variable_from_yaml" |
| 119 | } |
| 120 | } else { |
| 121 | println "${param.key} missing 'yaml_url'/'yaml_key' parameters, skipping get_variable_from_yaml" |
| 122 | } |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 123 | } else if (param.value.containsKey('use_template')) { |
| 124 | template = engine.createTemplate(param.value.use_template).make(global_variables) |
| 125 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: template.toString()]) |
| 126 | println "${param.key}: <${param.value.type}>\n${template.toString()}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 127 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Build the job |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 131 | def job_info = build job: "${job_name}", parameters: parameters, propagate: propagate |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 132 | return job_info |
| 133 | } |
| 134 | |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 135 | def runOrGetJob(job_name, job_parameters, global_variables, propagate, String fullTaskName = '') { |
| 136 | /** |
| 137 | * Run job directly or try to find already executed build |
| 138 | * Flow, in case CI_JOBS_OVERRIDES passed: |
| 139 | * |
| 140 | * |
| 141 | * CI_JOBS_OVERRIDES = text in yaml|json format |
| 142 | * CI_JOBS_OVERRIDES = 'kaas-testing-core-release-artifact' : 3505 |
| 143 | * 'reindex-testing-core-release-index-with-rc' : 2822 |
| 144 | * 'si-test-release-sanity-check-prepare-configuration': 1877 |
| 145 | */ |
| 146 | common = new com.mirantis.mk.Common() |
| 147 | def jobsOverrides = readYaml(text: env.CI_JOBS_OVERRIDES ?: '---') ?: [:] |
| 148 | // get id of overriding job |
| 149 | def jobOverrideID = jobsOverrides.getOrDefault(fullTaskName, '') |
| 150 | |
| 151 | if (fullTaskName in jobsOverrides.keySet()) { |
| 152 | common.warningMsg("Overriding: ${fullTaskName}/${job_name} <<< ${jobOverrideID}") |
| 153 | common.infoMsg("For debug pin use:\n'${fullTaskName}' : ${jobOverrideID}") |
| 154 | return Jenkins.instance.getItemByFullName(job_name, |
| 155 | hudson.model.Job.class).getBuildByNumber(jobOverrideID.toInteger()) |
| 156 | } else { |
| 157 | return runJob(job_name, job_parameters, global_variables, propagate) |
| 158 | } |
| 159 | } |
| 160 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 161 | /** |
| 162 | * Store URLs of the specified artifacts to the global_variables |
| 163 | * |
| 164 | * @param build_url URL of the completed job |
| 165 | * @param step_artifacts Map that contains artifact names in the job, and variable names |
| 166 | * where the URLs to that atrifacts should be stored, for example: |
| 167 | * {'ARTIFACT1': 'logs.tar.gz', 'ARTIFACT2': 'test_report.xml', ...} |
| 168 | * @param global_variables Map that will keep the artifact URLs. Variable 'ARTIFACT1', for example, |
| 169 | * be used in next job parameters: {'ARTIFACT1_URL':{ 'use_variable': 'ARTIFACT1', ...}} |
| 170 | * |
| 171 | * If the artifact with the specified name not found, the parameter ARTIFACT1_URL |
| 172 | * will be empty. |
| 173 | * |
| 174 | */ |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 175 | def storeArtifacts(build_url, step_artifacts, global_variables, job_name, build_num, artifactory_url = '') { |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 176 | def common = new com.mirantis.mk.Common() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 177 | def http = new com.mirantis.mk.Http() |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 178 | if (!artifactory_url) { |
| 179 | artifactory_url = 'https://artifactory.mcp.mirantis.net/api/storage/si-local/jenkins-job-artifacts' |
| 180 | } |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 181 | def baseJenkins = [:] |
| 182 | def baseArtifactory = [:] |
| 183 | build_url = build_url.replaceAll(~/\/+$/, "") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 184 | baseArtifactory["url"] = artifactory_url + "/${job_name}/${build_num}" |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 185 | baseJenkins["url"] = build_url |
| 186 | def job_config = http.restGet(baseJenkins, "/api/json/") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 187 | def job_artifacts = job_config['artifacts'] |
| 188 | for (artifact in step_artifacts) { |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 189 | try { |
| 190 | artifactoryResp = http.restGet(baseArtifactory, "/${artifact.value}") |
| 191 | global_variables[artifact.key] = artifactoryResp.downloadUri |
| 192 | println "Artifact URL ${artifactoryResp.downloadUri} stored to ${artifact.key}" |
| 193 | continue |
| 194 | } catch (Exception e) { |
| 195 | common.warningMsg("Can't find an artifact in ${artifactory_url}/${job_name}/${build_num}/${artifact.value} error code ${e.message}") |
| 196 | } |
| 197 | |
| 198 | job_artifact = job_artifacts.findAll { item -> artifact.value == item['fileName'] || artifact.value == item['relativePath'] } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 199 | if (job_artifact.size() == 1) { |
| 200 | // Store artifact URL |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 201 | def artifact_url = "${build_url}/artifact/${job_artifact[0]['relativePath']}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 202 | global_variables[artifact.key] = artifact_url |
| 203 | println "Artifact URL ${artifact_url} stored to ${artifact.key}" |
| 204 | } else if (job_artifact.size() > 1) { |
| 205 | // Error: too many artifacts with the same name, fail the job |
| 206 | error "Multiple artifacts ${artifact.value} for ${artifact.key} found in the build results ${build_url}, expected one:\n${job_artifact}" |
| 207 | } else { |
| 208 | // Warning: no artifact with expected name |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 209 | println "Artifact ${artifact.value} for ${artifact.key} not found in the build results ${build_url} and in the artifactory ${artifactory_url}/${job_name}/${build_num}/, found the following artifacts in Jenkins:\n${job_artifacts}" |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 210 | global_variables[artifact.key] = '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 215 | /** |
| 216 | * Update workflow job build description |
| 217 | * |
| 218 | * @param jobs_data Map with all job names and result statuses, to showing it in description |
| 219 | */ |
| 220 | def updateDescription(jobs_data) { |
| 221 | table = '' |
| 222 | child_jobs_description = '<strong>Descriptions from jobs:</strong><br>' |
| 223 | table_template_start = "<div><table style='border: solid 1px;'><tr><th>Job:</th><th>Status:</th></tr>" |
| 224 | table_template_end = "</table></div>" |
| 225 | |
| 226 | for (jobdata in jobs_data) { |
| 227 | // Grey background for 'finally' jobs in list |
| 228 | if (jobdata['type'] == 'finally') { |
| 229 | trstyle = "<tr style='background: #DDDDDD;'>" |
| 230 | } else { |
| 231 | trstyle = "<tr>" |
| 232 | } |
| 233 | |
| 234 | // 'description' instead of job name if it exists |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 235 | if (jobdata['desc'].toString() != "") { |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 236 | display_name = "'${jobdata['desc']}': ${jobdata['build_id']}" |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 237 | } else { |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 238 | display_name = "'${jobdata['name']}': ${jobdata['build_id']}" |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Attach url for already builded jobs |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 242 | if (jobdata['build_url'] != "0") { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 243 | build_url = "<a href=${jobdata['build_url']}>$display_name</a>" |
| 244 | } else { |
| 245 | build_url = display_name |
| 246 | } |
| 247 | |
| 248 | // Styling the status of job result |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 249 | switch (jobdata['status'].toString()) { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 250 | case "SUCCESS": |
| 251 | status_style = "<td style='color: green;'><img src='/images/16x16/blue.png' alt='SUCCESS'>" |
| 252 | break |
| 253 | case "UNSTABLE": |
| 254 | status_style = "<td style='color: #FF5733;'><img src='/images/16x16/yellow.png' alt='UNSTABLE'>" |
| 255 | break |
| 256 | case "ABORTED": |
| 257 | status_style = "<td style='color: red;'><img src='/images/16x16/aborted.png' alt='ABORTED'>" |
| 258 | break |
| 259 | case "NOT_BUILT": |
| 260 | status_style = "<td style='color: red;'><img src='/images/16x16/aborted.png' alt='NOT_BUILT'>" |
| 261 | break |
| 262 | case "FAILURE": |
| 263 | status_style = "<td style='color: red;'><img src='/images/16x16/red.png' alt='FAILURE'>" |
| 264 | break |
| 265 | default: |
| 266 | status_style = "<td>-" |
| 267 | } |
| 268 | |
| 269 | // Collect table |
| 270 | table += "$trstyle<td>$build_url</td>$status_style</td></tr>" |
| 271 | |
| 272 | // Collecting descriptions of builded child jobs |
| 273 | if (jobdata['child_desc'] != "") { |
| 274 | child_jobs_description += "<b><small><a href=${jobdata['build_url']}>- ${jobdata['name']} (${jobdata['status']}):</a></small></b><br>" |
| 275 | child_jobs_description += "<small>${jobdata['child_desc']}</small><br>" |
| 276 | } |
| 277 | } |
| 278 | currentBuild.description = table_template_start + table + table_template_end + child_jobs_description |
| 279 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 280 | |
| 281 | /** |
| 282 | * Run the workflow or final steps one by one |
| 283 | * |
| 284 | * @param steps List of steps (Jenkins jobs) to execute |
| 285 | * @param global_variables Map where the collected artifact URLs and 'env' objects are stored |
| 286 | * @param failed_jobs Map with failed job names and result statuses, to report it later |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 287 | * @param jobs_data Map with all job names and result statuses, to showing it in description |
| 288 | * @param step_id Counter for matching step ID with cell ID in description table |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 289 | * @param propagate Boolean. If false: allows to collect artifacts after job is finished, even with FAILURE status |
| 290 | * If true: immediatelly fails the pipeline. DO NOT USE 'true' with runScenario(). |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 291 | */ |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 292 | def runSteps(steps, global_variables, failed_jobs, jobs_data, step_id, Boolean propagate = false, artifactoryBaseUrl = '') { |
azvyagintsev | b673f39 | 2021-05-19 15:31:48 +0300 | [diff] [blame] | 293 | common = new com.mirantis.mk.Common() |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 294 | // Show expected jobs list in description |
| 295 | updateDescription(jobs_data) |
| 296 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 297 | for (step in steps) { |
| 298 | stage("Running job ${step['job']}") { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 299 | def engine = new groovy.text.GStringTemplateEngine() |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 300 | String desc = step['description'] ?: '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 301 | def job_name = step['job'] |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 302 | def job_parameters = [:] |
Dennis Dmitriev | 334eecd | 2020-04-30 14:32:45 +0300 | [diff] [blame] | 303 | def step_parameters = step['parameters'] ?: [:] |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 304 | if (step['inherit_parent_params'] ?: false) { |
| 305 | // add parameters from the current job for the child job |
| 306 | job_parameters << getJobDefaultParameters(env.JOB_NAME) |
| 307 | } |
| 308 | // add parameters from the workflow for the child job |
Dennis Dmitriev | 334eecd | 2020-04-30 14:32:45 +0300 | [diff] [blame] | 309 | job_parameters << step_parameters |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 310 | |
azvyagintsev | b673f39 | 2021-05-19 15:31:48 +0300 | [diff] [blame] | 311 | common.infoMsg("Attempt to run: ${job_name}/${desc}") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 312 | // Collect job parameters and run the job |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 313 | // WARN(alexz): desc must not contain invalid chars for yaml |
| 314 | def job_info = runOrGetJob(job_name, job_parameters, global_variables, propagate, desc) |
| 315 | def job_result = job_info.getResult().toString() |
| 316 | def build_url = job_info.getAbsoluteUrl().toString() |
| 317 | def build_description = job_info.getDescription().toString() |
| 318 | def build_id = job_info.getId().toString() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 319 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 320 | // Update jobs_data for updating description |
| 321 | jobs_data[step_id]['build_url'] = build_url |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 322 | jobs_data[step_id]['build_id'] = build_id |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 323 | jobs_data[step_id]['status'] = job_result |
| 324 | jobs_data[step_id]['desc'] = engine.createTemplate(desc).make(global_variables) |
| 325 | if (build_description) { |
| 326 | jobs_data[step_id]['child_desc'] = build_description |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 327 | } |
| 328 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 329 | updateDescription(jobs_data) |
| 330 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 331 | // Store links to the resulting artifacts into 'global_variables' |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 332 | storeArtifacts(build_url, step['artifacts'], global_variables, job_name, build_id, artifactoryBaseUrl) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 333 | |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 334 | // Check job result, in case of SUCCESS, move to next step. |
Mykyta Karpin | 0bd8bc6 | 2020-04-29 12:27:14 +0300 | [diff] [blame] | 335 | // In case job has status NOT_BUILT, fail the build or keep going depending on 'ignore_not_built' flag |
Vasyl Saienko | e72b994 | 2021-03-04 10:54:49 +0200 | [diff] [blame] | 336 | // In other cases check flag ignore_failed, if true ignore any statuses and keep going additionally |
| 337 | // if skip_results is not set or set to false fail entrie workflow, otherwise succed. |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 338 | if (job_result != 'SUCCESS') { |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 339 | def ignoreStepResult = false |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 340 | switch (job_result) { |
| 341 | // In cases when job was waiting too long in queue or internal job logic allows to skip building, |
| 342 | // job may have NOT_BUILT status. In that case ignore_not_built flag can be used not to fail scenario. |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 343 | case "NOT_BUILT": |
| 344 | ignoreStepResult = step['ignore_not_built'] ?: false |
| 345 | break; |
| 346 | default: |
| 347 | ignoreStepResult = step['ignore_failed'] ?: false |
Vasyl Saienko | e72b994 | 2021-03-04 10:54:49 +0200 | [diff] [blame] | 348 | if (ignoreStepResult && !step['skip_results'] ?: false) { |
| 349 | failed_jobs[build_url] = job_result |
| 350 | } |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 351 | } |
| 352 | if (!ignoreStepResult) { |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 353 | currentBuild.result = job_result |
| 354 | error "Job ${build_url} finished with result: ${job_result}" |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 355 | } // if (!ignoreStepResult) |
| 356 | } // if (job_result != 'SUCCESS') |
| 357 | println "Job ${build_url} finished with result: ${job_result}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 358 | } // stage ("Running job ${step['job']}") |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 359 | // Jump to next ID for updating next job data in description table |
| 360 | step_id++ |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 361 | } // for (step in scenario['workflow']) |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Run the workflow scenario |
| 366 | * |
| 367 | * @param scenario: Map with scenario steps. |
| 368 | |
| 369 | * There are two keys in the scenario: |
| 370 | * workflow: contains steps to run deploy and test jobs |
| 371 | * finally: contains steps to run report and cleanup jobs |
| 372 | * |
| 373 | * Scenario execution example: |
| 374 | * |
| 375 | * scenario_yaml = """\ |
| 376 | * workflow: |
| 377 | * - job: deploy-kaas |
| 378 | * ignore_failed: false |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 379 | * description: "Management cluster ${KAAS_VERSION}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 380 | * parameters: |
| 381 | * KAAS_VERSION: |
| 382 | * type: StringParameterValue |
| 383 | * use_variable: KAAS_VERSION |
| 384 | * artifacts: |
| 385 | * KUBECONFIG_ARTIFACT: artifacts/management_kubeconfig |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 386 | * DEPLOYED_KAAS_VERSION: artifacts/management_version |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 387 | * |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 388 | * - job: create-child |
| 389 | * inherit_parent_params: true |
| 390 | * ignore_failed: false |
| 391 | * parameters: |
| 392 | * KUBECONFIG_ARTIFACT_URL: |
| 393 | * type: StringParameterValue |
| 394 | * use_variable: KUBECONFIG_ARTIFACT |
| 395 | * KAAS_VERSION: |
| 396 | * type: StringParameterValue |
| 397 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame^] | 398 | * RELEASE_NAME: |
| 399 | * type: StringParameterValue |
| 400 | * get_variable_from_yaml: |
| 401 | * yaml_url: SI_CONFIG_ARTIFACT |
| 402 | * yaml_key: .clusters[0].release_name |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 403 | * |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 404 | * - job: test-kaas-ui |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 405 | * ignore_not_built: false |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 406 | * parameters: |
| 407 | * KUBECONFIG_ARTIFACT_URL: |
| 408 | * type: StringParameterValue |
| 409 | * use_variable: KUBECONFIG_ARTIFACT |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 410 | * KAAS_VERSION: |
| 411 | * type: StringParameterValue |
| 412 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 413 | * artifacts: |
| 414 | * REPORT_SI_KAAS_UI: artifacts/test_kaas_ui_result.xml |
| 415 | * |
| 416 | * finally: |
| 417 | * - job: testrail-report |
| 418 | * ignore_failed: true |
| 419 | * parameters: |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 420 | * KAAS_VERSION: |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 421 | * type: StringParameterValue |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 422 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 423 | * REPORTS_LIST: |
| 424 | * type: TextParameterValue |
| 425 | * use_template: | |
| 426 | * REPORT_SI_KAAS_UI: \$REPORT_SI_KAAS_UI |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 427 | * """ |
| 428 | * |
| 429 | * runScenario(scenario) |
| 430 | * |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 431 | * Scenario workflow keys: |
| 432 | * |
| 433 | * job: string. Jenkins job name |
| 434 | * ignore_failed: bool. if true, keep running the workflow jobs if the job is failed, but fail the workflow at finish |
Vasyl Saienko | e72b994 | 2021-03-04 10:54:49 +0200 | [diff] [blame] | 435 | * skip_results: bool. if true, keep running the workflow jobs if the job is failed, but do not fail the workflow at finish. Makes sense only when ignore_failed is set. |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 436 | * ignore_not_built: bool. if true, keep running the workflow jobs if the job set own status to NOT_BUILT, do not fail the workflow at finish for such jobs |
| 437 | * inherit_parent_params: bool. if true, provide all parameters from the parent job to the child job as defaults |
| 438 | * parameters: dict. parameters name and type to inherit from parent to child job, or from artifact to child job |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 439 | */ |
| 440 | |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 441 | def runScenario(scenario, slackReportChannel = '', artifactoryBaseUrl = '') { |
Dennis Dmitriev | 79f3a2d | 2019-08-09 16:06:00 +0300 | [diff] [blame] | 442 | // Clear description before adding new messages |
| 443 | currentBuild.description = '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 444 | // Collect the parameters for the jobs here |
| 445 | global_variables = [:] |
| 446 | // List of failed jobs to show at the end |
| 447 | failed_jobs = [:] |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 448 | // Jobs data to use for wf job build description |
| 449 | def jobs_data = [] |
| 450 | // Counter for matching step ID with cell ID in description table |
| 451 | step_id = 0 |
| 452 | |
| 453 | // Generate expected list jobs for description |
| 454 | list_id = 0 |
| 455 | for (step in scenario['workflow']) { |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 456 | if (step['description'] != null && step['description'].toString() != "") { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 457 | display_name = step['description'] |
| 458 | } else { |
| 459 | display_name = step['job'] |
| 460 | } |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 461 | jobs_data.add([list_id : "$list_id", |
| 462 | type : "workflow", |
| 463 | name : "$display_name", |
| 464 | build_url : "0", |
| 465 | build_id : "-", |
| 466 | status : "-", |
| 467 | desc : "", |
| 468 | child_desc: ""]) |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 469 | list_id += 1 |
| 470 | } |
| 471 | finally_step_id = list_id |
| 472 | for (step in scenario['finally']) { |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 473 | if (step['description'] != null && step['description'].toString() != "") { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 474 | display_name = step['description'] |
| 475 | } else { |
| 476 | display_name = step['job'] |
| 477 | } |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 478 | jobs_data.add([list_id : "$list_id", |
| 479 | type : "finally", |
| 480 | name : "$display_name", |
| 481 | build_url : "0", |
| 482 | build_id : "-", |
| 483 | status : "-", |
| 484 | desc : "", |
| 485 | child_desc: ""]) |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 486 | list_id += 1 |
| 487 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 488 | |
| 489 | try { |
| 490 | // Run the 'workflow' jobs |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 491 | runSteps(scenario['workflow'], global_variables, failed_jobs, jobs_data, step_id, false, artifactoryBaseUrl) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 492 | } catch (InterruptedException x) { |
| 493 | error "The job was aborted" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 494 | } catch (e) { |
| 495 | error("Build failed: " + e.toString()) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 496 | } finally { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 497 | // Switching to 'finally' step index |
| 498 | step_id = finally_step_id |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 499 | // Run the 'finally' jobs |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 500 | runSteps(scenario['finally'], global_variables, failed_jobs, jobs_data, step_id, false, artifactoryBaseUrl) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 501 | |
| 502 | if (failed_jobs) { |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 503 | statuses = [] |
| 504 | failed_jobs.each { |
sgudz | 74c8cdd | 2020-01-23 14:26:32 +0200 | [diff] [blame] | 505 | statuses += it.value |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 506 | } |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 507 | if (statuses.contains('FAILURE')) { |
| 508 | currentBuild.result = 'FAILURE' |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 509 | } else if (statuses.contains('UNSTABLE')) { |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 510 | currentBuild.result = 'UNSTABLE' |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 511 | } else { |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 512 | currentBuild.result = 'FAILURE' |
| 513 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 514 | println "Failed jobs: ${failed_jobs}" |
vnaumov | 68cba27 | 2020-05-20 11:24:02 +0200 | [diff] [blame] | 515 | } else { |
| 516 | currentBuild.result = 'SUCCESS' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 517 | } |
vnaumov | 5a6eb8a | 2020-03-31 11:16:54 +0200 | [diff] [blame] | 518 | |
| 519 | if (slackReportChannel) { |
| 520 | def slack = new com.mirantis.mcp.SlackNotification() |
| 521 | slack.jobResultNotification(currentBuild.result, slackReportChannel, '', null, '', 'slack_webhook_url') |
| 522 | } |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 523 | } // finally |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 524 | } |