Lionel Jouin | ab2f897 | 2022-12-15 11:14:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2022 Nordix Foundation |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | import org.jenkinsci.plugins.pipeline.modeldefinition.Utils |
| 17 | |
| 18 | node('nordix-nsm-build-ubuntu2204') { |
| 19 | build_number = env.BUILD_NUMBER |
| 20 | workspace = env.WORKSPACE |
| 21 | ws("${workspace}/${build_number}") { |
| 22 | def git_project = params.GIT_PROJECT |
| 23 | def current_branch = params.CURRENT_BRANCH |
| 24 | def default_branch = params.DEFAULT_BRANCH |
| 25 | |
| 26 | def meridio_version = params.MERIDIO_VERSION |
| 27 | def tapa_version = params.TAPA_VERSION |
| 28 | def kubernetes_version = params.KUBERNETES_VERSION |
| 29 | def nsm_version = params.NSM_VERSION |
| 30 | def ip_family = params.IP_FAMILY |
| 31 | def number_of_workers = params.NUMBER_OF_WORKERS |
| 32 | def environment_name = params.ENVIRONMENT_NAME |
| 33 | def focus = params.FOCUS |
| 34 | def skip = params.SKIP |
| 35 | |
| 36 | def number_of_runs = params.NUMBER_OF_RUNS |
| 37 | def interval = params.INTERVAL |
| 38 | |
| 39 | stage('Clone/Checkout') { |
| 40 | git branch: default_branch, url: git_project |
| 41 | checkout([ |
| 42 | $class: 'GitSCM', |
| 43 | branches: [[name: current_branch]], |
| 44 | extensions: [], |
| 45 | userRemoteConfigs: [[ |
| 46 | refspec: '+refs/pull/*/head:refs/remotes/origin/pr/*', |
| 47 | url: git_project |
| 48 | ]] |
| 49 | ]) |
| 50 | sh 'git show' |
| 51 | } |
| 52 | timeout(30) { |
| 53 | stage('Environment') { |
| 54 | def command = "make -s -C test/e2e/environment/$environment_name/ KUBERNETES_VERSION=$kubernetes_version NSM_VERSION=$nsm_version IP_FAMILY=$ip_family KUBERNETES_WORKERS=$number_of_workers MERIDIO_VERSION=$meridio_version TAPA_VERSION=$tapa_version" |
| 55 | try { |
| 56 | ExecSh(command).call() |
| 57 | } catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) { |
| 58 | currentBuild.result = 'ABORTED' |
| 59 | } catch (Exception e) { |
| 60 | unstable 'Environment setup failed' |
| 61 | currentBuild.result = 'FAILURE' |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | stage('E2E List') { |
| 66 | E2EList(number_of_runs, interval, environment_name, ip_family, focus, skip).call() |
| 67 | } |
| 68 | stage('Cleanup') { |
| 69 | Cleanup() |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Creates the list of e2e to run during this job |
| 75 | def E2EList(number_of_runs, interval, environment_name, ip_family, focus, skip) { |
| 76 | return { |
| 77 | def stages = [:] |
| 78 | def list = sh(script: "seq -w 1 $number_of_runs | paste -sd ' ' -", returnStdout: true).trim().split(' ') |
| 79 | def previous = '0' |
Lionel Jouin | ab2f897 | 2022-12-15 11:14:43 +0100 | [diff] [blame] | 80 | for (i in list) { |
| 81 | stages.put("$i", E2E(i, previous, interval, environment_name, ip_family, focus, skip)) |
| 82 | previous = i |
| 83 | } |
| 84 | parallel(stages) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Run e2e |
| 89 | def E2E(id, previous_id, interval, environment_name, ip_family, focus, skip) { |
| 90 | return { |
Lionel Jouin | 2d28d8a | 2023-03-07 17:58:33 +0100 | [diff] [blame] | 91 | def wait = sh(script: "echo `expr $interval \\* $previous_id`", returnStdout: true).trim() |
| 92 | stage("Wait $wait seconds") { |
| 93 | sh "sleep $wait" |
Lionel Jouin | ab2f897 | 2022-12-15 11:14:43 +0100 | [diff] [blame] | 94 | } |
| 95 | stage('E2E') { |
| 96 | def command = "make e2e E2E_ENVIRONMENT=\"$environment_name\" E2E_IP_FAMILY=\"$ip_family\" E2E_FOCUS=\"$focus\" E2E_SKIP=\"$skip\"" |
Lionel Jouin | 2d28d8a | 2023-03-07 17:58:33 +0100 | [diff] [blame] | 97 | timeout(time: interval, unit: 'SECONDS') { |
| 98 | try { |
| 99 | ExecSh(command).call() |
| 100 | } catch (Exception e) { |
| 101 | unstable 'Failing e2e' |
| 102 | } |
Lionel Jouin | ab2f897 | 2022-12-15 11:14:43 +0100 | [diff] [blame] | 103 | } |
| 104 | } |
Lionel Jouin | ab2f897 | 2022-12-15 11:14:43 +0100 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | // Raise error in Jenkins job |
| 109 | def Error(e) { |
| 110 | return { |
| 111 | Cleanup() |
| 112 | error e |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Cleanup directory and kind cluster |
| 117 | def Cleanup() { |
| 118 | def command = 'make -s -C docs/demo/scripts/kind/ clean' |
| 119 | ExecSh(command).call() |
| 120 | cleanWs() |
| 121 | } |
| 122 | |
| 123 | // Execute command |
| 124 | def ExecSh(command) { |
| 125 | return { |
| 126 | if (env.DRY_RUN != 'true') { |
| 127 | sh """ |
| 128 | . \${HOME}/.profile |
| 129 | ${command} |
| 130 | """ |
| 131 | } else { |
| 132 | echo "${command}" |
| 133 | } |
| 134 | } |
| 135 | } |