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