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