Better release note check
[osm/devops.git] / jenkins / ci-pipelines / ci_stage_2.groovy
1 /* Copyright 2017 Sandvine
2  *
3  * All Rights Reserved.
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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
15  *   under the License.
16  */
17
18 def project_checkout(url_prefix,project,refspec,revision) {
19     // checkout the project
20     // this is done automaticaly by the multibranch pipeline plugin
21     // git url: "${url_prefix}/${project}"
22
23     sh "git fetch --tags"
24     sh "git fetch origin ${refspec}"
25     if (GERRIT_PATCHSET_REVISION.size() > 0 ) {
26         sh "git checkout -f ${revision}"
27     }
28     sh "sudo git clean -dfx"
29 }
30
31 def ci_pipeline(mdg,url_prefix,project,branch,refspec,revision,do_stage_3,artifactory_server,docker_args="",do_stage_4=false) {
32     println("do_stage_3= ${do_stage_3}")
33     ci_helper = load "devops/jenkins/ci-pipelines/ci_helper.groovy"
34
35     stage('Prepare') {
36         sh 'env'
37     }
38
39     stage('Checkout') {
40         project_checkout(url_prefix,project,refspec,revision)
41     }
42
43     stage('License Scan') {
44       if (!JOB_NAME.contains('merge')) {
45         sh "devops/tools/license_scan.sh"
46       }
47       else {
48         println("skip the scan for merge")
49       }
50     }
51
52     stage('Release Note Check') {
53       if (fileExists('devops-stages/stage-releasenote.sh')) {
54         if (!JOB_NAME.contains('merge')) {
55             sh "devops-stages/stage-releasenote.sh"
56         }
57         else {
58             println("Not checking release notes for merge job")
59         }
60       }
61       else {
62           println("No releasenote check present")
63       }
64     }
65
66     container_name = "${project}-${branch}".toLowerCase()
67
68     stage('Docker-Build') {
69         sh "docker build -t ${container_name} ."
70     }
71
72     UID = sh(returnStdout:true,  script: 'id -u').trim()
73     GID = sh(returnStdout:true,  script: 'id -g').trim()
74     withDockerContainer(image: "${container_name}", args: docker_args + " -u root") {
75         stage('Test') {
76             sh "groupadd -o -g $GID -r jenkins"
77             sh "useradd -o -u $UID -d `pwd` -r -g jenkins jenkins"
78             sh "echo '#! /bin/sh' > /usr/bin/mesg"
79             sh "chmod 755 /usr/bin/mesg"
80             sh "runuser jenkins -c devops-stages/stage-test.sh"
81             if (fileExists('coverage.xml')) {
82                 cobertura coberturaReportFile: 'coverage.xml'
83             }
84             if (fileExists('nosetests.xml')) {
85                 junit 'nosetests.xml'
86             }
87         }
88         stage('Build') {
89             sh(returnStdout:true,
90                 script: "runuser jenkins -c devops-stages/stage-build.sh").trim()
91         }
92         stage('Archive') {
93             sh "runuser jenkins -c 'mkdir -p changelog'"
94             sh "runuser jenkins -c \"devops/tools/generatechangelog-pipeline.sh > changelog/changelog-${mdg}.html\""
95             sh(returnStdout:true,
96                 script: "runuser jenkins -c devops-stages/stage-archive.sh").trim()
97             ci_helper.archive(artifactory_server,mdg,branch,'untested')
98         }
99     }
100
101     if (fileExists('snap/snapcraft.yaml')) {
102         stage('Snap build') {
103             sh "docker pull snapcore/snapcraft:stable"
104             sh "sudo rm -rf ${WORKSPACE}/stage/ ${WORKSPACE}/parts/ ${WORKSPACE}/prime/ ${WORKSPACE}/*.snap"
105             sh "sudo snapcraft clean --use-lxd"
106             sh "snapcraft --use-lxd"
107             sh "mv ${WORKSPACE}/${mdg}_*.snap ${WORKSPACE}/${mdg}.snap"
108             sh "sudo rm -rf ${WORKSPACE}/stage/ ${WORKSPACE}/parts/ ${WORKSPACE}/prime/"
109
110             REV=""
111             if ( !JOB_NAME.contains('merge') ) {
112                 REV="/"+"${GERRIT_REFSPEC}".replaceAll('/','-')
113             }
114             channel="latest"
115             if (BRANCH_NAME.startsWith("v")) {
116                 channel=BRANCH_NAME.substring(1)
117             } else if (BRANCH_NAME!="master") {
118                 REV="/"+BRANCH_NAME+REV.replaceAll('/','-')
119             }
120
121             sh "sudo docker run -v ~/.snapcraft:/snapcraft -v ${WORKSPACE}:/build " +
122                 "-w /build snapcore/snapcraft:stable /bin/bash -c " +
123                 "\"snapcraft login --with /snapcraft/config ; snapcraft push --release=${channel}/edge${REV} ${mdg}.snap\""
124             sh "sudo rm -rf ${WORKSPACE}/*.snap"
125         }
126     }
127
128     if ( do_stage_3 ) {
129         stage('Build System') {
130             def downstream_params_stage_3 = [
131                 string(name: 'GERRIT_BRANCH', value: "${branch}"),
132                 string(name: 'INSTALLER', value: "Default" ),
133                 string(name: 'OPENSTACK_BASE_IMAGE', value: "ubuntu20.04" ),
134                 string(name: 'UPSTREAM_JOB_NAME', value: "${JOB_NAME}" ),
135                 string(name: 'UPSTREAM_JOB_NUMBER', value: "${BUILD_NUMBER}" ),
136                 booleanParam(name: 'DO_STAGE_4', value: do_stage_4 )
137             ]
138             stage_3_job = "osm-stage_3"
139             if ( JOB_NAME.contains('merge') ) {
140                 stage_3_job += '-merge'
141             }
142
143             // callout to stage_3. This is the system build
144             result = build job: "${stage_3_job}/${branch}", parameters: downstream_params_stage_3, propagate: true
145             if (result.getResult() != 'SUCCESS') {
146                 project = result.getProjectName()
147                 build = result.getNumber()
148                 error("${project} build ${build} failed")
149             }
150         }
151     }
152 }
153
154 return this