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 | */ |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 28 | def printVariables(global_variables, Boolean yamlStyle = true) { |
| 29 | def common = new com.mirantis.mk.Common() |
| 30 | def mcpcommon = new com.mirantis.mcp.Common() |
| 31 | def global_variables_msg = '' |
| 32 | if (yamlStyle) { |
| 33 | global_variables_msg = mcpcommon.dumpYAML(global_variables) |
| 34 | } else { |
| 35 | for (variable in global_variables) { |
| 36 | global_variables_msg += "env.${variable.key}=\"\"\"${variable.value}\"\"\"\n" |
| 37 | } |
Dennis Dmitriev | 38a45cd | 2023-02-27 14:22:13 +0200 | [diff] [blame] | 38 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 39 | def message = "// Collected global_variables during the workflow:\n${global_variables_msg}" |
Dennis Dmitriev | 38a45cd | 2023-02-27 14:22:13 +0200 | [diff] [blame] | 40 | common.warningMsg(message) |
| 41 | } |
| 42 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Print stack trace to the console |
| 46 | */ |
| 47 | def printStackTrace(e, String prefix = 'at com.mirantis') { |
| 48 | def common = new com.mirantis.mk.Common() |
| 49 | StringWriter writer = new StringWriter() |
| 50 | e.printStackTrace(new PrintWriter(writer)) |
| 51 | String stackTrace = writer |
| 52 | |
| 53 | // Filter the stacktrace to show only the lines related to the specified library |
| 54 | String[] lines = stackTrace.split("\n") |
| 55 | String stackTraceFiltered = '' |
| 56 | Boolean filteredLine = false |
| 57 | for (String line in lines) { |
| 58 | if (line.contains('at ') && line.contains(prefix)) { |
| 59 | if (!filteredLine) { |
| 60 | stackTraceFiltered += "...\n" |
| 61 | filteredLine = true |
| 62 | } |
| 63 | stackTraceFiltered += "${line}\n" |
| 64 | } |
| 65 | else if (!line.contains('at ')) { |
| 66 | if (filteredLine) { |
| 67 | stackTraceFiltered += "...\n" |
| 68 | filteredLine = false |
| 69 | } |
| 70 | stackTraceFiltered += "${line}\n" |
| 71 | } |
| 72 | } |
| 73 | common.errorMsg("Stack trace:\n${stackTraceFiltered}") |
| 74 | } |
| 75 | |
| 76 | |
Dennis Dmitriev | 38a45cd | 2023-02-27 14:22:13 +0200 | [diff] [blame] | 77 | /** |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 78 | * Get Jenkins parameter names, values and types from jobName |
| 79 | * @param jobName job name |
| 80 | * @return Map with parameter names as keys and the following map as values: |
| 81 | * [ |
| 82 | * <str name1>: [type: <str cls1>, use_variable: <str name1>, defaultValue: <cls value1>], |
| 83 | * <str name2>: [type: <str cls2>, use_variable: <str name2>, defaultValue: <cls value2>], |
| 84 | * ] |
| 85 | */ |
| 86 | def getJobDefaultParameters(jobName) { |
| 87 | def jenkinsUtils = new com.mirantis.mk.JenkinsUtils() |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 88 | def item = jenkinsUtils.getJobByName(jobName) |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 89 | def parameters = [:] |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 90 | // def prop = item.getProperty(ParametersDefinitionProperty.class) |
| 91 | def prop = item.getProperty(ParametersDefinitionProperty) |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 92 | if (prop != null) { |
| 93 | for (param in prop.getParameterDefinitions()) { |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 94 | def defaultParam = param.getDefaultParameterValue() |
| 95 | def cls = defaultParam.getClass().getName() |
| 96 | def value = defaultParam.getValue() |
| 97 | def name = defaultParam.getName() |
| 98 | parameters[name] = [type: cls, use_variable: name, defaultValue: value] |
| 99 | } |
| 100 | } |
| 101 | return parameters |
| 102 | } |
| 103 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 104 | |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 105 | /** |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 106 | * Generate parameters for a Jenkins job using different sources |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 107 | * |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 108 | * @param job_parameters Map that declares which values from global_variables should be used, in the following format: |
| 109 | * {'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] | 110 | * or |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 111 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'get_variable_from_url': <a key from global_variables which contains URL with required content>}, ...} |
| 112 | * or |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 113 | * {'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] | 114 | * or |
| 115 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'get_variable_from_yaml': {'yaml_url': <URL with YAML content>, |
| 116 | * 'yaml_key': <a groovy-interpolating path to the key in the YAML, starting from dot '.'> } }, ...} |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 117 | * or |
| 118 | * {'PARAM_NAME': {'type': <job parameter $class name>, 'use_variables_map': <a nested map of job_parameters>}, ...} |
| 119 | * , where job_parameters may contain a special 'type': '_defaultText' for a Yaml with some additional parameters for this map |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 120 | * @param global_variables Map that keeps the artifact URLs and used 'env' objects: |
| 121 | * {'PARAM1_NAME': <param1 value>, 'PARAM2_NAME': 'http://.../artifacts/param2_value', ...} |
| 122 | */ |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 123 | def generateParameters(job_parameters, global_variables) { |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 124 | def parameters = [] |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 125 | def common = new com.mirantis.mk.Common() |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 126 | def mcpcommon = new com.mirantis.mcp.Common() |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 127 | def http = new com.mirantis.mk.Http() |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 128 | def engine = new groovy.text.GStringTemplateEngine() |
| 129 | def template |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 130 | def yamls_from_urls = [:] |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 131 | def base = [:] |
| 132 | base["url"] = '' |
| 133 | def variable_content |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 134 | def env_variables = common.getEnvAsMap() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 135 | |
| 136 | // Collect required parameters from 'global_variables' or 'env' |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 137 | def _msg = '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 138 | for (param in job_parameters) { |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 139 | if (param.value.containsKey('use_variable')) { |
| 140 | if (!global_variables[param.value.use_variable]) { |
| 141 | global_variables[param.value.use_variable] = env[param.value.use_variable] ?: '' |
| 142 | } |
| 143 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: global_variables[param.value.use_variable]]) |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 144 | _msg += "\n${param.key}: <${param.value.type}> From:${param.value.use_variable}, Value:${global_variables[param.value.use_variable]}" |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 145 | } else if (param.value.containsKey('get_variable_from_url')) { |
| 146 | if (!global_variables[param.value.get_variable_from_url]) { |
| 147 | global_variables[param.value.get_variable_from_url] = env[param.value.get_variable_from_url] ?: '' |
| 148 | } |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 149 | if (global_variables[param.value.get_variable_from_url]) { |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 150 | variable_content = http.restGet(base, global_variables[param.value.get_variable_from_url]) |
| 151 | // http.restGet() attempts to read the response as a JSON, and may return an object instead of a string |
| 152 | variable_content = "${variable_content}".trim() |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 153 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: variable_content]) |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 154 | _msg += "\n${param.key}: <${param.value.type}> Content from url: ${variable_content}" |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 155 | } else { |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 156 | _msg += "\n${param.key} is empty, skipping get_variable_from_url" |
Andrew Baraniuk | e0aef1e | 2019-10-16 14:50:10 +0300 | [diff] [blame] | 157 | } |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 158 | } else if (param.value.containsKey('get_variable_from_yaml')) { |
| 159 | if (param.value.get_variable_from_yaml.containsKey('yaml_url') && param.value.get_variable_from_yaml.containsKey('yaml_key')) { |
| 160 | // 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] | 161 | def yaml_url_var = param.value.get_variable_from_yaml.yaml_url |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 162 | if (!global_variables[yaml_url_var]) { |
| 163 | global_variables[yaml_url_var] = env[yaml_url_var] ?: '' |
| 164 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 165 | def yaml_url = global_variables[yaml_url_var] // Real YAML URL |
| 166 | def yaml_key = param.value.get_variable_from_yaml.yaml_key |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 167 | // Key to get the data from YAML, to interpolate in the groovy, for example: |
| 168 | // <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] | 169 | if (yaml_url) { |
| 170 | if (!yamls_from_urls[yaml_url]) { |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 171 | _msg += "\nReading YAML from ${yaml_url} for ${param.key}" |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 172 | def yaml_content = http.restGet(base, yaml_url) |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 173 | yamls_from_urls[yaml_url] = readYaml text: yaml_content |
| 174 | } |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 175 | _msg += "\nGetting key ${yaml_key} from YAML ${yaml_url} for ${param.key}" |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 176 | def template_variables = [ |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 177 | 'yaml_data': yamls_from_urls[yaml_url], |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 178 | ] |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 179 | def request = "\${yaml_data${yaml_key}}" |
Dennis Dmitriev | 5e07671 | 2022-02-08 15:05:21 +0200 | [diff] [blame] | 180 | def result |
Dennis Dmitriev | 450cf73 | 2021-11-11 14:59:17 +0200 | [diff] [blame] | 181 | // Catch errors related to wrong key or index in the list or map objects |
| 182 | // For wrong key in map or wrong index in list, groovy returns <null> object, |
| 183 | // but it can be catched only after the string interpolation <template.toString()>, |
| 184 | // so we should catch the string 'null' instead of object <null>. |
| 185 | try { |
| 186 | template = engine.createTemplate(request).make(template_variables) |
Dennis Dmitriev | 5e07671 | 2022-02-08 15:05:21 +0200 | [diff] [blame] | 187 | result = template.toString() |
Dennis Dmitriev | 450cf73 | 2021-11-11 14:59:17 +0200 | [diff] [blame] | 188 | if (result == 'null') { |
| 189 | error "No such key or index, got 'null'" |
| 190 | } |
| 191 | } catch (e) { |
| 192 | error("Failed to get the key ${yaml_key} from YAML ${yaml_url}: " + e.toString()) |
| 193 | } |
| 194 | |
| 195 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: result]) |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 196 | _msg += "\n${param.key}: <${param.value.type}>\n${result}" |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 197 | } else { |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 198 | 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] | 199 | } |
| 200 | } else { |
azvyagintsev | 353b876 | 2022-01-14 12:30:43 +0200 | [diff] [blame] | 201 | 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] | 202 | } |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 203 | } else if (param.value.containsKey('use_template')) { |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 204 | template = engine.createTemplate(param.value.use_template.toString()).make(env_variables + global_variables) |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 205 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: template.toString()]) |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 206 | _msg += "\n${param.key}: <${param.value.type}>\n${template.toString()}" |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 207 | } else if (param.value.containsKey('use_variables_map')) { |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 208 | // Generate multistring YAML with key/value pairs (like job_parameters) from a nested parameters map |
| 209 | def nested_parameters = generateParameters(param.value.use_variables_map, global_variables) |
| 210 | def nested_values = [:] |
| 211 | for (_parameter in nested_parameters) { |
| 212 | if (_parameter.$class == '_defaultText') { |
| 213 | // This is a special type for multiline with default values |
| 214 | def _values = readYaml(text: _parameter.value ?: '---') ?: [:] |
| 215 | _values << nested_values |
| 216 | nested_values = _values |
| 217 | } else { |
| 218 | nested_values[_parameter.name] = _parameter.value |
| 219 | } |
| 220 | } |
| 221 | def multistring_value = mcpcommon.dumpYAML(nested_values) |
| 222 | parameters.add([$class: "${param.value.type}", name: "${param.key}", value: multistring_value]) |
| 223 | _msg += "\n${param.key}: <${param.value.type}>\n${multistring_value}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 224 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 225 | } |
azvyagintsev | 2501527 | 2023-11-28 17:31:18 +0200 | [diff] [blame] | 226 | common.infoMsg(_msg) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 227 | return parameters |
| 228 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 229 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * Run a Jenkins job using the collected parameters |
| 233 | * |
| 234 | * @param job_name Name of the running job |
| 235 | * @param job_parameters Map that declares which values from global_variables should be used |
| 236 | * @param global_variables Map that keeps the artifact URLs and used 'env' objects |
| 237 | * @param propagate Boolean. If false: allows to collect artifacts after job is finished, even with FAILURE status |
| 238 | * If true: immediatelly fails the pipeline. DO NOT USE 'true' if you want to collect artifacts |
| 239 | * for 'finally' steps |
| 240 | */ |
| 241 | def runJob(job_name, job_parameters, global_variables, Boolean propagate = false) { |
| 242 | |
| 243 | def parameters = generateParameters(job_parameters, global_variables) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 244 | // Build the job |
Dennis Dmitriev | e09e029 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 245 | def job_info = build job: "${job_name}", parameters: parameters, propagate: propagate |
azvyagintsev | ebe94b4 | 2025-01-11 18:07:41 +0200 | [diff] [blame^] | 246 | // Inject hidden random parameter (is not showed in jjb) to be sure we are triggering unique downstream job. |
| 247 | // Most actual case - parallel run for same jobs( but with different params) |
| 248 | // WARNING: dont move hack to generateParameters: |
| 249 | // PRODX-48965 - it will conflict with si_run_steps logic and will be copy-paste to sub.jobs |
| 250 | String rand_value = "${env.JOB_NAME.toLowerCase()}-${env.BUILD_NUMBER}-${UUID.randomUUID().toString().split('-')[0]}" |
| 251 | parameters.add([$class: "StringParameterValue", |
| 252 | name : "RANDOM_SEED_STRING", |
| 253 | value : rand_value]) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 254 | return job_info |
| 255 | } |
| 256 | |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 257 | def runOrGetJob(job_name, job_parameters, global_variables, propagate, String fullTaskName = '') { |
| 258 | /** |
| 259 | * Run job directly or try to find already executed build |
| 260 | * Flow, in case CI_JOBS_OVERRIDES passed: |
| 261 | * |
| 262 | * |
| 263 | * CI_JOBS_OVERRIDES = text in yaml|json format |
| 264 | * CI_JOBS_OVERRIDES = 'kaas-testing-core-release-artifact' : 3505 |
| 265 | * 'reindex-testing-core-release-index-with-rc' : 2822 |
| 266 | * 'si-test-release-sanity-check-prepare-configuration': 1877 |
| 267 | */ |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 268 | def common = new com.mirantis.mk.Common() |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 269 | def jobsOverrides = readYaml(text: env.CI_JOBS_OVERRIDES ?: '---') ?: [:] |
| 270 | // get id of overriding job |
| 271 | def jobOverrideID = jobsOverrides.getOrDefault(fullTaskName, '') |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 272 | if (fullTaskName in jobsOverrides.keySet()) { |
| 273 | common.warningMsg("Overriding: ${fullTaskName}/${job_name} <<< ${jobOverrideID}") |
| 274 | common.infoMsg("For debug pin use:\n'${fullTaskName}' : ${jobOverrideID}") |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 275 | return Jenkins.instance.getItemByFullName(job_name, hudson.model.Job).getBuildByNumber(jobOverrideID.toInteger()) |
azvyagintsev | 061179d | 2021-05-05 16:52:18 +0300 | [diff] [blame] | 276 | } else { |
| 277 | return runJob(job_name, job_parameters, global_variables, propagate) |
| 278 | } |
| 279 | } |
| 280 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 281 | /** |
| 282 | * Store URLs of the specified artifacts to the global_variables |
| 283 | * |
Andrii Baraniuk | cf4c2fa | 2023-04-18 13:53:32 +0300 | [diff] [blame] | 284 | * @param build_url URL of the completed job |
| 285 | * @param step_artifacts Map that contains artifact names in the job, and variable names |
| 286 | * where the URLs to that atrifacts should be stored, for example: |
| 287 | * {'ARTIFACT1': 'logs.tar.gz', 'ARTIFACT2': 'test_report.xml', ...} |
| 288 | * @param global_variables Map that will keep the artifact URLs. Variable 'ARTIFACT1', for example, |
| 289 | * be used in next job parameters: {'ARTIFACT1_URL':{ 'use_variable': 'ARTIFACT1', ...}} |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 290 | * |
Andrii Baraniuk | cf4c2fa | 2023-04-18 13:53:32 +0300 | [diff] [blame] | 291 | * If the artifact with the specified name not found, the parameter ARTIFACT1_URL |
| 292 | * will be empty. |
| 293 | * @param artifactory_server Artifactory server ID defined in Jenkins config |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 294 | * |
| 295 | */ |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 296 | def storeArtifacts(build_url, step_artifacts, global_variables, job_name, build_num, artifactory_url = '', artifactory_server = '', artifacts_msg='local artifacts') { |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 297 | def common = new com.mirantis.mk.Common() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 298 | def http = new com.mirantis.mk.Http() |
Andrii Baraniuk | cf4c2fa | 2023-04-18 13:53:32 +0300 | [diff] [blame] | 299 | def artifactory = new com.mirantis.mcp.MCPArtifactory() |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 300 | if (!artifactory_url && !artifactory_server) { |
Andrii Baraniuk | cf4c2fa | 2023-04-18 13:53:32 +0300 | [diff] [blame] | 301 | artifactory_url = 'https://artifactory.mcp.mirantis.net/artifactory/api/storage/si-local/jenkins-job-artifacts' |
| 302 | } else if (!artifactory_url && artifactory_server) { |
| 303 | artifactory_url = artifactory.getArtifactoryServer(artifactory_server).getUrl() + '/artifactory/api/storage/si-local/jenkins-job-artifacts' |
Aleksey Zvyagintsev | 25ed4a5 | 2021-05-12 14:35:03 +0000 | [diff] [blame] | 304 | } |
Andrii Baraniuk | cf4c2fa | 2023-04-18 13:53:32 +0300 | [diff] [blame] | 305 | |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 306 | def baseJenkins = [:] |
| 307 | def baseArtifactory = [:] |
| 308 | build_url = build_url.replaceAll(~/\/+$/, "") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 309 | baseArtifactory["url"] = artifactory_url + "/${job_name}/${build_num}" |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 310 | baseJenkins["url"] = build_url |
| 311 | def job_config = http.restGet(baseJenkins, "/api/json/") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 312 | def job_artifacts = job_config['artifacts'] |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 313 | common.infoMsg("Attempt to store ${artifacts_msg} for: ${job_name}/${build_num}") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 314 | for (artifact in step_artifacts) { |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 315 | try { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 316 | def artifactoryResp = http.restGet(baseArtifactory, "/${artifact.value}") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 317 | global_variables[artifact.key] = artifactoryResp.downloadUri |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 318 | common.infoMsg("Artifact URL ${artifactoryResp.downloadUri} stored to ${artifact.key}") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 319 | continue |
| 320 | } catch (Exception e) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 321 | common.warningMsg("Can't find an artifact in ${artifactory_url}/${job_name}/${build_num}/${artifact.value} to store in ${artifact.key}\n" + |
| 322 | "error code ${e.message}") |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 323 | } |
| 324 | |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 325 | 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] | 326 | if (job_artifact.size() == 1) { |
| 327 | // Store artifact URL |
Dmitry Tyzhnenko | f446e41 | 2020-04-06 13:24:54 +0300 | [diff] [blame] | 328 | def artifact_url = "${build_url}/artifact/${job_artifact[0]['relativePath']}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 329 | global_variables[artifact.key] = artifact_url |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 330 | common.infoMsg("Artifact URL ${artifact_url} stored to ${artifact.key}") |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 331 | } else if (job_artifact.size() > 1) { |
| 332 | // Error: too many artifacts with the same name, fail the job |
| 333 | error "Multiple artifacts ${artifact.value} for ${artifact.key} found in the build results ${build_url}, expected one:\n${job_artifact}" |
| 334 | } else { |
| 335 | // Warning: no artifact with expected name |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 336 | 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] | 337 | global_variables[artifact.key] = '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 342 | |
| 343 | def getStatusStyle(status) { |
| 344 | // Styling the status of job result |
| 345 | def status_style = '' |
| 346 | switch (status) { |
| 347 | case "SUCCESS": |
| 348 | status_style = "<td style='color: green;'><img src='/images/16x16/blue.png' alt='SUCCESS'>" |
| 349 | break |
| 350 | case "UNSTABLE": |
| 351 | status_style = "<td style='color: #FF5733;'><img src='/images/16x16/yellow.png' alt='UNSTABLE'>" |
| 352 | break |
| 353 | case "ABORTED": |
| 354 | status_style = "<td style='color: red;'><img src='/images/16x16/aborted.png' alt='ABORTED'>" |
| 355 | break |
| 356 | case "NOT_BUILT": |
| 357 | status_style = "<td style='color: red;'><img src='/images/16x16/aborted.png' alt='NOT_BUILT'>" |
| 358 | break |
| 359 | case "FAILURE": |
| 360 | status_style = "<td style='color: red;'><img src='/images/16x16/red.png' alt='FAILURE'>" |
| 361 | break |
| 362 | default: |
| 363 | status_style = "<td>-" |
| 364 | } |
| 365 | return status_style |
| 366 | } |
| 367 | |
| 368 | |
| 369 | def getTrStyle(jobdata) { |
| 370 | def trstyle = "<tr>" |
| 371 | // Grey background for 'finally' jobs in list |
| 372 | if (jobdata.getOrDefault('type', '') == 'finally') { |
| 373 | trstyle = "<tr style='background: #DDDDDD;'>" |
| 374 | } |
| 375 | return trstyle |
| 376 | } |
| 377 | |
| 378 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 379 | /** |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 380 | * Update a 'job' step description |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 381 | * |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 382 | * @param jobsdata Map with a 'job' step details and status |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 383 | */ |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 384 | def getJobDescription(jobdata) { |
| 385 | def trstyle = getTrStyle(jobdata) |
| 386 | def display_name = jobdata['desc'] ? "${jobdata['desc']}: ${jobdata['build_id']}" : "${jobdata['name']}: ${jobdata['build_id']}" |
| 387 | if ((env.WF_SHOW_FULL_WORKFLOW_DESCRIPTION ?: false).toBoolean()) { |
| 388 | display_name = "[${jobdata['name']}/${jobdata['build_id']}]: ${jobdata['desc']}" |
| 389 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 390 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 391 | // Attach url for already built jobs |
| 392 | def build_url = display_name |
| 393 | if (jobdata['build_url'] != "0") { |
| 394 | build_url = "<a href=${jobdata['build_url']}>$display_name</a>" |
| 395 | } |
| 396 | |
| 397 | def status_style = getStatusStyle(jobdata['status'].toString()) |
| 398 | |
| 399 | return [[trstyle, build_url, jobdata['duration'], status_style,],] |
| 400 | } |
| 401 | |
| 402 | |
| 403 | /** |
| 404 | * Update a 'script' step description |
| 405 | * |
| 406 | * @param jobsdata Map with a 'script' step details and status |
| 407 | */ |
| 408 | def getScriptDescription(jobdata) { |
| 409 | def trstyle = getTrStyle(jobdata) |
| 410 | |
| 411 | def display_name = "${jobdata['desc']}" ?: "${jobdata['name']}" |
| 412 | if ((env.WF_SHOW_FULL_WORKFLOW_DESCRIPTION ?: false).toBoolean()) { |
| 413 | display_name = "[${jobdata['name']}]: ${jobdata['desc']}" |
| 414 | } |
| 415 | |
| 416 | // Attach url for already built jobs |
| 417 | def build_url = display_name |
| 418 | if (jobdata['build_url'] != "0") { |
| 419 | build_url = "<a href=${jobdata['build_url']}>$display_name</a>" |
| 420 | } |
| 421 | |
| 422 | def status_style = getStatusStyle(jobdata['status'].toString()) |
| 423 | |
| 424 | return [[trstyle, build_url, jobdata['duration'], status_style,],] |
| 425 | } |
| 426 | |
| 427 | |
| 428 | /** |
| 429 | * Update a 'parallel' or a 'sequence' step description |
| 430 | * |
| 431 | * @param jobsdata Map with a 'together' step details and statuses |
| 432 | */ |
| 433 | def getNestedDescription(jobdata) { |
| 434 | def tableEntries = [] |
| 435 | def trstyle = getTrStyle(jobdata) |
| 436 | |
| 437 | def display_name = "${jobdata['desc']}" ?: "${jobdata['name']}" |
| 438 | if ((env.WF_SHOW_FULL_WORKFLOW_DESCRIPTION ?: false).toBoolean()) { |
| 439 | display_name = "[${jobdata['name']}]: ${jobdata['desc']}" |
| 440 | } |
| 441 | |
| 442 | // Attach url for already built jobs |
| 443 | def build_url = display_name |
| 444 | if (jobdata['build_url'] != "0") { |
| 445 | build_url = "<a href=${jobdata['build_url']}>$display_name</a>" |
| 446 | } |
| 447 | |
| 448 | def status_style = getStatusStyle(jobdata['status'].toString()) |
| 449 | |
| 450 | tableEntries += [[trstyle, build_url, jobdata['duration'], status_style,],] |
| 451 | |
| 452 | // Collect nested job descriptions |
| 453 | for (nested_jobdata in jobdata['nested_steps_data']) { |
| 454 | (nestedTableEntries, _) = getStepDescription(nested_jobdata.value) |
| 455 | for (nestedTableEntry in nestedTableEntries) { |
| 456 | (nested_trstyle, nested_display_name, nested_duration, nested_status_style) = nestedTableEntry |
| 457 | tableEntries += [[nested_trstyle, " | ${nested_jobdata.key}: ${nested_display_name}", nested_duration, nested_status_style,],] |
| 458 | } |
| 459 | } |
| 460 | return tableEntries |
| 461 | } |
| 462 | |
| 463 | |
| 464 | def getStepDescription(jobs_data) { |
| 465 | def tableEntries = [] |
| 466 | def child_jobs_description = '' |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 467 | for (jobdata in jobs_data) { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 468 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 469 | if (jobdata['step_key'] == 'job') { |
| 470 | tableEntries += getJobDescription(jobdata) |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 471 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 472 | else if (jobdata['step_key'] == 'script') { |
| 473 | tableEntries += getScriptDescription(jobdata) |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 474 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 475 | else if (jobdata['step_key'] == 'parallel' || jobdata['step_key'] == 'sequence') { |
| 476 | tableEntries += getNestedDescription(jobdata) |
| 477 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 478 | |
| 479 | // Collecting descriptions of builded child jobs |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 480 | if (jobdata['child_desc'] != '') { |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 481 | 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] | 482 | // remove "null" message-result from description, but leave XXX:JOBRESULT in description |
azvyagintsev | 8b8224d | 2023-10-06 20:52:26 +0300 | [diff] [blame] | 483 | if (jobdata['child_desc'] != 'null') { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 484 | child_jobs_description += "<small>${jobdata['child_desc']}</small><br>" |
| 485 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 486 | } |
| 487 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 488 | return [tableEntries, child_jobs_description] |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Update description for workflow steps |
| 493 | * |
| 494 | * @param jobs_data Map with all step names and result statuses, to showing it in description |
| 495 | */ |
| 496 | def updateDescription(jobs_data) { |
| 497 | def child_jobs_description = '<strong>Descriptions from jobs:</strong><br>' |
| 498 | def table_template_start = "<div><table style='border: solid 1px;'><tr><th>Job:</th><th>Duration:</th><th>Status:</th></tr>" |
| 499 | def table_template_end = "</table></div>" |
| 500 | |
| 501 | (tableEntries, _child_jobs_description) = getStepDescription(jobs_data) |
| 502 | |
| 503 | def table = '' |
| 504 | for (tableEntry in tableEntries) { |
| 505 | // Collect table |
| 506 | (trstyle, display_name, duration, status_style) = tableEntry |
| 507 | table += "${trstyle}<td>${display_name}</td><td>${duration}</td>${status_style}</td></tr>" |
| 508 | } |
| 509 | |
| 510 | child_jobs_description += _child_jobs_description |
| 511 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 512 | currentBuild.description = table_template_start + table + table_template_end + child_jobs_description |
| 513 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 514 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 515 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 516 | def runStep(global_variables, step, Boolean propagate = false, artifactoryBaseUrl = '', artifactoryServer = '', parent_global_variables=null) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 517 | return { |
| 518 | def common = new com.mirantis.mk.Common() |
| 519 | def engine = new groovy.text.GStringTemplateEngine() |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 520 | def env_variables = common.getEnvAsMap() |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 521 | |
| 522 | String jobDescription = step['description'] ?: '' |
| 523 | def jobName = step['job'] |
| 524 | def jobParameters = [:] |
| 525 | def stepParameters = step['parameters'] ?: [:] |
| 526 | if (step['inherit_parent_params'] ?: false) { |
| 527 | // add parameters from the current job for the child job |
| 528 | jobParameters << getJobDefaultParameters(env.JOB_NAME) |
| 529 | } |
| 530 | // add parameters from the workflow for the child job |
| 531 | jobParameters << stepParameters |
| 532 | def wfPauseStepBeforeRun = (step['wf_pause_step_before_run'] ?: false).toBoolean() |
| 533 | def wfPauseStepTimeout = (step['wf_pause_step_timeout'] ?: 10).toInteger() |
| 534 | def wfPauseStepSlackReportChannel = step['wf_pause_step_slack_report_channel'] ?: '' |
| 535 | |
| 536 | if (wfPauseStepBeforeRun) { |
| 537 | // Try-catch construction will allow to continue Steps, if timeout reached |
| 538 | try { |
| 539 | if (wfPauseStepSlackReportChannel) { |
| 540 | def slack = new com.mirantis.mcp.SlackNotification() |
azvyagintsev | da22aa8 | 2022-06-10 15:46:55 +0300 | [diff] [blame] | 541 | wfPauseStepSlackReportChannel.split(',').each { |
| 542 | slack.jobResultNotification('wf_pause_step_before_run', |
| 543 | it.toString(), |
| 544 | env.JOB_NAME, null, |
| 545 | env.BUILD_URL, 'slack_webhook_url') |
| 546 | } |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 547 | } |
| 548 | timeout(time: wfPauseStepTimeout, unit: 'MINUTES') { |
| 549 | input("Workflow pause requested before run: ${jobName}/${jobDescription}\n" + |
| 550 | "Timeout set to ${wfPauseStepTimeout}.\n" + |
| 551 | "Do you want to proceed workflow?") |
| 552 | } |
| 553 | } catch (err) { // timeout reached or input false |
Sergey Lalov | a89e16a | 2024-10-17 20:26:44 +0400 | [diff] [blame] | 554 | def cause = err.getCauses().get(0) |
| 555 | if (cause instanceof org.jenkinsci.plugins.workflow.steps.TimeoutStepExecution.ExceededTimeout) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 556 | common.infoMsg("Timeout finished, continue..") |
Sergey Lalov | a89e16a | 2024-10-17 20:26:44 +0400 | [diff] [blame] | 557 | } else { |
| 558 | def user = causes[0].getUser() |
| 559 | error("Aborted after workflow pause by: [${user}]") |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | } |
| 563 | common.infoMsg("Attempt to run: ${jobName}/${jobDescription}") |
| 564 | // Collect job parameters and run the job |
| 565 | // WARN(alexz): desc must not contain invalid chars for yaml |
| 566 | def jobResult = runOrGetJob(jobName, jobParameters, |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 567 | global_variables, propagate, jobDescription) |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 568 | def buildDuration = jobResult.durationString ?: '-' |
| 569 | if (buildDuration.toString() == null) { |
| 570 | buildDuration = '-' |
| 571 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 572 | def desc = engine.createTemplate(jobDescription.toString()).make(env_variables + global_variables) |
| 573 | if ((desc.toString() == '') || (desc.toString() == 'null')) { |
| 574 | desc = '' |
| 575 | } |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 576 | def jobSummary = [ |
| 577 | job_result : jobResult.getResult().toString(), |
| 578 | build_url : jobResult.getAbsoluteUrl().toString(), |
| 579 | build_id : jobResult.getId().toString(), |
| 580 | buildDuration : buildDuration, |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 581 | desc : desc, |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 582 | ] |
| 583 | def _buildDescription = jobResult.getDescription().toString() |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 584 | if (_buildDescription) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 585 | jobSummary['build_description'] = _buildDescription |
| 586 | } |
| 587 | // Store links to the resulting artifacts into 'global_variables' |
| 588 | storeArtifacts(jobSummary['build_url'], step['artifacts'], |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 589 | global_variables, jobName, jobSummary['build_id'], artifactoryBaseUrl, artifactoryServer, artifacts_msg='artifacts to local variables') |
| 590 | // Store links to the resulting 'global_artifacts' into 'global_variables' |
| 591 | storeArtifacts(jobSummary['build_url'], step['global_artifacts'], |
| 592 | global_variables, jobName, jobSummary['build_id'], artifactoryBaseUrl, artifactoryServer, artifacts_msg='global_artifacts to local variables') |
| 593 | // Store links to the resulting 'global_artifacts' into 'parent_global_variables' |
| 594 | storeArtifacts(jobSummary['build_url'], step['global_artifacts'], |
| 595 | parent_global_variables, jobName, jobSummary['build_id'], artifactoryBaseUrl, artifactoryServer, artifacts_msg='global_artifacts to global_variables') |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 596 | return jobSummary |
| 597 | } |
| 598 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 599 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 600 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 601 | def runScript(global_variables, step, artifactoryBaseUrl = '', artifactoryServer = '', scriptsLibrary = null, parent_global_variables=null) { |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 602 | def common = new com.mirantis.mk.Common() |
| 603 | def env_variables = common.getEnvAsMap() |
| 604 | |
| 605 | if (!scriptsLibrary) { |
| 606 | error "'scriptsLibrary' argument is not provided to load a script object '${step['script']}' from that library" |
| 607 | } |
| 608 | // Evaluate the object from it's name, for example: scriptsLibrary.com.mirantis.si.runtime_steps.ParallelMkeMoskUpgradeSequences |
| 609 | def scriptObj = scriptsLibrary |
| 610 | for (sObj in step['script'].split("\\.")) { |
| 611 | scriptObj = scriptObj."$sObj" |
| 612 | } |
| 613 | |
| 614 | def script = scriptObj.new() |
| 615 | |
| 616 | def scriptSummary = [ |
| 617 | job_result : '', |
| 618 | desc : step['description'] ?: '', |
| 619 | ] |
| 620 | |
| 621 | // prepare 'script_env' from merged 'env' and script step parameters |
| 622 | def script_env = env_variables.clone() |
| 623 | def stepParameters = step['parameters'] ?: [:] |
| 624 | def script_parameters = generateParameters(stepParameters, global_variables) |
| 625 | println "${script_parameters}" |
| 626 | for (script_parameter in script_parameters) { |
| 627 | common.infoMsg("Updating script env['${script_parameter.name}'] with value: ${script_parameter.value}") |
| 628 | script_env[script_parameter.name] = script_parameter.value |
| 629 | } |
| 630 | |
| 631 | try { |
| 632 | script.main(this, script_env) |
| 633 | scriptSummary['script_result'] = 'SUCCESS' |
| 634 | } catch (InterruptedException e) { |
| 635 | scriptSummary['script_result'] = 'ABORTED' |
| 636 | printStackTrace(e) |
| 637 | } catch (e) { |
| 638 | scriptSummary['script_result'] = 'FAILURE' |
| 639 | printStackTrace(e) |
| 640 | } |
| 641 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 642 | // Store links to the resulting 'artifacts' into 'global_variables' |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 643 | storeArtifacts(env.BUILD_URL, step['artifacts'], |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 644 | global_variables, env.JOB_NAME, env.BUILD_NUMBER, artifactoryBaseUrl, artifactoryServer, artifacts_msg='artifacts to local variables') |
| 645 | // Store links to the resulting 'global_artifacts' into 'global_variables' |
| 646 | storeArtifacts(env.BUILD_URL, step['global_artifacts'], |
| 647 | global_variables, env.JOB_NAME, env.BUILD_NUMBER, artifactoryBaseUrl, artifactoryServer, artifacts_msg='global_artifacts to local variables') |
| 648 | // Store links to the resulting 'global_artifacts' into 'parent_global_variables' |
| 649 | storeArtifacts(env.BUILD_URL, step['global_artifacts'], |
| 650 | parent_global_variables, env.JOB_NAME, env.BUILD_NUMBER, artifactoryBaseUrl, artifactoryServer, artifacts_msg='global_artifacts to global_variables') |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 651 | |
| 652 | return scriptSummary |
| 653 | } |
| 654 | |
| 655 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 656 | def runParallel(global_variables, step, failed_jobs, global_jobs_data, nested_steps_data, artifactoryBaseUrl = '', artifactoryServer = '', scriptsLibrary = null, prefixMsg = '', parent_global_variables=null) { |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 657 | // Run the specified steps in parallel |
| 658 | // Repeat the steps for each parameters set from 'repeat_with_parameters_from_yaml' |
| 659 | // If 'repeat_with_parameters_from_yaml' is not provided, then 'parallel' step will perform just one iteration for a default "- _FOO: _BAR" parameter |
| 660 | // If 'repeat_with_parameters_from_yaml' is present, but the specified artifact contains empty list '[]', then 'parallel' step will be skipped |
| 661 | // Example: |
| 662 | // - parallel: |
| 663 | // - job: |
| 664 | // - job: |
| 665 | // - sequence: |
| 666 | // repeat_with_parameters_from_yaml: |
| 667 | // type: TextParameterValue |
| 668 | // get_variable_from_url: SI_PARALLEL_PARAMETERS |
| 669 | // max_concurrent: 2 # how many parallel jobs shold be run at the same time |
| 670 | // max_concurrent_interval: 300 # how many seconds should be passed between checking for an available concurrency |
| 671 | // check_failed_concurrent: false # stop waiting for available concurrent executors if count of failed jobs >= max_concurrent, |
| 672 | // # which means that all available shared resources are occupied by the failed jobs |
azvyagintsev | 8bf15d7 | 2024-09-19 14:43:33 +0300 | [diff] [blame] | 673 | // abort_on_parallel_fail: false # pass parallel.fail_fast option. force your parallel stages to all be aborted when any one of them fails |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 674 | def common = new com.mirantis.mk.Common() |
| 675 | |
| 676 | def sourceText = "" |
| 677 | def defaultSourceText = "- _FOO: _BAR" |
| 678 | if (step['repeat_with_parameters_from_yaml']) { |
| 679 | def sourceParameter = ["repeat_with_parameters_from_yaml": step['repeat_with_parameters_from_yaml']] |
| 680 | for (parameter in generateParameters(sourceParameter, global_variables)) { |
| 681 | if (parameter.name == "repeat_with_parameters_from_yaml") { |
| 682 | sourceText = parameter.value |
| 683 | common.infoMsg("'repeat_with_parameters_from_yaml' is defined, using it as a yaml text:\n${sourceText}") |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | if (!sourceText) { |
| 688 | sourceText = defaultSourceText |
| 689 | common.warningMsg("'repeat_with_parameters_from_yaml' is not defined. To get one iteration, use default single entry:\n${sourceText}") |
| 690 | } |
| 691 | def iterateParametersList = readYaml text: sourceText |
| 692 | if (!(iterateParametersList instanceof List)) { |
| 693 | // Stop the pipeline if there is wrong parameters data type, to not generate parallel jobs for wrong data |
| 694 | error "Expected a List in 'repeat_with_parameters_from_yaml' for 'parallel' step, but got:\n${sourceText}" |
| 695 | } |
| 696 | |
| 697 | // Limit the maximum steps in parallel at the same time |
| 698 | def max_concurrent = (step['max_concurrent'] ?: 100).toInteger() |
| 699 | // Sleep for the specified amount of time until a free thread will be available |
| 700 | def max_concurrent_interval = (step['max_concurrent_interval'] ?: 600).toInteger() |
| 701 | // Check that failed jobs is not >= free executors. if 'true', then don't wait for free executors, fail the parallel step |
| 702 | def check_failed_concurrent = (step['check_failed_concurrent'] ?: false).toBoolean() |
| 703 | |
| 704 | def jobs = [:] |
azvyagintsev | 8bf15d7 | 2024-09-19 14:43:33 +0300 | [diff] [blame] | 705 | jobs.failFast = (step['abort_on_parallel_fail'] ?: false).toBoolean() |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 706 | def nested_step_id = 0 |
| 707 | def free_concurrent = max_concurrent |
| 708 | def failed_concurrent = [] |
| 709 | |
| 710 | common.printMsg("${prefixMsg} Running parallel steps with the following parameters:\n${iterateParametersList}", "purple") |
| 711 | |
| 712 | for (parameters in iterateParametersList) { |
| 713 | for (parallel_step in step['parallel']) { |
| 714 | def step_name = "parallel#${nested_step_id}" |
| 715 | def nested_step = parallel_step |
| 716 | def nested_step_name = step_name |
| 717 | def nested_prefix_name = "${prefixMsg}${nested_step_name} | " |
| 718 | |
| 719 | nested_steps_data[step_name] = [] |
| 720 | prepareJobsData([nested_step,], 'parallel', nested_steps_data[step_name]) |
| 721 | |
| 722 | //Copy global variables and merge "parameters" dict into it for the current particular step |
| 723 | def nested_global_variables = global_variables.clone() |
| 724 | nested_global_variables << parameters |
| 725 | |
| 726 | jobs[step_name] = { |
| 727 | // initialRecurrencePeriod in milliseconds |
| 728 | waitUntil(initialRecurrencePeriod: 1500, quiet: true) { |
| 729 | if (check_failed_concurrent) { |
| 730 | if (failed_concurrent.size() >= max_concurrent){ |
| 731 | common.errorMsg("Failed jobs count is equal max_concurrent value ${max_concurrent}. Will not continue because resources are consumed") |
| 732 | error("max_concurrent == failed_concurrent") |
| 733 | } |
| 734 | } |
| 735 | if (free_concurrent > 0) { |
| 736 | free_concurrent-- |
| 737 | true |
| 738 | } else { |
| 739 | sleep(max_concurrent_interval) |
| 740 | false |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | try { |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 745 | runWorkflowStep(nested_global_variables, nested_step, 0, nested_steps_data[nested_step_name], global_jobs_data, failed_jobs, false, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, nested_prefix_name, parent_global_variables) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 746 | } |
| 747 | catch (e) { |
| 748 | failed_concurrent.add(step_name) |
| 749 | throw(e) |
| 750 | } |
| 751 | |
| 752 | free_concurrent++ |
| 753 | } // 'jobs' closure |
| 754 | |
| 755 | nested_step_id++ |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | def parallelSummary = [ |
| 760 | nested_result : '', |
| 761 | desc : step['description'] ?: '', |
| 762 | nested_steps_data : [:], |
| 763 | ] |
| 764 | |
| 765 | if (iterateParametersList) { |
| 766 | // Run parallel iterations |
| 767 | try { |
| 768 | common.infoMsg("${prefixMsg} Run steps in parallel") |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 769 | parallel jobs |
| 770 | |
| 771 | parallelSummary['nested_result'] = 'SUCCESS' |
| 772 | } catch (InterruptedException e) { |
| 773 | parallelSummary['nested_result'] = 'ABORTED' |
| 774 | printStackTrace(e) |
| 775 | } catch (e) { |
| 776 | parallelSummary['nested_result'] = 'FAILURE' |
| 777 | printStackTrace(e) |
| 778 | } |
| 779 | parallelSummary['nested_steps_data'] = nested_steps_data |
| 780 | } |
| 781 | else |
| 782 | { |
| 783 | // No parameters were provided to iterate |
| 784 | common.errorMsg("${prefixMsg} No parameters were provided to iterate, skipping 'parallel' step") |
| 785 | parallelSummary['nested_result'] = 'SUCCESS' |
| 786 | } |
| 787 | return parallelSummary |
| 788 | } |
| 789 | |
| 790 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 791 | def runSequence(global_variables, step, failed_jobs, global_jobs_data, nested_steps_data, artifactoryBaseUrl = '', artifactoryServer = '', scriptsLibrary = null, prefixMsg = '', parent_global_variables=null) { |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 792 | // Run the steps in the specified order, like in main workflow, but repeat the sequence for each parameters set from 'repeat_with_parameters_from_yaml' |
| 793 | // If 'repeat_with_parameters_from_yaml' is not provided, then 'sequence' step will perform just one iteration for a default "- _FOO: _BAR" parameter |
| 794 | // If 'repeat_with_parameters_from_yaml' is present, but the specified artifact contains empty list '[]', then 'sequence' step will be skipped |
| 795 | // - sequence: |
| 796 | // - job: |
| 797 | // - job: |
| 798 | // - script: |
| 799 | // repeat_with_parameters_from_yaml: |
| 800 | // type: TextParameterValue |
| 801 | // get_variable_from_url: SI_PARALLEL_PARAMETERS |
| 802 | def common = new com.mirantis.mk.Common() |
| 803 | |
| 804 | def sourceText = "" |
| 805 | def defaultSourceText = "- _FOO: _BAR" |
| 806 | if (step['repeat_with_parameters_from_yaml']) { |
| 807 | def sourceParameter = ["repeat_with_parameters_from_yaml": step['repeat_with_parameters_from_yaml']] |
| 808 | for (parameter in generateParameters(sourceParameter, global_variables)) { |
| 809 | if (parameter.name == "repeat_with_parameters_from_yaml") { |
| 810 | sourceText = parameter.value |
| 811 | common.infoMsg("'repeat_with_parameters_from_yaml' is defined, using it as a yaml text:\n${sourceText}") |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | if (!sourceText) { |
| 816 | sourceText = defaultSourceText |
| 817 | common.warningMsg("'repeat_with_parameters_from_yaml' is not defined. To get one iteration, use default single entry:\n${sourceText}") |
| 818 | } |
| 819 | def iterateParametersList = readYaml text: sourceText |
| 820 | if (!(iterateParametersList instanceof List)) { |
| 821 | // Stop the pipeline if there is wrong parameters data type, to not generate parallel jobs for wrong data |
| 822 | error "Expected a List in 'repeat_with_parameters_from_yaml' for 'sequence' step, but got:\n${sourceText}" |
| 823 | } |
| 824 | |
| 825 | def jobs = [:] |
| 826 | def nested_step_id = 0 |
| 827 | |
| 828 | common.printMsg("${prefixMsg} Running parallel steps with the following parameters:\n${iterateParametersList}", "purple") |
| 829 | |
| 830 | for (parameters in iterateParametersList) { |
| 831 | def step_name = "sequence#${nested_step_id}" |
| 832 | def nested_steps = step['sequence'] |
| 833 | def nested_step_name = step_name |
| 834 | def nested_prefix_name = "${prefixMsg}${nested_step_name} | " |
| 835 | |
| 836 | nested_steps_data[step_name] = [] |
| 837 | prepareJobsData(nested_steps, 'sequence', nested_steps_data[step_name]) |
| 838 | |
| 839 | //Copy global variables and merge "parameters" dict into it for the current particular step |
| 840 | def nested_global_variables = global_variables.clone() |
| 841 | nested_global_variables << parameters |
| 842 | |
| 843 | jobs[step_name] = { |
| 844 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 845 | runSteps(nested_steps, nested_global_variables, failed_jobs, nested_steps_data[nested_step_name], global_jobs_data, 0, false, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, nested_prefix_name, parent_global_variables) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 846 | |
| 847 | } // 'jobs' closure |
| 848 | |
| 849 | nested_step_id++ |
| 850 | } |
| 851 | |
| 852 | def sequenceSummary = [ |
| 853 | nested_result : '', |
| 854 | desc : step['description'] ?: '', |
| 855 | nested_steps_data : [:], |
| 856 | ] |
| 857 | |
| 858 | if (iterateParametersList) { |
| 859 | // Run sequence iterations |
| 860 | try { |
| 861 | jobs.each { stepName, job -> |
| 862 | common.infoMsg("${prefixMsg} Running sequence ${stepName}") |
| 863 | job() |
azvyagintsev | d5f0512 | 2024-09-21 13:00:16 +0300 | [diff] [blame] | 864 | // just in case sleep. |
| 865 | sleep(5) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 866 | } |
| 867 | sequenceSummary['nested_result'] = 'SUCCESS' |
| 868 | } catch (InterruptedException e) { |
| 869 | sequenceSummary['nested_result'] = 'ABORTED' |
| 870 | printStackTrace(e) |
| 871 | } catch (e) { |
| 872 | sequenceSummary['nested_result'] = 'FAILURE' |
| 873 | printStackTrace(e) |
| 874 | } |
| 875 | sequenceSummary['nested_steps_data'] = nested_steps_data |
| 876 | } |
| 877 | else |
| 878 | { |
| 879 | // No parameters were provided to iterate |
| 880 | common.errorMsg("${prefixMsg} No parameters were provided to iterate, skipping 'sequence' step") |
| 881 | sequenceSummary['nested_result'] = 'SUCCESS' |
| 882 | } |
| 883 | |
| 884 | return sequenceSummary |
| 885 | } |
| 886 | |
| 887 | |
| 888 | def checkResult(job_result, build_url, step, failed_jobs) { |
| 889 | // Check job result, in case of SUCCESS, move to next step. |
| 890 | // In case job has status NOT_BUILT, fail the build or keep going depending on 'ignore_not_built' flag |
| 891 | // In other cases check flag ignore_failed, if true ignore any statuses and keep going additionally |
| 892 | // if skip_results is not set or set to false fail entrie workflow, otherwise succed. |
| 893 | if (job_result != 'SUCCESS') { |
| 894 | def ignoreStepResult = false |
| 895 | switch (job_result) { |
| 896 | // In cases when job was waiting too long in queue or internal job logic allows to skip building, |
| 897 | // job may have NOT_BUILT status. In that case ignore_not_built flag can be used not to fail scenario. |
| 898 | case "NOT_BUILT": |
| 899 | ignoreStepResult = step['ignore_not_built'] ?: false |
| 900 | break |
| 901 | case "UNSTABLE": |
| 902 | ignoreStepResult = step['ignore_unstable'] ?: (step['ignore_failed'] ?: false) |
| 903 | if (ignoreStepResult && !step['skip_results'] ?: false) { |
| 904 | failed_jobs[build_url] = job_result |
| 905 | } |
| 906 | break |
azvyagintsev | e012e41 | 2024-05-22 16:09:23 +0300 | [diff] [blame] | 907 | case "ABORTED": |
| 908 | ignoreStepResult = step['ignore_aborted'] ?: (step['ignore_failed'] ?: false) |
| 909 | if (ignoreStepResult && !step['skip_results'] ?: false) { |
| 910 | failed_jobs[build_url] = job_result |
| 911 | } |
| 912 | break |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 913 | default: |
| 914 | ignoreStepResult = step['ignore_failed'] ?: false |
| 915 | if (ignoreStepResult && !step['skip_results'] ?: false) { |
| 916 | failed_jobs[build_url] = job_result |
| 917 | } |
| 918 | } |
| 919 | if (!ignoreStepResult) { |
| 920 | currentBuild.result = job_result |
| 921 | error "Job ${build_url} finished with result: ${job_result}" |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 926 | def runWorkflowStep(global_variables, step, step_id, jobs_data, global_jobs_data, failed_jobs, propagate, artifactoryBaseUrl, artifactoryServer, scriptsLibrary = null, prefixMsg = '', parent_global_variables=null) { |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 927 | def common = new com.mirantis.mk.Common() |
| 928 | |
| 929 | def _sep = "\n======================\n" |
| 930 | if (step.containsKey('job')) { |
| 931 | |
| 932 | common.printMsg("${_sep}${prefixMsg}Run job ${step['job']} [at ${java.time.LocalDateTime.now()}]${_sep}", "blue") |
| 933 | stage("Run job ${step['job']}") { |
| 934 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 935 | def job_summary = runStep(global_variables, step, propagate, artifactoryBaseUrl, artifactoryServer, parent_global_variables).call() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 936 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 937 | // Update jobs_data for updating description |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 938 | jobs_data[step_id]['build_url'] = job_summary['build_url'] |
| 939 | jobs_data[step_id]['build_id'] = job_summary['build_id'] |
| 940 | jobs_data[step_id]['status'] = job_summary['job_result'] |
| 941 | jobs_data[step_id]['duration'] = job_summary['buildDuration'] |
| 942 | jobs_data[step_id]['desc'] = job_summary['desc'] |
| 943 | if (job_summary['build_description']) { |
| 944 | jobs_data[step_id]['child_desc'] = job_summary['build_description'] |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 945 | } |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 946 | def job_result = job_summary['job_result'] |
| 947 | def build_url = job_summary['build_url'] |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 948 | common.printMsg("${_sep}${prefixMsg}Job ${build_url} finished with result: ${job_result} [at ${java.time.LocalDateTime.now()}]${_sep}", "blue") |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 949 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 950 | } |
| 951 | else if (step.containsKey('script')) { |
| 952 | common.printMsg("${_sep}${prefixMsg}Run script ${step['script']} [at ${java.time.LocalDateTime.now()}]${_sep}", "blue") |
| 953 | stage("Run script ${step['script']}") { |
| 954 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 955 | def scriptResult = runScript(global_variables, step, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, parent_global_variables) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 956 | |
| 957 | // Use build_url just as an unique key for failed_jobs. |
| 958 | // All characters after '#' are 'comment' |
| 959 | def build_url = "${env.BUILD_URL}#${step_id}:${step['script']}" |
| 960 | def job_result = scriptResult['script_result'] |
| 961 | common.printMsg("${_sep}${prefixMsg}Script ${build_url} finished with result: ${job_result} [at ${java.time.LocalDateTime.now()}]${_sep}", "blue") |
| 962 | |
| 963 | jobs_data[step_id]['build_url'] = build_url |
| 964 | jobs_data[step_id]['status'] = scriptResult['script_result'] |
| 965 | jobs_data[step_id]['desc'] = scriptResult['desc'] |
| 966 | if (scriptResult['build_description']) { |
| 967 | jobs_data[step_id]['child_desc'] = scriptResult['build_description'] |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | else if (step.containsKey('parallel')) { |
| 972 | common.printMsg("${_sep}${prefixMsg}Run steps in parallel [at ${java.time.LocalDateTime.now()}]:${_sep}", "blue") |
| 973 | stage("Run steps in parallel:") { |
| 974 | |
| 975 | // Allocate a map to collect nested steps data for updateDescription() |
| 976 | def nested_steps_data = [:] |
| 977 | jobs_data[step_id]['nested_steps_data'] = nested_steps_data |
| 978 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 979 | def parallelResult = runParallel(global_variables, step, failed_jobs, global_jobs_data, nested_steps_data, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, prefixMsg, parent_global_variables) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 980 | |
| 981 | // Use build_url just as an unique key for failed_jobs. |
| 982 | // All characters after '#' are 'comment' |
| 983 | def build_url = "${env.BUILD_URL}#${step_id}" |
| 984 | def job_result = parallelResult['nested_result'] |
| 985 | common.printMsg("${_sep}${prefixMsg}Parallel steps ${build_url} finished with result: ${job_result} [at ${java.time.LocalDateTime.now()}]${_sep}", "blue") |
| 986 | |
| 987 | jobs_data[step_id]['build_url'] = build_url |
| 988 | jobs_data[step_id]['status'] = parallelResult['nested_result'] |
| 989 | jobs_data[step_id]['desc'] = parallelResult['desc'] |
| 990 | if (parallelResult['build_description']) { |
| 991 | jobs_data[step_id]['child_desc'] = parallelResult['build_description'] |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | else if (step.containsKey('sequence')) { |
| 996 | common.printMsg("${_sep}${prefixMsg}Run steps in sequence [at ${java.time.LocalDateTime.now()}]:${_sep}", "blue") |
| 997 | stage("Run steps in sequence:") { |
| 998 | |
| 999 | // Allocate a map to collect nested steps data for updateDescription() |
| 1000 | def nested_steps_data = [:] |
| 1001 | jobs_data[step_id]['nested_steps_data'] = nested_steps_data |
| 1002 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 1003 | def sequenceResult = runSequence(global_variables, step, failed_jobs, global_jobs_data, nested_steps_data, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, prefixMsg, parent_global_variables) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1004 | |
| 1005 | // Use build_url just as an unique key for failed_jobs. |
| 1006 | // All characters after '#' are 'comment' |
| 1007 | def build_url = "${env.BUILD_URL}#${step_id}" |
| 1008 | def job_result = sequenceResult['nested_result'] |
| 1009 | common.printMsg("${_sep}${prefixMsg}Sequence steps ${build_url} finished with result: ${job_result} [at ${java.time.LocalDateTime.now()}]${_sep}", "blue") |
| 1010 | |
| 1011 | jobs_data[step_id]['build_url'] = build_url |
| 1012 | jobs_data[step_id]['status'] = sequenceResult['nested_result'] |
| 1013 | jobs_data[step_id]['desc'] = sequenceResult['desc'] |
| 1014 | if (sequenceResult['build_description']) { |
| 1015 | jobs_data[step_id]['child_desc'] = sequenceResult['build_description'] |
| 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | updateDescription(global_jobs_data) |
| 1021 | |
| 1022 | job_result = jobs_data[step_id]['status'] |
| 1023 | checkResult(job_result, build_url, step, failed_jobs) |
| 1024 | |
| 1025 | // return build_url |
| 1026 | |
| 1027 | } |
| 1028 | |
| 1029 | /** |
| 1030 | * Run the workflow or final steps one by one |
| 1031 | * |
| 1032 | * @param steps List of steps (Jenkins jobs) to execute |
| 1033 | * @param global_variables Map where the collected artifact URLs and 'env' objects are stored |
| 1034 | * @param failed_jobs Map with failed job names and result statuses, to report it later |
| 1035 | * @param jobs_data Map with all job names and result statuses, to showing it in description |
| 1036 | * @param step_id Counter for matching step ID with cell ID in description table |
| 1037 | * @param propagate Boolean. If false: allows to collect artifacts after job is finished, even with FAILURE status |
| 1038 | * If true: immediatelly fails the pipeline. DO NOT USE 'true' with runScenario(). |
| 1039 | */ |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 1040 | def runSteps(steps, global_variables, failed_jobs, jobs_data, global_jobs_data, step_id, Boolean propagate = false, artifactoryBaseUrl = '', artifactoryServer = '', scriptsLibrary = null, prefixMsg = '', parent_global_variables=null) { |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1041 | // Show expected jobs list in description |
| 1042 | updateDescription(global_jobs_data) |
| 1043 | |
| 1044 | for (step in steps) { |
| 1045 | |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 1046 | runWorkflowStep(global_variables, step, step_id, jobs_data, global_jobs_data, failed_jobs, propagate, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, prefixMsg, parent_global_variables) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1047 | |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 1048 | // Jump to next ID for updating next job data in description table |
| 1049 | step_id++ |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 1050 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1051 | } |
| 1052 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1053 | |
| 1054 | /** |
| 1055 | * Prepare jobs_data for generating the scenario description |
| 1056 | */ |
| 1057 | def prepareJobsData(scenario_steps, step_type, jobs_data) { |
| 1058 | def list_id = jobs_data.size() |
| 1059 | |
| 1060 | for (step in scenario_steps) { |
| 1061 | def display_name = '' |
| 1062 | def step_key = '' |
| 1063 | def desc = '' |
| 1064 | |
| 1065 | if (step.containsKey('job')) { |
| 1066 | display_name = step['job'] |
| 1067 | step_key = 'job' |
| 1068 | } |
| 1069 | else if (step.containsKey('script')) { |
| 1070 | display_name = step['script'] |
| 1071 | step_key = 'script' |
| 1072 | } |
| 1073 | else if (step.containsKey('parallel')) { |
| 1074 | display_name = 'Parallel steps' |
| 1075 | step_key = 'parallel' |
| 1076 | } |
| 1077 | else if (step.containsKey('sequence')) { |
| 1078 | display_name = 'Sequence steps' |
| 1079 | step_key = 'sequence' |
| 1080 | } |
| 1081 | |
| 1082 | if (step['description'] != null && step['description'] != 'null' && step['description'].toString() != '') { |
| 1083 | desc = (step['description'] ?: '').toString() |
| 1084 | } |
| 1085 | |
| 1086 | jobs_data.add([list_id : "$list_id", |
| 1087 | type : step_type, |
| 1088 | name : "$display_name", |
| 1089 | build_url : "0", |
| 1090 | build_id : "-", |
| 1091 | status : "-", |
| 1092 | desc : desc, |
| 1093 | child_desc : "", |
| 1094 | duration : '-', |
| 1095 | step_key : step_key, |
| 1096 | together_steps: [], |
| 1097 | ]) |
| 1098 | list_id += 1 |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1103 | /** |
| 1104 | * Run the workflow scenario |
| 1105 | * |
| 1106 | * @param scenario: Map with scenario steps. |
| 1107 | |
| 1108 | * There are two keys in the scenario: |
| 1109 | * workflow: contains steps to run deploy and test jobs |
| 1110 | * finally: contains steps to run report and cleanup jobs |
| 1111 | * |
| 1112 | * Scenario execution example: |
| 1113 | * |
| 1114 | * scenario_yaml = """\ |
| 1115 | * workflow: |
| 1116 | * - job: deploy-kaas |
| 1117 | * ignore_failed: false |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 1118 | * description: "Management cluster ${KAAS_VERSION}" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1119 | * parameters: |
| 1120 | * KAAS_VERSION: |
| 1121 | * type: StringParameterValue |
| 1122 | * use_variable: KAAS_VERSION |
| 1123 | * artifacts: |
| 1124 | * KUBECONFIG_ARTIFACT: artifacts/management_kubeconfig |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 1125 | * DEPLOYED_KAAS_VERSION: artifacts/management_version |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1126 | * |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 1127 | * - job: create-child |
| 1128 | * inherit_parent_params: true |
| 1129 | * ignore_failed: false |
| 1130 | * parameters: |
| 1131 | * KUBECONFIG_ARTIFACT_URL: |
| 1132 | * type: StringParameterValue |
| 1133 | * use_variable: KUBECONFIG_ARTIFACT |
| 1134 | * KAAS_VERSION: |
| 1135 | * type: StringParameterValue |
| 1136 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | 6c355be | 2021-11-09 14:06:56 +0200 | [diff] [blame] | 1137 | * RELEASE_NAME: |
| 1138 | * type: StringParameterValue |
| 1139 | * get_variable_from_yaml: |
| 1140 | * yaml_url: SI_CONFIG_ARTIFACT |
| 1141 | * yaml_key: .clusters[0].release_name |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 1142 | * global_artifacts: |
| 1143 | * CHILD_CONFIG_1: artifacts/child_kubeconfig |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 1144 | * |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1145 | * - job: test-kaas-ui |
Mykyta Karpin | a3d775e | 2020-04-24 14:45:17 +0300 | [diff] [blame] | 1146 | * ignore_not_built: false |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1147 | * parameters: |
| 1148 | * KUBECONFIG_ARTIFACT_URL: |
| 1149 | * type: StringParameterValue |
| 1150 | * use_variable: KUBECONFIG_ARTIFACT |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 1151 | * KAAS_VERSION: |
| 1152 | * type: StringParameterValue |
| 1153 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1154 | * artifacts: |
| 1155 | * REPORT_SI_KAAS_UI: artifacts/test_kaas_ui_result.xml |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1156 | * finally: |
| 1157 | * - job: testrail-report |
| 1158 | * ignore_failed: true |
| 1159 | * parameters: |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 1160 | * KAAS_VERSION: |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1161 | * type: StringParameterValue |
Dennis Dmitriev | cae9bca | 2019-09-19 16:10:03 +0300 | [diff] [blame] | 1162 | * get_variable_from_url: DEPLOYED_KAAS_VERSION |
Dennis Dmitriev | ce47093 | 2019-09-18 18:31:11 +0300 | [diff] [blame] | 1163 | * REPORTS_LIST: |
| 1164 | * type: TextParameterValue |
| 1165 | * use_template: | |
| 1166 | * REPORT_SI_KAAS_UI: \$REPORT_SI_KAAS_UI |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1167 | * """ |
| 1168 | * |
| 1169 | * runScenario(scenario) |
| 1170 | * |
Dennis Dmitriev | 5f014d8 | 2020-04-29 00:00:34 +0300 | [diff] [blame] | 1171 | * Scenario workflow keys: |
| 1172 | * |
| 1173 | * job: string. Jenkins job name |
| 1174 | * ignore_failed: bool. if true, keep running the workflow jobs if the job is failed, but fail the workflow at finish |
Sergey Lalov | 3a2e790 | 2023-07-27 01:19:02 +0400 | [diff] [blame] | 1175 | * ignore_unstable: bool. if true, keep running the workflow jobs if the job is unstable, but mark the workflow is unstable at finish |
azvyagintsev | e012e41 | 2024-05-22 16:09:23 +0300 | [diff] [blame] | 1176 | * ignore_aborted: bool. if true, keep running the workflow jobs if the job is aborted, but mark the workflow is unstable at finish |
Vasyl Saienko | e72b994 | 2021-03-04 10:54:49 +0200 | [diff] [blame] | 1177 | * 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] | 1178 | * 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 |
| 1179 | * inherit_parent_params: bool. if true, provide all parameters from the parent job to the child job as defaults |
| 1180 | * 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] | 1181 | * wf_pause_step_before_run: bool. Interactive pause exact step before run. |
| 1182 | * wf_pause_step_slack_report_channel: If step paused, send message about it in slack. |
| 1183 | * wf_pause_step_timeout: timeout im minutes to wait for manual unpause. |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1184 | */ |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1185 | def runScenario(scenario, slackReportChannel = '', artifactoryBaseUrl = '', Boolean logGlobalVariables = false, artifactoryServer = '', scriptsLibrary = null, |
| 1186 | global_variables = null, failed_jobs = null, jobs_data = null) { |
| 1187 | def common = new com.mirantis.mk.Common() |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1188 | |
Dennis Dmitriev | 79f3a2d | 2019-08-09 16:06:00 +0300 | [diff] [blame] | 1189 | // Clear description before adding new messages |
| 1190 | currentBuild.description = '' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1191 | // Collect the parameters for the jobs here |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1192 | if (global_variables == null) { |
| 1193 | global_variables = [:] |
| 1194 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1195 | // List of failed jobs to show at the end |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1196 | if (failed_jobs == null) { |
| 1197 | failed_jobs = [:] |
| 1198 | } |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 1199 | // Jobs data to use for wf job build description |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1200 | if (jobs_data == null) { |
| 1201 | jobs_data = [] |
| 1202 | } |
| 1203 | def global_jobs_data = jobs_data |
| 1204 | |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 1205 | // Counter for matching step ID with cell ID in description table |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1206 | def step_id = jobs_data.size() |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 1207 | // Generate expected list jobs for description |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1208 | prepareJobsData(scenario['workflow'], 'workflow', jobs_data) |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 1209 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1210 | def pause_step_id = jobs_data.size() |
| 1211 | // Generate expected list jobs for description |
| 1212 | prepareJobsData(scenario['pause'], 'pause', jobs_data) |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1213 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1214 | def finally_step_id = jobs_data.size() |
| 1215 | // Generate expected list jobs for description |
| 1216 | prepareJobsData(scenario['finally'], 'finally', jobs_data) |
| 1217 | |
| 1218 | |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1219 | def job_failed_flag = false |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1220 | try { |
| 1221 | // Run the 'workflow' jobs |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 1222 | runSteps(scenario['workflow'], global_variables, failed_jobs, jobs_data, global_jobs_data, step_id, false, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, '', global_variables) |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1223 | } catch (InterruptedException e) { |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1224 | job_failed_flag = true |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1225 | error "The job was aborted" |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1226 | } catch (e) { |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1227 | job_failed_flag = true |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1228 | printStackTrace(e) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1229 | error("Build failed: " + e.toString()) |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1230 | |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1231 | } finally { |
Dennis Dmitriev | 38a45cd | 2023-02-27 14:22:13 +0200 | [diff] [blame] | 1232 | // Log global_variables |
| 1233 | if (logGlobalVariables) { |
| 1234 | printVariables(global_variables) |
| 1235 | } |
| 1236 | |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1237 | def flag_pause_variable = (env.PAUSE_FOR_DEBUG) != null |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1238 | // Run the 'finally' or 'pause' jobs |
Sergey Lalov | 6e9400c | 2022-11-17 12:59:31 +0400 | [diff] [blame] | 1239 | common.infoMsg(failed_jobs) |
Sergey Lalov | 2d1cd9c | 2023-08-03 17:08:09 +0400 | [diff] [blame] | 1240 | // Run only if there are failed jobs in the scenario |
| 1241 | if (flag_pause_variable && (PAUSE_FOR_DEBUG && job_failed_flag)) { |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1242 | // Switching to 'pause' step index |
| 1243 | common.infoMsg("FINALLY BLOCK - PAUSE") |
| 1244 | step_id = pause_step_id |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 1245 | runSteps(scenario['pause'], global_variables, failed_jobs, jobs_data, global_jobs_data, step_id, false, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, '', global_variables) |
Sergey Lalov | 702384d | 2022-11-10 12:10:23 +0400 | [diff] [blame] | 1246 | |
| 1247 | } |
| 1248 | // Switching to 'finally' step index |
| 1249 | common.infoMsg("FINALLY BLOCK - CLEAR") |
AndrewB | 8505a7f | 2020-06-05 13:42:08 +0300 | [diff] [blame] | 1250 | step_id = finally_step_id |
Dennis Dmitriev | aa0fa74 | 2024-03-27 22:38:25 +0200 | [diff] [blame] | 1251 | runSteps(scenario['finally'], global_variables, failed_jobs, jobs_data, global_jobs_data, step_id, false, artifactoryBaseUrl, artifactoryServer, scriptsLibrary, '', global_variables) |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1252 | |
| 1253 | if (failed_jobs) { |
azvyagintsev | 0d97815 | 2022-01-27 14:01:33 +0200 | [diff] [blame] | 1254 | def statuses = [] |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 1255 | failed_jobs.each { |
sgudz | 74c8cdd | 2020-01-23 14:26:32 +0200 | [diff] [blame] | 1256 | statuses += it.value |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 1257 | } |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 1258 | if (statuses.contains('FAILURE')) { |
| 1259 | currentBuild.result = 'FAILURE' |
Sergey Lalov | e5e0a84 | 2023-10-02 15:55:59 +0400 | [diff] [blame] | 1260 | } else if (statuses.contains('ABORTED')) { |
| 1261 | currentBuild.result = 'ABORTED' |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1262 | } else if (statuses.contains('UNSTABLE')) { |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 1263 | currentBuild.result = 'UNSTABLE' |
azvyagintsev | 75390d9 | 2021-04-12 14:20:11 +0300 | [diff] [blame] | 1264 | } else { |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 1265 | currentBuild.result = 'FAILURE' |
| 1266 | } |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1267 | println "Failed jobs: ${failed_jobs}" |
vnaumov | 68cba27 | 2020-05-20 11:24:02 +0200 | [diff] [blame] | 1268 | } else { |
| 1269 | currentBuild.result = 'SUCCESS' |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1270 | } |
vnaumov | 5a6eb8a | 2020-03-31 11:16:54 +0200 | [diff] [blame] | 1271 | |
Sergey Lalov | 3a2e790 | 2023-07-27 01:19:02 +0400 | [diff] [blame] | 1272 | common.infoMsg("Workflow finished with result: ${currentBuild.result}") |
| 1273 | |
vnaumov | 5a6eb8a | 2020-03-31 11:16:54 +0200 | [diff] [blame] | 1274 | if (slackReportChannel) { |
| 1275 | def slack = new com.mirantis.mcp.SlackNotification() |
| 1276 | slack.jobResultNotification(currentBuild.result, slackReportChannel, '', null, '', 'slack_webhook_url') |
| 1277 | } |
sgudz | 9ac09d2 | 2020-01-22 14:31:30 +0200 | [diff] [blame] | 1278 | } // finally |
Dennis Dmitriev | 5d8a153 | 2019-07-30 16:39:27 +0300 | [diff] [blame] | 1279 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1280 | |
| 1281 | |
| 1282 | def manageArtifacts(entrypointDirectory, storeArtsInJenkins = false, artifactoryServerName = 'mcp-ci') { |
| 1283 | def mcpArtifactory = new com.mirantis.mcp.MCPArtifactory() |
| 1284 | def artifactoryRepoPath = "si-local/jenkins-job-artifacts/${JOB_NAME}/${BUILD_NUMBER}" |
| 1285 | def tests_log = "${entrypointDirectory}/tests.log" |
| 1286 | |
| 1287 | if (fileExists(tests_log)) { |
| 1288 | try { |
| 1289 | def size = sh([returnStdout: true, script: "stat --printf='%s' ${tests_log}"]).trim().toInteger() |
| 1290 | // do not archive unless it is more than 50 MB |
| 1291 | def allowed_size = 1048576 * 50 |
| 1292 | if (size >= allowed_size) { |
| 1293 | sh("gzip ${tests_log} || true") |
| 1294 | } |
| 1295 | } catch (e) { |
| 1296 | print("Cannot determine tests.log filesize: ${e}") |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | if (storeArtsInJenkins) { |
| 1301 | archiveArtifacts( |
| 1302 | artifacts: "${entrypointDirectory}/**", |
| 1303 | allowEmptyArchive: true |
| 1304 | ) |
| 1305 | } |
| 1306 | artConfig = [ |
| 1307 | deleteArtifacts: false, |
| 1308 | artifactory : artifactoryServerName, |
| 1309 | artifactPattern: "${entrypointDirectory}/**", |
| 1310 | artifactoryRepo: "artifactory/${artifactoryRepoPath}", |
| 1311 | ] |
| 1312 | def artDescription = mcpArtifactory.uploadArtifactsToArtifactory(artConfig) |
Vasyl Saienko | c0c029e | 2024-10-03 09:24:23 +0300 | [diff] [blame] | 1313 | if (currentBuild.description) { |
| 1314 | currentBuild.description += "${artDescription}<br>" |
| 1315 | } else { |
| 1316 | currentBuild.description = "${artDescription}<br>" |
| 1317 | } |
Dennis Dmitriev | 44ad94c | 2023-11-29 12:38:12 +0200 | [diff] [blame] | 1318 | |
| 1319 | junit(testResults: "${entrypointDirectory}/**/*.xml", allowEmptyResults: true) |
| 1320 | |
| 1321 | def artifactoryServer = Artifactory.server(artifactoryServerName) |
| 1322 | def artifactsUrl = "${artifactoryServer.getUrl()}/artifactory/${artifactoryRepoPath}" |
| 1323 | return artifactsUrl |
| 1324 | } |
| 1325 | |
| 1326 | |
| 1327 | return this |