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