1 /* Copyright 2017 Sandvine
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
19 * 1. Bug 745 : Jayant Madavi, Mrityunjay Yadav : JM00553988@techmahindra.com : 23-july-2019 : Improvement to the code, typically we have 2 * or more branches whose build gets triggered, ex master & release branch, the previous code was removing any/all docker.
20 * Now removing previous docker of the same branch, so that the other branch failed docker should not be removed. It also
21 * acts as clean-up for previous docker remove failure.
22 * 2. Feature 7829 : Mrityunjay Yadav, Jayant Madavi: MY00514913@techmahindra.com : 19-Aug-2019 : Added a parameters & function to invoke Robot test.
27 string(defaultValue: env.GERRIT_BRANCH, description: '', name: 'GERRIT_BRANCH'),
28 string(defaultValue: 'system', description: '', name: 'NODE'),
29 string(defaultValue: '', description: '', name: 'BUILD_FROM_SOURCE'),
30 string(defaultValue: 'unstable', description: '', name: 'REPO_DISTRO'),
31 string(defaultValue: '', description: '', name: 'COMMIT_ID'),
32 string(defaultValue: '-stage_2', description: '', name: 'UPSTREAM_SUFFIX'),
33 string(defaultValue: 'pubkey.asc', description: '', name: 'REPO_KEY_NAME'),
34 string(defaultValue: 'release', description: '', name: 'RELEASE'),
35 string(defaultValue: '', description: '', name: 'UPSTREAM_JOB_NAME'),
36 string(defaultValue: '', description: '', name: 'UPSTREAM_JOB_NUMBER'),
37 string(defaultValue: '', description: '', name: 'UPSTREAM_JOB_NUMBER'),
38 string(defaultValue: 'OSMETSI', description: '', name: 'GPG_KEY_NAME'),
39 string(defaultValue: 'artifactory-osm', description: '', name: 'ARTIFACTORY_SERVER'),
40 string(defaultValue: 'osm-stage_4', description: '', name: 'DOWNSTREAM_STAGE_NAME'),
41 string(defaultValue: 'testing-daily', description: '', name: 'DOCKER_TAG'),
42 booleanParam(defaultValue: true, description: '', name: 'SAVE_CONTAINER_ON_FAIL'),
43 booleanParam(defaultValue: false, description: '', name: 'SAVE_CONTAINER_ON_PASS'),
44 booleanParam(defaultValue: true, description: '', name: 'SAVE_ARTIFACTS_ON_SMOKE_SUCCESS'),
45 booleanParam(defaultValue: true, description: '', name: 'DO_STAGE_4'),
46 booleanParam(defaultValue: true, description: '', name: 'DO_BUILD'),
47 booleanParam(defaultValue: true, description: '', name: 'DO_INSTALL'),
48 booleanParam(defaultValue: true, description: '', name: 'DO_SMOKE'),
49 booleanParam(defaultValue: true, description: '', name: 'DO_DOCKERPUSH'),
50 booleanParam(defaultValue: false, description: '', name: 'SAVE_ARTIFACTS_OVERRIDE'),
51 string(defaultValue: '/home/jenkins/hive/openstack-etsi.rc', description: '', name: 'HIVE_VIM_1'),
52 booleanParam(defaultValue: false, description: '', name: 'DO_ROBOT'),
53 string(defaultValue: 'sanity', description: 'sanity/regression are the options', name: 'TEST_NAME'),
54 string(defaultValue: '/home/jenkins/hive/robot-systest.cfg', description: '', name: 'ROBOT_VIM'),
55 string(defaultValue: '/home/jenkins/hive/kubeconfig.yaml', description: '', name: 'KUBECONFIG'),
56 string(defaultValue: '/home/jenkins/hive/clouds.yaml', description: '', name: 'CLOUDS'),
60 def uninstall_osm(stackName) {
62 export OSM_USE_LOCAL_DEVOPS=true
63 export PATH=$PATH:/snap/bin
64 installers/full_install_osm.sh -y -w /tmp/osm -t ${stackName} -s ${stackName} --test --nolxd --nodocker --nojuju --nohostports --nohostclient --uninstall
68 def run_systest(stackName,tagName,testName,envfile=null) {
69 tempdir = sh(returnStdout: true, script: "mktemp -d").trim()
72 sh(script: "touch ${tempdir}/env")
73 envfile="${tempdir}/env"
75 sh "docker run --network net${stackName} --env-file ${envfile} -v ${tempdir}:/usr/share/osm-devops/systest/reports opensourcemano/osmclient:${tagName} make -C /usr/share/osm-devops/systest ${testName}"
76 sh "cp ${tempdir}/* ."
80 def run_robot_systest(stackName,tagName,testName,envfile=null,kubeconfig=null,clouds=null) {
81 tempdir = sh(returnStdout: true, script: "mktemp -d").trim()
84 sh(script: "touch ${tempdir}/env")
85 envfile="${tempdir}/env"
87 sh "docker run --network net${stackName} --env OSM_HOSTNAME=${stackName}_nbi --env PROMETHEUS_HOSTNAME=${stackName}_prometheus --env-file ${envfile} -v ${clouds}:/etc/openstack/clouds.yaml -v ${kubeconfig}:/root/.kube/config -v ${tempdir}:/robot-systest/reports opensourcemano/tests:${tagName} -c -t ${testName}"
88 sh "cp ${tempdir}/* ."
89 outputDirectory = sh(returnStdout: true, script: "pwd").trim()
90 println ("Present Directory is : ${outputDirectory}")
92 $class : 'RobotPublisher',
93 outputPath : "${outputDirectory}",
94 outputFileName : "*.xml",
95 disableArchiveOutput : false,
96 reportFileName : "report.html",
97 logFileName : "log.html",
100 otherFiles : "*.png",
104 def archive_logs(stackName) {
105 sh "docker service ls |grep \"${stackName}\"| awk '{print \$2}' | xargs -iy docker ps -af name=y --format \"{{.ID}} {{.Names}}\" --no-trunc | awk '{ print \"sudo cp /var/lib/docker/containers/\"\$1\"/\"\$1\"-json.log \"\$2\".log\"}' | xargs -iy bash -c y"
106 sh "sudo chown jenkins: osm*.log"
107 archiveArtifacts artifacts: '*.log'
110 node("${params.NODE}") {
114 tag_or_branch = params.GERRIT_BRANCH.replaceAll(/\./,"")
120 ci_helper = load "jenkins/ci-pipelines/ci_helper.groovy"
122 def upstream_main_job = params.UPSTREAM_SUFFIX
124 // upstream jobs always use merged artifacts
125 upstream_main_job += '-merge'
126 container_name_prefix = "osm-${tag_or_branch}"
127 container_name = "${container_name_prefix}"
129 keep_artifacts = false
130 if ( JOB_NAME.contains('merge') ) {
131 container_name += "-merge"
133 // On a merge job, we keep artifacts on smoke success
134 keep_artifacts = params.SAVE_ARTIFACTS_ON_SMOKE_SUCCESS
136 container_name += "-${BUILD_NUMBER}"
138 // Copy the artifacts from the upstream jobs
139 stage("Copy Artifacts") {
140 // cleanup any previous repo
143 // grab all stable upstream builds based on the
146 //def list = ["RO", "openvim", "osmclient", "IM", "devops", "MON", "N2VC", "NBI", "common", "LCM", "POL", "LW-UI", "NG-UI", "PLA", "tests"]
147 def list = ["RO", "openvim", "osmclient", "IM", "devops", "MON", "N2VC", "NBI", "common", "LCM", "POL", "NG-UI", "PLA", "tests"]
148 for (component in list) {
149 step ([$class: 'CopyArtifact',
150 projectName: "${component}${upstream_main_job}/${GERRIT_BRANCH}"])
152 // grab the build name/number
153 //options = get_env_from_build('build.env')
154 build_num = ci_helper.get_env_value('build.env','BUILD_NUMBER')
156 // grab the archives from the stage_2 builds (ie. this will be the artifacts stored based on a merge)
157 ci_helper.get_archive(params.ARTIFACTORY_SERVER,component,GERRIT_BRANCH, "${component}${upstream_main_job} :: ${GERRIT_BRANCH}", build_num)
159 // cleanup any prevously defined dists
163 // check if an upstream artifact based on specific build number has been requested
164 // This is the case of a merge build and the upstream merge build is not yet complete (it is not deemed
165 // a successful build yet). The upstream job is calling this downstream job (with the its build artifiact)
166 if ( params.UPSTREAM_JOB_NAME ) {
167 step ([$class: 'CopyArtifact',
168 projectName: "${params.UPSTREAM_JOB_NAME}",
169 selector: [$class: 'SpecificBuildSelector', buildNumber: "${params.UPSTREAM_JOB_NUMBER}"]
172 //options = get_env_from_build('build.env')
173 // grab the build name/number
174 //build_num = sh(returnStdout:true, script: "cat build.env | awk -F= '/BUILD_NUMBER/{print \$2}'").trim()
175 build_num = ci_helper.get_env_value('build.env','BUILD_NUMBER')
176 component = ci_helper.get_mdg_from_project(ci_helper.get_env_value('build.env','GERRIT_PROJECT'))
178 // the upstream job name contains suffix with the project. Need this stripped off
179 def project_without_branch = params.UPSTREAM_JOB_NAME.split('/')[0]
181 // Remove the previous artifact for this component. Use the new upstream artifact
182 sh "rm -rf pool/${component}"
184 ci_helper.get_archive(params.ARTIFACTORY_SERVER,component,GERRIT_BRANCH, "${project_without_branch} :: ${GERRIT_BRANCH}", build_num)
189 // sign all the components
190 for (component in list) {
191 sh "dpkg-sig --sign builder -k ${GPG_KEY_NAME} pool/${component}/*"
194 // now create the distro
195 for (component in list) {
196 sh "mkdir -p dists/${params.REPO_DISTRO}/${component}/binary-amd64/"
197 sh "apt-ftparchive packages pool/${component} > dists/${params.REPO_DISTRO}/${component}/binary-amd64/Packages"
198 sh "gzip -9fk dists/${params.REPO_DISTRO}/${component}/binary-amd64/Packages"
201 // create and sign the release file
202 sh "apt-ftparchive release dists/${params.REPO_DISTRO} > dists/${params.REPO_DISTRO}/Release"
203 sh "gpg --yes -abs -u ${GPG_KEY_NAME} -o dists/${params.REPO_DISTRO}/Release.gpg dists/${params.REPO_DISTRO}/Release"
205 // copy the public key into the release folder
206 // this pulls the key from the home dir of the current user (jenkins)
207 sh "cp ~/${REPO_KEY_NAME} ."
209 // merge the change logs
211 rm -f changelog/changelog-osm.html
212 [ ! -d changelog ] || for mdgchange in \$(ls changelog); do cat changelog/\$mdgchange >> changelog/changelog-osm.html; done
214 RELEASE_DIR = sh(returnStdout:true, script: 'pwd').trim()
216 // start an apache server to serve up the images
217 http_server_name = "${container_name}-apache"
219 pwd = sh(returnStdout:true, script: 'pwd').trim()
220 repo_base_url = ci_helper.start_http_server(pwd,http_server_name)
223 // now pull the devops package and install in temporary location
224 tempdir = sh(returnStdout: true, script: "mktemp -d").trim()
225 osm_devops_dpkg = sh(returnStdout: true, script: "find . -name osm-devops*.deb").trim()
226 sh "dpkg -x ${osm_devops_dpkg} ${tempdir}"
227 OSM_DEVOPS="${tempdir}/usr/share/osm-devops"
232 if ( params.DO_BUILD ) {
234 sh "make -C docker clean"
235 sh "make -C docker -j `nproc` Q= CMD_DOCKER_ARGS= TAG=${container_name} RELEASE=${params.RELEASE} REPOSITORY_BASE=${repo_base_url} REPOSITORY_KEY=${params.REPO_KEY_NAME} REPOSITORY=${params.REPO_DISTRO}"
240 if ( params.DO_INSTALL ) {
243 //will by default always delete containers on complete
244 //sh "jenkins/system/delete_old_containers.sh ${container_name_prefix}"
251 if ( params.COMMIT_ID )
253 commit_id = "-b ${params.COMMIT_ID}"
256 if ( params.REPO_DISTRO )
258 repo_distro = "-r ${params.REPO_DISTRO}"
261 if ( params.REPO_KEY_NAME )
263 repo_key_name = "-k ${params.REPO_KEY_NAME}"
266 if ( params.RELEASE )
268 release = "-R ${params.RELEASE}"
271 if ( params.REPOSITORY_BASE )
273 repo_base_url = "-u ${params.REPOSITORY_BASE}"
275 if ( params.DO_STAGE_4 ) {
277 sh "docker stack list |grep \"${container_name_prefix}\"| awk '{ print \$1 }'| xargs docker stack rm"
279 catch (caughtError) {
280 println("Caught error: docker stack rm failed!")
284 export PATH=$PATH:/snap/bin
285 installers/full_install_osm.sh -y -s ${container_name} --test --nolxd --nodocker --nojuju --nohostports --nohostclient \
286 --nodockerbuild -t ${container_name} \
293 ${params.BUILD_FROM_SOURCE}
298 stage_archive = false
299 if ( params.DO_SMOKE ) {
300 stage("OSM Health") {
301 sh "installers/osm_health.sh -s ${container_name}"
304 run_systest(container_name,container_name,"smoke")
305 // archive smoke success until stage_4 is ready
307 if ( ! currentBuild.result.equals('UNSTABLE') ) {
308 stage_archive = keep_artifacts
310 error = new Exception("Smoke test failed")
311 currentBuild.result = 'FAILURE'
316 if ( params.DO_STAGE_4 ) {
317 // override stage_archive to only archive on stable
318 stage_archive = false
319 stage("System Integration Test") {
320 if ( params.DO_ROBOT ) {
321 run_robot_systest(container_name,container_name,params.TEST_NAME,params.ROBOT_VIM,params.KUBECONFIG,params.CLOUDS)
323 run_systest(container_name,container_name,"openstack_stage_4",params.HIVE_VIM_1)
325 // Archive logs to containers_logs.txt
326 archive_logs(container_name)
327 if ( ! currentBuild.result.equals('UNSTABLE') && ! currentBuild.result.equals('FAILURE')) {
328 stage_archive = keep_artifacts
330 println ("Systest test failed, throwing error")
331 error = new Exception("Systest test failed")
332 currentBuild.result = 'FAILURE'
338 // override to save the artifacts
339 if ( params.SAVE_ARTIFACTS_OVERRIDE || stage_archive ) {
341 sh "echo ${container_name} > build_version.txt"
342 archiveArtifacts artifacts: "build_version.txt", fingerprint: true
344 // Archive the tested repo
345 dir("${RELEASE_DIR}") {
346 ci_helper.archive(params.ARTIFACTORY_SERVER,RELEASE,GERRIT_BRANCH,'tested')
348 if ( params.DO_DOCKERPUSH ) {
349 stage("Docker Push") {
350 sh "make -C docker push INPUT_TAG=${container_name} TAG=${params.DOCKER_TAG}"
353 stage("Snap promotion") {
354 def snaps = ["osmclient"]
355 for (snap in snaps) {
357 if (BRANCH_NAME.startsWith("v")) {
358 channel=BRANCH_NAME.substring(1)+"/"
359 } else if (BRANCH_NAME!="master") {
360 channel+="/"+BRANCH_NAME.replaceAll('/','-')
362 track=channel+"edge\\*"
363 edge_rev=sh(returnStdout: true,
364 script: "sudo docker run -v ~/.snapcraft:/snapcraft -v ${WORKSPACE}:/build " +
365 "-w /build snapcore/snapcraft:stable /bin/bash -c " +
366 "\"snapcraft login --with /snapcraft/config &>/dev/null && " +
367 "snapcraft revisions $snap\" | " +
368 "grep \" $track\" | tail -1 | awk '{print \$1}'").trim()
369 track=channel+"beta\\*"
370 beta_rev=sh(returnStdout: true,
371 script: "sudo docker run -v ~/.snapcraft:/snapcraft -v ${WORKSPACE}:/build " +
372 "-w /build snapcore/snapcraft:stable /bin/bash -c " +
373 "\"snapcraft login --with /snapcraft/config &>/dev/null && " +
374 "snapcraft revisions $snap\" | " +
375 "grep \" $track\" | tail -1 | awk '{print \$1}'").trim()
377 if ( edge_rev != beta_rev ) {
378 print "Promoting $edge_rev to beta in place of $beta_rev"
379 beta_track=channel+"beta"
380 sh("sudo docker run -v ~/.snapcraft:/snapcraft -v ${WORKSPACE}:/build " +
381 "-w /build snapcore/snapcraft:stable /bin/bash -c " +
382 "\"snapcraft login --with /snapcraft/config &>/dev/null && " +
383 "snapcraft release $snap $edge_rev $beta_track\"")
391 catch(Exception ex) {
393 currentBuild.result = 'FAILURE'
394 println("Caught error")
395 println(ex.getMessage())
398 if ( params.DO_INSTALL ) {
400 if ( !params.SAVE_CONTAINER_ON_FAIL ) {
401 uninstall_osm container_name
402 sh "docker stop ${http_server_name} || true"
403 sh "docker rm ${http_server_name} || true"
407 if ( !params.SAVE_CONTAINER_ON_PASS ) {
408 uninstall_osm container_name
409 sh "docker stop ${http_server_name} || true"
410 sh "docker rm ${http_server_name} || true"