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 | 38a45cd | 2023-02-27 14:22:13 +0200 | [diff] [blame^] | 20 | * Print 'global_variables' accumulated during workflow execution, including |
| 21 | * collected artifacts. |
| 22 | * Output is prepared in format that can be copy-pasted into groovy code |
| 23 | * to replay the workflow using the already created artifacts. |
| 24 | * |
| 25 | * @param global_variables Map that keeps the artifact URLs and used 'env' objects: |
| 26 | * {'PARAM1_NAME': <param1 value>, 'PARAM2_NAME': 'http://.../artifacts/param2_value', ...} |
| 27 | */ |
| 28 | def printVariables(global_variables) { |
| 29 | def message = "// Collected global_variables during the workflow:\n" |
| 30 | for (variable in global_variables) { |
| 31 | message += "env.${variable.key}=\"${variable.value}\"\n" |
| 32 | } |
| 33 | common.warningMsg(message) |
| 34 | } |
| 35 | |
| 36 | /** |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 37 | * Get Jenkins parameter names, values and types from jobName |
| 38 | * @param jobName job name |
| 39 | * @return Map with parameter names as keys and the following map as values: |
| 40 | * [ |
| 41 | * <str name1>: [type: <str cls1>, use_variable: <str name1>, defaultValue: <cls value1>], |
| 42 | * <str name2>: [type: <str cls2>, use_variable: <str name2>, defaultValue: <cls value2>], |
| 43 | * ] |
| 44 | */ |
| 45 | def getJobDefaultParameters(jobName) { |
| 46 | def jenkinsUtils = new com.mirantis.mk.JenkinsUtils() |
| 47 | def item = jenkinsUtils.getJobByName(env.JOB_NAME) |
| 48 | def parameters = [:] |
| 49 | def prop = item.getProperty(ParametersDefinitionProperty.class) |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 50 | if (prop != null) { |
| 51 | for (param in prop.getParameterDefinitions()) { |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 52 | def defaultParam = param.getDefaultParameterValue() |
| 53 | def cls = defaultParam.getClass().getName() |
| 54 | def value = defaultParam.getValue() |
| 55 | def name = defaultParam.getName() |
| 56 | parameters[name] = [type: cls, use_variable: name, defaultValue: value] |
| 57 | } |
| 58 | } |
| 59 | return parameters |
| 60 | } |
| 61 | |
| 62 | /** |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 63 | * Run a Jenkins job using the collected parameters |
| 64 | * |
| 65 | * @param job_name Name of the running job |
| 66 | * @param job_parameters Map that declares which values from global_variables should be used, in the following format: |
| 67 | * {'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] | 68 | * or |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 69 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'get_variable_from_url': <a key from global_variables which contains URL with required content>}, ...} |
| 70 | * or |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 71 | * {'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] | 72 | * or |
| 73 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'get_variable_from_yaml': {'yaml_url': <URL with YAML content>, |
| 74 | * '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] | 75 | * @param global_variables Map that keeps the artifact URLs and used 'env' objects: |
| 76 | * {'PARAM1_NAME': <param1 value>, 'PARAM2_NAME': 'http://.../artifacts/param2_value', ...} |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 77 | * @param propagate Boolean. If false: allows to collect artifacts after job is finished, even with FAILURE status |
| 78 | * If true: immediatelly fails the pipeline. DO NOT USE 'true' if you want to collect artifacts |
| 79 | * for 'finally' steps |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 80 | */ |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 81 | def runJob(job_name, job_parameters, global_variables, Boolean propagate = false) { |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 82 | def parameters = [] |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 83 | def common = new com.mirantis.mk.Common() |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 84 | def http = new com.mirantis.mk.Http() |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 85 | def engine = new groovy.text.GStringTemplateEngine() |
| 86 | def template |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 87 | def yamls_from_urls = [:] |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 88 | def base = [:] |
| 89 | base["url"] = '' |
| 90 | def variable_content |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 91 | |
| 92 | // Collect required parameters from 'global_variables' or 'env' |
| 93 | for (param in job_parameters) { |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 94 | if (param.value.containsKey('use_variable')) { |
| 95 | if (!global_variables[param.value.use_variable]) { |
| 96 | global_variables[param.value.use_variable] = env[param.value.use_variable] ?: '' |
| 97 | } |
| 98 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: global_variables[param.value.use_variable]]) |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 99 | common.infoMsg("${param.key}: <${param.value.type}> ${global_variables[param.value.use_variable]}") |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 100 | } else if (param.value.containsKey('get_variable_from_url')) { |
| 101 | if (!global_variables[param.value.get_variable_from_url]) { |
| 102 | global_variables[param.value.get_variable_from_url] = env[param.value.get_variable_from_url] ?: '' |
| 103 | } |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 104 | if (global_variables[param.value.get_variable_from_url]) { |
Dennis Dmitriev | 3782836 | 2019-11-11 18:06:49 +0200 | [diff] [blame] | 105 | 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] | 106 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: variable_content]) |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 107 | common.infoMsg("${param.key}: <${param.value.type}> ${variable_content}") |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 108 | } else { |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 109 | common.warningMsg("${param.key} is empty, skipping get_variable_from_url") |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 110 | } |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 111 | } else if (param.value.containsKey('get_variable_from_yaml')) { |
| 112 | if (param.value.get_variable_from_yaml.containsKey('yaml_url') && param.value.get_variable_from_yaml.containsKey('yaml_key')) { |
| 113 | // YAML url is stored in an environment or a global variable (like 'SI_CONFIG_ARTIFACT') |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 114 | def yaml_url_var = param.value.get_variable_from_yaml.yaml_url |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 115 | if (!global_variables[yaml_url_var]) { |
| 116 | global_variables[yaml_url_var] = env[yaml_url_var] ?: '' |
| 117 | } |
| 118 | yaml_url = global_variables[yaml_url_var] // Real YAML URL |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 119 | yaml_key = param.value.get_variable_from_yaml.yaml_key |
| 120 | // Key to get the data from YAML, to interpolate in the groovy, for example: |
| 121 | // <yaml_map_variable>.key.to.the[0].required.data , where yaml_key = '.key.to.the[0].required.data' |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 122 | if (yaml_url) { |
| 123 | if (!yamls_from_urls[yaml_url]) { |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 124 | common.infoMsg("Reading YAML from ${yaml_url} for ${param.key}") |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 125 | yaml_content = http.restGet(base, yaml_url) |
| 126 | yamls_from_urls[yaml_url] = readYaml text: yaml_content |
| 127 | } |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 128 | common.infoMsg("Getting key ${yaml_key} from YAML ${yaml_url} for ${param.key}") |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 129 | template_variables = [ |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 130 | 'yaml_data': yamls_from_urls[yaml_url] |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 131 | ] |
| 132 | request = "\${yaml_data${yaml_key}}" |
Dennis Dmitriev | 5e07671 | 2022-02-08 15:05:21 +0200 | [diff] [blame] | 133 | def result |
Dennis Dmitriev | 450cf73 | 2021-11-11 14:59:17 +0200 | [diff] [blame] | 134 | // Catch errors related to wrong key or index in the list or map objects |
| 135 | // For wrong key in map or wrong index in list, groovy returns <null> object, |
| 136 | // but it can be catched only after the string interpolation <template.toString()>, |
| 137 | // so we should catch the string 'null' instead of object <null>. |
| 138 | try { |
| 139 | template = engine.createTemplate(request).make(template_variables) |
Dennis Dmitriev | 5e07671 | 2022-02-08 15:05:21 +0200 | [diff] [blame] | 140 | result = template.toString() |
Dennis Dmitriev | 450cf73 | 2021-11-11 14:59:17 +0200 | [diff] [blame] | 141 | if (result == 'null') { |
| 142 | error "No such key or index, got 'null'" |
| 143 | } |
| 144 | } catch (e) { |
| 145 | error("Failed to get the key ${yaml_key} from YAML ${yaml_url}: " + e.toString()) |
| 146 | } |
| 147 | |
| 148 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: result]) |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 149 | common.infoMsg("${param.key}: <${param.value.type}>\n${result}") |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 150 | } else { |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 151 | common.warningMsg("'yaml_url' in ${param.key} is empty, skipping get_variable_from_yaml") |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 152 | } |
| 153 | } else { |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 154 | common.warningMsg("${param.key} missing 'yaml_url'/'yaml_key' parameters, skipping get_variable_from_yaml") |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 155 | } |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 156 | } else if (param.value.containsKey('use_template')) { |
| 157 | template = engine.createTemplate(param.value.use_template).make(global_variables) |
| 158 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: template.toString()]) |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 159 | common.infoMsg("${param.key}: <${param.value.type}>\n${template.toString()}") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 160 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Build the job |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 164 | def job_info = build job: "${job_name}", parameters: parameters, propagate: propagate |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 165 | return job_info |
| 166 | } |
| 167 | |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 168 | def runOrGetJob(job_name, job_parameters, global_variables, propagate, String fullTaskName = '') { |
| 169 | /** |
| 170 | * Run job directly or try to find already executed build |
| 171 | * Flow, in case CI_JOBS_OVERRIDES passed: |
| 172 | * |
| 173 | * |
| 174 | * CI_JOBS_OVERRIDES = text in yaml|json format |
| 175 | * CI_JOBS_OVERRIDES = 'kaas-testing-core-release-artifact' : 3505 |
| 176 | * 'reindex-testing-core-release-index-with-rc' : 2822 |
| 177 | * 'si-test-release-sanity-check-prepare-configuration': 1877 |
| 178 | */ |
| 179 | common = new com.mirantis.mk.Common() |
| 180 | def jobsOverrides = readYaml(text: env.CI_JOBS_OVERRIDES ?: '---') ?: [:] |
| 181 | // get id of overriding job |
| 182 | def jobOverrideID = jobsOverrides.getOrDefault(fullTaskName, '') |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 183 | if (fullTaskName in jobsOverrides.keySet()) { |
| 184 | common.warningMsg("Overriding: ${fullTaskName}/${job_name} <<< ${jobOverrideID}") |
| 185 | common.infoMsg("For debug pin use:\n'${fullTaskName}' : ${jobOverrideID}") |
| 186 | return Jenkins.instance.getItemByFullName(job_name, |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 187 | hudson.model.Job.class).getBuildByNumber(jobOverrideID.toInteger()) |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 188 | } else { |
| 189 | return runJob(job_name, job_parameters, global_variables, propagate) |
| 190 | } |
| 191 | } |
| 192 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 193 | /** |
| 194 | * Store URLs of the specified artifacts to the global_variables |
| 195 | * |
| 196 | * @param build_url URL of the completed job |
| 197 | * @param step_artifacts Map that contains artifact names in the job, and variable names |
| 198 | * where the URLs to that atrifacts should be stored, for example: |
| 199 | * {'ARTIFACT1': 'logs.tar.gz', 'ARTIFACT2': 'test_report.xml', ...} |
| 200 | * @param global_variables Map that will keep the artifact URLs. Variable 'ARTIFACT1', for example, |
| 201 | * be used in next job parameters: {'ARTIFACT1_URL':{ 'use_variable': 'ARTIFACT1', ...}} |
| 202 | * |
| 203 | * If the artifact with the specified name not found, the parameter ARTIFACT1_URL |
| 204 | * will be empty. |
| 205 | * |
| 206 | */ |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 207 | 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] | 208 | def common = new com.mirantis.mk.Common() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 209 | def http = new com.mirantis.mk.Http() |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 210 | if (!artifactory_url) { |
| 211 | artifactory_url = 'https://artifactory.mcp.mirantis.net/api/storage/si-local/jenkins-job-artifacts' |
| 212 | } |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 213 | def baseJenkins = [:] |
| 214 | def baseArtifactory = [:] |
| 215 | build_url = build_url.replaceAll(~/\/+$/, "") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 216 | baseArtifactory["url"] = artifactory_url + "/${job_name}/${build_num}" |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 217 | baseJenkins["url"] = build_url |
| 218 | def job_config = http.restGet(baseJenkins, "/api/json/") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 219 | def job_artifacts = job_config['artifacts'] |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 220 | common.infoMsg("Attempt to storeArtifacts for: ${job_name}/${build_num}") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 221 | for (artifact in step_artifacts) { |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 222 | try { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 223 | def artifactoryResp = http.restGet(baseArtifactory, "/${artifact.value}") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 224 | global_variables[artifact.key] = artifactoryResp.downloadUri |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 225 | common.infoMsg("Artifact URL ${artifactoryResp.downloadUri} stored to ${artifact.key}") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 226 | continue |
| 227 | } catch (Exception e) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 228 | common.warningMsg("Can't find an artifact in ${artifactory_url}/${job_name}/${build_num}/${artifact.value} to store in ${artifact.key}\n" + |
| 229 | "error code ${e.message}") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 230 | } |
| 231 | |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 232 | def 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] | 233 | if (job_artifact.size() == 1) { |
| 234 | // Store artifact URL |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 235 | def artifact_url = "${build_url}/artifact/${job_artifact[0]['relativePath']}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 236 | global_variables[artifact.key] = artifact_url |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 237 | common.infoMsg("Artifact URL ${artifact_url} stored to ${artifact.key}") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 238 | } else if (job_artifact.size() > 1) { |
| 239 | // Error: too many artifacts with the same name, fail the job |
| 240 | error "Multiple artifacts ${artifact.value} for ${artifact.key} found in the build results ${build_url}, expected one:\n${job_artifact}" |
| 241 | } else { |
| 242 | // Warning: no artifact with expected name |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 243 | common.warningMsg("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] | 244 | global_variables[artifact.key] = '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 249 | /** |
| 250 | * Update workflow job build description |
| 251 | * |
| 252 | * @param jobs_data Map with all job names and result statuses, to showing it in description |
| 253 | */ |
| 254 | def updateDescription(jobs_data) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 255 | def common = new com.mirantis.mk.Common() |
| 256 | def table = '' |
| 257 | def child_jobs_description = '<strong>Descriptions from jobs:</strong><br>' |
| 258 | def table_template_start = "<div><table style='border: solid 1px;'><tr><th>Job:</th><th>Duration:</th><th>Status:</th></tr>" |
| 259 | def table_template_end = "</table></div>" |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 260 | |
| 261 | for (jobdata in jobs_data) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 262 | def trstyle = "<tr>" |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 263 | // Grey background for 'finally' jobs in list |
| 264 | if (jobdata['type'] == 'finally') { |
| 265 | trstyle = "<tr style='background: #DDDDDD;'>" |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 266 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 267 | // 'description' instead of job name if it exists |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 268 | def display_name = "'${jobdata['name']}': ${jobdata['build_id']}" |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 269 | if (jobdata['desc'].toString() != "") { |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 270 | display_name = "'${jobdata['desc']}': ${jobdata['build_id']}" |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 271 | } |
| 272 | |
azvyagintsev | 2eeaa56 | 2022-01-27 12:03:40 +0200 | [diff] [blame] | 273 | // Attach url for already built jobs |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 274 | def build_url = display_name |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 275 | if (jobdata['build_url'] != "0") { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 276 | build_url = "<a href=${jobdata['build_url']}>$display_name</a>" |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // Styling the status of job result |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 280 | switch (jobdata['status'].toString()) { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 281 | case "SUCCESS": |
| 282 | status_style = "<td style='color: green;'><img src='/images/16x16/blue.png' alt='SUCCESS'>" |
| 283 | break |
| 284 | case "UNSTABLE": |
| 285 | status_style = "<td style='color: #FF5733;'><img src='/images/16x16/yellow.png' alt='UNSTABLE'>" |
| 286 | break |
| 287 | case "ABORTED": |
| 288 | status_style = "<td style='color: red;'><img src='/images/16x16/aborted.png' alt='ABORTED'>" |
| 289 | break |
| 290 | case "NOT_BUILT": |
| 291 | status_style = "<td style='color: red;'><img src='/images/16x16/aborted.png' alt='NOT_BUILT'>" |
| 292 | break |
| 293 | case "FAILURE": |
| 294 | status_style = "<td style='color: red;'><img src='/images/16x16/red.png' alt='FAILURE'>" |
| 295 | break |
| 296 | default: |
| 297 | status_style = "<td>-" |
| 298 | } |
| 299 | |
| 300 | // Collect table |
azvyagintsev | 2eeaa56 | 2022-01-27 12:03:40 +0200 | [diff] [blame] | 301 | table += "$trstyle<td>$build_url</td><td>${jobdata['duration']}</td>$status_style</td></tr>" |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 302 | |
| 303 | // Collecting descriptions of builded child jobs |
| 304 | if (jobdata['child_desc'] != "") { |
| 305 | child_jobs_description += "<b><small><a href=${jobdata['build_url']}>- ${jobdata['name']} (${jobdata['status']}):</a></small></b><br>" |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 306 | // remove "null" message-result from description, but leave XXX:JOBRESULT in description |
| 307 | if (jobdata['child_desc'] != "null") { |
| 308 | child_jobs_description += "<small>${jobdata['child_desc']}</small><br>" |
| 309 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | currentBuild.description = table_template_start + table + table_template_end + child_jobs_description |
| 313 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 314 | |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 315 | def runStep(global_variables, step, Boolean propagate = false, artifactoryBaseUrl = '') { |
| 316 | return { |
| 317 | def common = new com.mirantis.mk.Common() |
| 318 | def engine = new groovy.text.GStringTemplateEngine() |
| 319 | |
| 320 | String jobDescription = step['description'] ?: '' |
| 321 | def jobName = step['job'] |
| 322 | def jobParameters = [:] |
| 323 | def stepParameters = step['parameters'] ?: [:] |
| 324 | if (step['inherit_parent_params'] ?: false) { |
| 325 | // add parameters from the current job for the child job |
| 326 | jobParameters << getJobDefaultParameters(env.JOB_NAME) |
| 327 | } |
| 328 | // add parameters from the workflow for the child job |
| 329 | jobParameters << stepParameters |
| 330 | def wfPauseStepBeforeRun = (step['wf_pause_step_before_run'] ?: false).toBoolean() |
| 331 | def wfPauseStepTimeout = (step['wf_pause_step_timeout'] ?: 10).toInteger() |
| 332 | def wfPauseStepSlackReportChannel = step['wf_pause_step_slack_report_channel'] ?: '' |
| 333 | |
| 334 | if (wfPauseStepBeforeRun) { |
| 335 | // Try-catch construction will allow to continue Steps, if timeout reached |
| 336 | try { |
| 337 | if (wfPauseStepSlackReportChannel) { |
| 338 | def slack = new com.mirantis.mcp.SlackNotification() |
azvyagintsev | da22aa8 | 2022-06-10 15:46:55 +0300 | [diff] [blame] | 339 | wfPauseStepSlackReportChannel.split(',').each { |
| 340 | slack.jobResultNotification('wf_pause_step_before_run', |
| 341 | it.toString(), |
| 342 | env.JOB_NAME, null, |
| 343 | env.BUILD_URL, 'slack_webhook_url') |
| 344 | } |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 345 | } |
| 346 | timeout(time: wfPauseStepTimeout, unit: 'MINUTES') { |
| 347 | input("Workflow pause requested before run: ${jobName}/${jobDescription}\n" + |
| 348 | "Timeout set to ${wfPauseStepTimeout}.\n" + |
| 349 | "Do you want to proceed workflow?") |
| 350 | } |
| 351 | } catch (err) { // timeout reached or input false |
| 352 | def user = err.getCauses()[0].getUser() |
| 353 | if (user.toString() != 'SYSTEM') { // SYSTEM means timeout. |
| 354 | error("Aborted after workFlow pause by: [${user}]") |
| 355 | } else { |
| 356 | common.infoMsg("Timeout finished, continue..") |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | common.infoMsg("Attempt to run: ${jobName}/${jobDescription}") |
| 361 | // Collect job parameters and run the job |
| 362 | // WARN(alexz): desc must not contain invalid chars for yaml |
| 363 | def jobResult = runOrGetJob(jobName, jobParameters, |
| 364 | global_variables, propagate, jobDescription) |
| 365 | def buildDuration = jobResult.durationString ?: '-' |
| 366 | if (buildDuration.toString() == null) { |
| 367 | buildDuration = '-' |
| 368 | } |
| 369 | def jobSummary = [ |
| 370 | job_result : jobResult.getResult().toString(), |
| 371 | build_url : jobResult.getAbsoluteUrl().toString(), |
| 372 | build_id : jobResult.getId().toString(), |
| 373 | buildDuration : buildDuration, |
| 374 | desc : engine.createTemplate(jobDescription).make(global_variables), |
| 375 | ] |
| 376 | def _buildDescription = jobResult.getDescription().toString() |
| 377 | if(_buildDescription){ |
| 378 | jobSummary['build_description'] = _buildDescription |
| 379 | } |
| 380 | // Store links to the resulting artifacts into 'global_variables' |
| 381 | storeArtifacts(jobSummary['build_url'], step['artifacts'], |
| 382 | global_variables, jobName, jobSummary['build_id'], artifactoryBaseUrl) |
| 383 | return jobSummary |
| 384 | } |
| 385 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 386 | /** |
| 387 | * Run the workflow or final steps one by one |
| 388 | * |
| 389 | * @param steps List of steps (Jenkins jobs) to execute |
| 390 | * @param global_variables Map where the collected artifact URLs and 'env' objects are stored |
| 391 | * @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] | 392 | * @param jobs_data Map with all job names and result statuses, to showing it in description |
| 393 | * @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] | 394 | * @param propagate Boolean. If false: allows to collect artifacts after job is finished, even with FAILURE status |
| 395 | * If true: immediatelly fails the pipeline. DO NOT USE 'true' with runScenario(). |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 396 | */ |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 397 | 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] | 398 | common = new com.mirantis.mk.Common() |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 399 | // Show expected jobs list in description |
| 400 | updateDescription(jobs_data) |
| 401 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 402 | for (step in steps) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 403 | stage("Preparing for run job ${step['job']}") { |
| 404 | def job_summary = runStep(global_variables, step, propagate, artifactoryBaseUrl).call() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 405 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 406 | // Update jobs_data for updating description |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 407 | jobs_data[step_id]['build_url'] = job_summary['build_url'] |
| 408 | jobs_data[step_id]['build_id'] = job_summary['build_id'] |
| 409 | jobs_data[step_id]['status'] = job_summary['job_result'] |
| 410 | jobs_data[step_id]['duration'] = job_summary['buildDuration'] |
| 411 | jobs_data[step_id]['desc'] = job_summary['desc'] |
| 412 | if (job_summary['build_description']) { |
| 413 | jobs_data[step_id]['child_desc'] = job_summary['build_description'] |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 414 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 415 | updateDescription(jobs_data) |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 416 | def job_result = job_summary['job_result'] |
| 417 | def build_url = job_summary['build_url'] |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 418 | |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 419 | // Check job result, in case of SUCCESS, move to next step. |
Mykyta Karpin | 0bd8bc6 | 2020-04-29 12:27:14 +0300 | [diff] [blame] | 420 | // 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] | 421 | // In other cases check flag ignore_failed, if true ignore any statuses and keep going additionally |
| 422 | // 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] | 423 | if (job_result != 'SUCCESS') { |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 424 | def ignoreStepResult = false |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 425 | switch (job_result) { |
| 426 | // In cases when job was waiting too long in queue or internal job logic allows to skip building, |
| 427 | // 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] | 428 | case "NOT_BUILT": |
| 429 | ignoreStepResult = step['ignore_not_built'] ?: false |
| 430 | break; |
Dmitry Tyzhnenko | a141270 | 2022-02-14 21:23:09 +0200 | [diff] [blame] | 431 | case "UNSTABLE": |
Dmitry Tyzhnenko | bafca28 | 2022-02-17 17:49:54 +0200 | [diff] [blame] | 432 | ignoreStepResult = step['ignore_unstable'] ?: (step['ignore_failed'] ?: false) |
Dmitry Tyzhnenko | a141270 | 2022-02-14 21:23:09 +0200 | [diff] [blame] | 433 | break; |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 434 | default: |
| 435 | ignoreStepResult = step['ignore_failed'] ?: false |
Vasyl Saienko | e72b994 | 2021-03-04 10:54:49 +0200 | [diff] [blame] | 436 | if (ignoreStepResult && !step['skip_results'] ?: false) { |
| 437 | failed_jobs[build_url] = job_result |
| 438 | } |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 439 | } |
| 440 | if (!ignoreStepResult) { |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 441 | currentBuild.result = job_result |
| 442 | error "Job ${build_url} finished with result: ${job_result}" |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 443 | } |
| 444 | } |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 445 | common.infoMsg("Job ${build_url} finished with result: ${job_result}") |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 446 | } |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 447 | // Jump to next ID for updating next job data in description table |
| 448 | step_id++ |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 449 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Run the workflow scenario |
| 454 | * |
| 455 | * @param scenario: Map with scenario steps. |
| 456 | |
| 457 | * There are two keys in the scenario: |
| 458 | * workflow: contains steps to run deploy and test jobs |
| 459 | * finally: contains steps to run report and cleanup jobs |
| 460 | * |
| 461 | * Scenario execution example: |
| 462 | * |
| 463 | * scenario_yaml = """\ |
| 464 | * workflow: |
| 465 | * - job: deploy-kaas |
| 466 | * ignore_failed: false |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 467 | * description: "Management cluster ${KAAS_VERSION}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 468 | * parameters: |
| 469 | * KAAS_VERSION: |
| 470 | * type: StringParameterValue |
| 471 | * use_variable: KAAS_VERSION |
| 472 | * artifacts: |
| 473 | * KUBECONFIG_ARTIFACT: artifacts/management_kubeconfig |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 474 | * DEPLOYED_KAAS_VERSION: artifacts/management_version |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 475 | * |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 476 | * - job: create-child |
| 477 | * inherit_parent_params: true |
| 478 | * ignore_failed: false |
| 479 | * parameters: |
| 480 | * KUBECONFIG_ARTIFACT_URL: |
| 481 | * type: StringParameterValue |
| 482 | * use_variable: KUBECONFIG_ARTIFACT |
| 483 | * KAAS_VERSION: |
| 484 | * type: StringParameterValue |
| 485 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 486 | * RELEASE_NAME: |
| 487 | * type: StringParameterValue |
| 488 | * get_variable_from_yaml: |
| 489 | * yaml_url: SI_CONFIG_ARTIFACT |
| 490 | * yaml_key: .clusters[0].release_name |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 491 | * |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 492 | * - job: test-kaas-ui |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 493 | * ignore_not_built: false |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 494 | * parameters: |
| 495 | * KUBECONFIG_ARTIFACT_URL: |
| 496 | * type: StringParameterValue |
| 497 | * use_variable: KUBECONFIG_ARTIFACT |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 498 | * KAAS_VERSION: |
| 499 | * type: StringParameterValue |
| 500 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 501 | * artifacts: |
| 502 | * REPORT_SI_KAAS_UI: artifacts/test_kaas_ui_result.xml |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 503 | * finally: |
| 504 | * - job: testrail-report |
| 505 | * ignore_failed: true |
| 506 | * parameters: |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 507 | * KAAS_VERSION: |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 508 | * type: StringParameterValue |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 509 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 510 | * REPORTS_LIST: |
| 511 | * type: TextParameterValue |
| 512 | * use_template: | |
| 513 | * REPORT_SI_KAAS_UI: \$REPORT_SI_KAAS_UI |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 514 | * """ |
| 515 | * |
| 516 | * runScenario(scenario) |
| 517 | * |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 518 | * Scenario workflow keys: |
| 519 | * |
| 520 | * job: string. Jenkins job name |
| 521 | * 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] | 522 | * 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] | 523 | * 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 |
| 524 | * inherit_parent_params: bool. if true, provide all parameters from the parent job to the child job as defaults |
| 525 | * parameters: dict. parameters name and type to inherit from parent to child job, or from artifact to child job |
azvyagintsev | b3cd2a7 | 2022-01-17 23:41:34 +0200 | [diff] [blame] | 526 | * wf_pause_step_before_run: bool. Interactive pause exact step before run. |
| 527 | * wf_pause_step_slack_report_channel: If step paused, send message about it in slack. |
| 528 | * wf_pause_step_timeout: timeout im minutes to wait for manual unpause. |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 529 | */ |
| 530 | |
Dennis Dmitriev | 38a45cd | 2023-02-27 14:22:13 +0200 | [diff] [blame^] | 531 | def runScenario(scenario, slackReportChannel = '', artifactoryBaseUrl = '', Boolean logGlobalVariables = false) { |
Dennis Dmitriev | 79f3a2d | 2019-08-09 16:06:00 +0300 | [diff] [blame] | 532 | // Clear description before adding new messages |
| 533 | currentBuild.description = '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 534 | // Collect the parameters for the jobs here |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 535 | def global_variables = [:] |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 536 | // List of failed jobs to show at the end |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 537 | def failed_jobs = [:] |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 538 | // Jobs data to use for wf job build description |
| 539 | def jobs_data = [] |
| 540 | // Counter for matching step ID with cell ID in description table |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 541 | def step_id = 0 |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 542 | |
| 543 | // Generate expected list jobs for description |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 544 | def list_id = 0 |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 545 | for (step in scenario['workflow']) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 546 | def display_name = step['job'] |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 547 | if (step['description'] != null && step['description'].toString() != "") { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 548 | display_name = step['description'] |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 549 | } |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 550 | jobs_data.add([list_id : "$list_id", |
| 551 | type : "workflow", |
| 552 | name : "$display_name", |
| 553 | build_url : "0", |
| 554 | build_id : "-", |
| 555 | status : "-", |
| 556 | desc : "", |
azvyagintsev | 2eeaa56 | 2022-01-27 12:03:40 +0200 | [diff] [blame] | 557 | child_desc: "", |
| 558 | duration : '-']) |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 559 | list_id += 1 |
| 560 | } |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 561 | |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 562 | def pause_step_id = list_id |
| 563 | for (step in scenario['pause']) { |
| 564 | def display_name = step['job'] |
| 565 | if (step['description'] != null && step['description'].toString() != "") { |
| 566 | display_name = step['description'] |
| 567 | } |
| 568 | jobs_data.add([list_id : "$list_id", |
| 569 | type : "pause", |
| 570 | name : "$display_name", |
| 571 | build_url : "0", |
| 572 | build_id : "-", |
| 573 | status : "-", |
| 574 | desc : "", |
| 575 | child_desc: "", |
| 576 | duration : '-']) |
| 577 | list_id += 1 |
| 578 | } |
| 579 | |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 580 | def finally_step_id = list_id |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 581 | for (step in scenario['finally']) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 582 | def display_name = step['job'] |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 583 | if (step['description'] != null && step['description'].toString() != "") { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 584 | display_name = step['description'] |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 585 | } |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 586 | jobs_data.add([list_id : "$list_id", |
| 587 | type : "finally", |
| 588 | name : "$display_name", |
| 589 | build_url : "0", |
| 590 | build_id : "-", |
| 591 | status : "-", |
| 592 | desc : "", |
azvyagintsev | 2eeaa56 | 2022-01-27 12:03:40 +0200 | [diff] [blame] | 593 | child_desc: "", |
| 594 | duration : '-']) |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 595 | list_id += 1 |
| 596 | } |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 597 | def job_failed_flag = false |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 598 | try { |
| 599 | // Run the 'workflow' jobs |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 600 | 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] | 601 | } catch (InterruptedException x) { |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 602 | job_failed_flag = true |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 603 | error "The job was aborted" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 604 | } catch (e) { |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 605 | job_failed_flag = true |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 606 | error("Build failed: " + e.toString()) |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 607 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 608 | } finally { |
Dennis Dmitriev | 38a45cd | 2023-02-27 14:22:13 +0200 | [diff] [blame^] | 609 | // Log global_variables |
| 610 | if (logGlobalVariables) { |
| 611 | printVariables(global_variables) |
| 612 | } |
| 613 | |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 614 | flag_pause_variable = (env.PAUSE_FOR_DEBUG) != null |
| 615 | // Run the 'finally' or 'pause' jobs |
Sergey Lalov | 6e9400c | 2022-11-17 12:59:31 +0400 | [diff] [blame] | 616 | common.infoMsg(failed_jobs) |
| 617 | if (flag_pause_variable && (PAUSE_FOR_DEBUG && (job_failed_flag || failed_jobs))) { |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 618 | // Switching to 'pause' step index |
| 619 | common.infoMsg("FINALLY BLOCK - PAUSE") |
| 620 | step_id = pause_step_id |
| 621 | runSteps(scenario['pause'], global_variables, failed_jobs, jobs_data, step_id, false, artifactoryBaseUrl) |
| 622 | |
| 623 | } |
| 624 | // Switching to 'finally' step index |
| 625 | common.infoMsg("FINALLY BLOCK - CLEAR") |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 626 | step_id = finally_step_id |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 627 | 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] | 628 | |
| 629 | if (failed_jobs) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 630 | def statuses = [] |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 631 | failed_jobs.each { |
sgudz | 74c8cdd | 2020-01-23 14:26:32 +0200 | [diff] [blame] | 632 | statuses += it.value |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 633 | } |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 634 | if (statuses.contains('FAILURE')) { |
| 635 | currentBuild.result = 'FAILURE' |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 636 | } else if (statuses.contains('UNSTABLE')) { |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 637 | currentBuild.result = 'UNSTABLE' |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 638 | } else { |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 639 | currentBuild.result = 'FAILURE' |
| 640 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 641 | println "Failed jobs: ${failed_jobs}" |
vnaumov | 68cba27 | 2020-05-20 11:24:02 +0200 | [diff] [blame] | 642 | } else { |
| 643 | currentBuild.result = 'SUCCESS' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 644 | } |
vnaumov | 5a6eb8a | 2020-03-31 11:16:54 +0200 | [diff] [blame] | 645 | |
| 646 | if (slackReportChannel) { |
| 647 | def slack = new com.mirantis.mcp.SlackNotification() |
| 648 | slack.jobResultNotification(currentBuild.result, slackReportChannel, '', null, '', 'slack_webhook_url') |
| 649 | } |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 650 | } // finally |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 651 | } |