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