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