d74de59107c071299fb376b76dcccaf3279e7a09
[osm/devops.git] / jenkins / ci-pipelines / ci_stage_1.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 def Get_MDG(project) {
19     // split the project.
20     def values = project.split('/')
21     if ( values.size() > 1 ) {
22         return values[1]
23     }
24     // no prefix, likely just the project name then
25     return project
26 }
27
28 node("${params.NODE}") {
29
30     mdg = Get_MDG("${GERRIT_PROJECT}")
31     println("MDG is ${mdg}")
32
33     if ( params.PROJECT_URL_PREFIX == null )
34     {
35         params.PROJECT_URL_PREFIX  = 'https://osm.etsi.org/gerrit'
36     }
37
38     stage('downstream') {
39         // default to stage_2 (patchset)
40         def stage_name = "stage_2"
41
42         try {
43             switch(GERRIT_EVENT_TYPE) {
44                 case "change-merged":
45                    stage_name = "stage_2-merge"
46                    break
47
48                 case "patchset-created":
49                    stage_name = "stage_2"
50                    break
51             }
52         }
53         catch(caughtError) {
54             println("No gerrit event found")
55         }
56
57         do_stage_4 = false
58         if (params.DO_STAGE_4)
59         {
60             do_stage_4 = params.DO_STAGE_4
61         }
62
63         // pipeline running from gerrit trigger.
64         // kickoff the downstream multibranch pipeline
65         def downstream_params = [
66             string(name: 'GERRIT_BRANCH', value: GERRIT_BRANCH),
67             string(name: 'GERRIT_PROJECT', value: GERRIT_PROJECT),
68             string(name: 'GERRIT_REFSPEC', value: GERRIT_REFSPEC),
69             string(name: 'GERRIT_PATCHSET_REVISION', value: GERRIT_PATCHSET_REVISION),
70             string(name: 'PROJECT_URL_PREFIX', value: params.PROJECT_URL_PREFIX),
71             booleanParam(name: 'TEST_INSTALL', value: params.TEST_INSTALL),
72             booleanParam(name: 'DO_STAGE_4', value: do_stage_4),
73         ]
74      
75         if ( params.STAGE )
76         {
77             // go directly to stage 3 (osm system)
78             stage_name = params.STAGE
79             mdg = "osm"
80             if ( ! params.TEST_INSTALL )
81             {
82                 println("disabling stage_3 invocation")
83                 return
84             }
85         }
86         // callout to stage_2.  This is a multi-branch pipeline.
87         downstream_job_name = "${mdg}-${stage_name}/${GERRIT_BRANCH}"
88
89         println("TEST_INSTALL = ${params.TEST_INSTALL}, downstream job: ${downstream_job_name}")
90
91         stage_2_result = build job: "${downstream_job_name}", parameters: downstream_params, propagate: true
92         if (stage_2_result.getResult() != 'SUCCESS') {
93             project = stage_2_result.getProjectName()
94             build = stage_2_result.getNumber()
95             error("${project} build ${build} failed")
96         }
97     }
98 }