add MON repo
[osm/devops.git] / jenkins / ci-pipelines / ci_stage_3.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 properties([
19     parameters([
20         string(defaultValue: env.GERRIT_BRANCH, description: '', name: 'GERRIT_BRANCH'),
21         string(defaultValue: 'system', description: '', name: 'NODE'),
22         string(defaultValue: '', description: '', name: 'BUILD_FROM_SOURCE'),
23         string(defaultValue: 'unstable', description: '', name: 'REPO_DISTRO'),
24         string(defaultValue: '', description: '', name: 'COMMIT_ID'),
25         string(defaultValue: '-stage_2', description: '', name: 'UPSTREAM_SUFFIX'),
26         string(defaultValue: 'pubkey.asc', description: '', name: 'REPO_KEY_NAME'),
27         string(defaultValue: 'release', description: '', name: 'RELEASE'),
28         string(defaultValue: '', description: '', name: 'UPSTREAM_JOB_NAME'),
29         string(defaultValue: '', description: '', name: 'UPSTREAM_JOB_NUMBER'),
30         string(defaultValue: '', description: '', name: 'UPSTREAM_JOB_NUMBER'),
31         string(defaultValue: 'dpkg1', description: '', name: 'GPG_KEY_NAME'),
32         string(defaultValue: 'artifactory-osm', description: '', name: 'ARTIFACTORY_SERVER'),
33         string(defaultValue: 'osm-stage_4', description: '', name: 'DOWNSTREAM_STAGE_NAME'),
34         booleanParam(defaultValue: false, description: '', name: 'SAVE_CONTAINER_ON_FAIL'),
35         booleanParam(defaultValue: false, description: '', name: 'SAVE_CONTAINER_ON_PASS'),
36         booleanParam(defaultValue: false, description: '', name: 'DO_STAGE_4'),
37         booleanParam(defaultValue: false, description: '', name: 'SAVE_ARTIFACTS_OVERRIDE'),
38     ])
39 ])
40
41 node("${params.NODE}") {
42
43     sh 'env'
44
45     tag_or_branch = params.GERRIT_BRANCH.replaceAll(/\./,"")
46
47     stage("Checkout") {
48         checkout scm
49     }
50
51     ci_helper = load "jenkins/ci-pipelines/ci_helper.groovy"
52
53     def upstream_main_job = params.UPSTREAM_SUFFIX
54
55     // upstream jobs always use merged artifacts
56     upstream_main_job += '-merge'
57     container_name_prefix = "osm-${tag_or_branch}"
58     container_name = "${container_name_prefix}"
59     if ( JOB_NAME.contains('merge') ) {
60         container_name += "-merge"
61     }
62     container_name += "-${BUILD_NUMBER}"
63
64     // Copy the artifacts from the upstream jobs
65     stage("Copy Artifacts") {
66         // cleanup any previous repo
67         sh 'rm -rf repo'
68         dir("repo") {
69             // grab all stable upstream builds based on the
70
71             dir("${RELEASE}") {
72                 def list = ["SO", "UI", "RO", "openvim", "osmclient", "IM", "devops", "MON" ]
73                 for (component in list) {
74                     step ([$class: 'CopyArtifact',
75                            projectName: "${component}${upstream_main_job}/${GERRIT_BRANCH}"])
76
77                     // grab the build name/number
78                     //options = get_env_from_build('build.env')
79                     build_num = ci_helper.get_env_value('build.env','BUILD_NUMBER')
80
81                     // grab the archives from the stage_2 builds (ie. this will be the artifacts stored based on a merge)
82                     ci_helper.get_archive(params.ARTIFACTORY_SERVER,component,GERRIT_BRANCH, "${component}${upstream_main_job} :: ${GERRIT_BRANCH}", build_num)
83
84                     // cleanup any prevously defined dists
85                     sh "rm -rf dists"
86                 }
87
88                 // check if an upstream artifact based on specific build number has been requested
89                 // This is the case of a merge build and the upstream merge build is not yet complete (it is not deemed
90                 // a successful build yet). The upstream job is calling this downstream job (with the its build artifiact)
91                 if ( params.UPSTREAM_JOB_NAME ) {
92                     step ([$class: 'CopyArtifact',
93                            projectName: "${params.UPSTREAM_JOB_NAME}",
94                            selector: [$class: 'SpecificBuildSelector', buildNumber: "${params.UPSTREAM_JOB_NUMBER}"]
95                           ])
96
97                     //options = get_env_from_build('build.env')
98                     // grab the build name/number
99                     //build_num = sh(returnStdout:true,  script: "cat build.env | awk -F= '/BUILD_NUMBER/{print \$2}'").trim()
100                     build_num = ci_helper.get_env_value('build.env','BUILD_NUMBER')
101                     component = ci_helper.get_mdg_from_project(ci_helper.get_env_value('build.env','GERRIT_PROJECT'))
102
103                     // the upstream job name contains suffix with the project. Need this stripped off
104                     def project_without_branch = params.UPSTREAM_JOB_NAME.split('/')[0]
105
106                     ci_helper.get_archive(params.ARTIFACTORY_SERVER,component,GERRIT_BRANCH, "${project_without_branch} :: ${GERRIT_BRANCH}", build_num)
107
108                     sh "rm -rf dists"
109                 }
110                 
111                 // sign all the components
112                 for (component in list) {
113                     sh "dpkg-sig --sign builder -k ${GPG_KEY_NAME} pool/${component}/*"
114                 }
115
116                 // now create the distro
117                 for (component in list) {
118                     sh "mkdir -p dists/${params.REPO_DISTRO}/${component}/binary-amd64/"
119                     sh "apt-ftparchive packages pool/${component} > dists/${params.REPO_DISTRO}/${component}/binary-amd64/Packages"
120                     sh "gzip -9fk dists/${params.REPO_DISTRO}/${component}/binary-amd64/Packages"
121                 }
122
123                 // create and sign the release file
124                 sh "apt-ftparchive release dists/${params.REPO_DISTRO} > dists/${params.REPO_DISTRO}/Release"
125                 sh "gpg --yes -abs -u ${GPG_KEY_NAME} -o dists/${params.REPO_DISTRO}/Release.gpg dists/${params.REPO_DISTRO}/Release"
126
127                 // copy the public key into the release folder
128                 // this pulls the key from the home dir of the current user (jenkins)
129                 sh "cp ~/${REPO_KEY_NAME} ."
130
131                 // merge the change logs
132                 sh """
133                    rm -f changelog/changelog-osm.html
134                    [ ! -d changelog ] || for mdgchange in \$(ls changelog); do cat changelog/\$mdgchange >> changelog/changelog-osm.html; done
135                    """
136             }
137             // start an apache server to serve up the images
138             http_server_name = "${container_name}-apache"
139
140             pwd = sh(returnStdout:true,  script: 'pwd').trim()
141             repo_base_url = ci_helper.start_http_server(pwd,http_server_name)
142         }
143     }
144
145     error = null
146
147     try {
148         stage("Install") {
149
150             //will by default always delete containers on complete
151             //sh "jenkins/system/delete_old_containers.sh ${container_name_prefix}"
152
153             commit_id = ''
154             repo_distro = ''
155             repo_key_name = ''
156             release = ''
157
158             if ( params.COMMIT_ID )
159             {
160                 commit_id = "-b ${params.COMMIT_ID}"
161             }
162
163             if ( params.REPO_DISTRO )
164             {
165                 repo_distro = "-r ${params.REPO_DISTRO}"
166             }
167
168             if ( params.REPO_KEY_NAME )
169             {
170                 repo_key_name = "-k ${params.REPO_KEY_NAME}"
171             }
172
173             if ( params.RELEASE )
174             {
175                 release = "-R ${params.RELEASE}"
176             }
177      
178             sh """
179                 export OSM_USE_LOCAL_DEVOPS=true
180                 jenkins/host/start_build system --build-container ${container_name} \
181                                                 ${commit_id} \
182                                                 ${repo_distro} \
183                                                 ${repo_base_url} \
184                                                 ${repo_key_name} \
185                                                 ${release} \
186                                                 ${params.BUILD_FROM_SOURCE}
187                """
188         }
189
190         stage("Smoke") {
191             ci_helper.systest_run(container_name, 'smoke')
192             junit '*.xml'
193         }
194
195         stage_4_archive = false
196         if ( params.DO_STAGE_4 ) {
197             stage("stage_4") {
198                 def downstream_params = [
199                     string(name: 'CONTAINER_NAME', value: container_name),
200                     string(name: 'NODE', value: NODE_NAME.split()[0]),
201                 ]
202                 stage_4_result = build job: "${params.DOWNSTREAM_STAGE_NAME}/${GERRIT_BRANCH}", parameters: downstream_params, propagate: false 
203                 currentBuild.result = stage_4_result.result
204
205                 if ( stage_4_result.getResult().equals('SUCCESS') ) {
206                     stage_4_archive = true;
207                 }
208             }
209         }
210
211         // override to save the artifacts
212         if ( params.SAVE_ARTIFACTS_OVERRIDE || stage_4_archive ) {
213             stage("Archive") {
214                 sh "echo ${container_name} > build_version.txt"
215                 archiveArtifacts artifacts: "build_version.txt", fingerprint: true
216
217                 // Archive the tested repo
218                 dir("repo/${RELEASE}") {
219                     ci_helper.archive(params.ARTIFACTORY_SERVER,RELEASE,GERRIT_BRANCH,'tested')
220                 }
221             }
222         }
223     }
224     catch(caughtError) {
225         println("Caught error!")
226         error = caughtError
227         currentBuild.result = 'FAILURE'
228     }
229     finally {
230         sh "docker stop ${http_server_name}"
231
232         if (error) {
233             if ( !params.SAVE_CONTAINER_ON_FAIL ) {
234                 sh "lxc delete ${container_name} --force"
235             }
236             throw error 
237         }
238         else {
239             if ( !params.SAVE_CONTAINER_ON_PASS ) {
240                 sh "lxc delete ${container_name} --force"
241             }
242         }
243     }
244 }