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