Ensure clean snap
[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 }
29
30 def ci_pipeline(mdg,url_prefix,project,branch,refspec,revision,do_stage_3,artifactory_server,docker_args="",do_stage_4=false) {
31     println("do_stage_3= ${do_stage_3}")
32     ci_helper = load "devops/jenkins/ci-pipelines/ci_helper.groovy"
33
34     stage('Prepare') {
35         sh 'env'
36     }
37
38     stage('Checkout') {
39         project_checkout(url_prefix,project,refspec,revision)
40     }
41
42     stage('License Scan') {
43       if (!JOB_NAME.contains('merge')) {
44         sh "devops/tools/license_scan.sh"
45       }
46       else {
47         println("skip the scan for merge")
48       }
49     }
50
51     container_name = "${project}-${branch}".toLowerCase()
52
53     stage('Docker-Build') {
54         sh '''
55            echo RUN groupadd -o -g $(id -g) -r jenkins >> Dockerfile
56            echo RUN useradd -o -u $(id -u) --create-home -r -g  jenkins jenkins >> Dockerfile
57            '''
58         sh "docker build -t ${container_name} ."
59     }
60
61     withDockerContainer(image: "${container_name}", args: docker_args) {
62         stage('Test') {
63             sh 'devops-stages/stage-test.sh'
64             if (fileExists('coverage.xml')) {
65                 cobertura coberturaReportFile: 'coverage.xml'
66             }
67             if (fileExists('nosetests.xml')) {
68                 junit 'nosetests.xml'
69             }
70         }
71         stage('Build') {
72             sh(returnStdout:true,  script: 'devops-stages/stage-build.sh').trim()
73         }
74         stage('Archive') {
75             sh "mkdir -p changelog"
76             sh "devops/tools/generatechangelog-pipeline.sh > changelog/changelog-${mdg}.html"
77             sh(returnStdout:true,  script: 'devops-stages/stage-archive.sh').trim()
78             ci_helper.archive(artifactory_server,mdg,branch,'untested')
79         }
80     }
81
82     if (fileExists('snap/snapcraft.yaml')) {
83         stage('Snap build') {
84             sh "docker pull snapcore/snapcraft:stable"
85             sh "sudo rm -rf ${WORKSPACE}/stage/ ${WORKSPACE}/parts/ ${WORKSPACE}/prime/ ${WORKSPACE}/*.snap"
86             sh "sudo snapcraft clean --use-lxd"
87             sh "snapcraft --use-lxd"
88             sh "mv ${WORKSPACE}/${mdg}_*.snap ${WORKSPACE}/${mdg}.snap"
89             sh "sudo rm -rf ${WORKSPACE}/stage/ ${WORKSPACE}/parts/ ${WORKSPACE}/prime/"
90
91             REV=""
92             if ( !JOB_NAME.contains('merge') ) {
93                 REV="/"+"${GERRIT_REFSPEC}".replaceAll('/','-')
94             }
95             channel="latest"
96             if (BRANCH_NAME.startsWith("v")) {
97                 channel=BRANCH_NAME.substring(1)
98             } else if (BRANCH_NAME!="master") {
99                 REV="/"+BRANCH_NAME+REV.replaceAll('/','-')
100             }
101
102             sh "sudo docker run -v ~/.snapcraft:/snapcraft -v ${WORKSPACE}:/build " +
103                 "-w /build snapcore/snapcraft:stable /bin/bash -c " +
104                 "\"snapcraft login --with /snapcraft/config ; snapcraft push --release=${channel}/edge${REV} ${mdg}.snap\""
105             sh "sudo rm -rf ${WORKSPACE}/*.snap"
106         }
107     }
108
109     if ( do_stage_3 ) {
110
111         stage('Build System') {
112             def downstream_params_stage_3 = [
113                 string(name: 'GERRIT_BRANCH', value: "${branch}"),
114                 string(name: 'UPSTREAM_JOB_NAME', value: "${JOB_NAME}" ),
115                 string(name: 'UPSTREAM_JOB_NUMBER', value: "${BUILD_NUMBER}" ),
116                 booleanParam(name: 'DO_STAGE_4', value: do_stage_4 )
117             ]
118             stage_3_job = "osm-stage_3"
119             if ( JOB_NAME.contains('merge') ) {
120                 stage_3_job += '-merge'
121             }
122
123             // callout to stage_3. This is the system build
124             result = build job: "${stage_3_job}/${branch}", parameters: downstream_params_stage_3, propagate: true
125             if (result.getResult() != 'SUCCESS') {
126                 project = result.getProjectName()
127                 build = result.getNumber()
128                 error("${project} build ${build} failed")
129             }
130         }
131     }
132
133 }
134
135 return this