Revert "Update stage1 and stage3 to run robot tests with tag"
[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 /* Change log:
18  * 1. Bug 699 : Jayant Madavi, Mrityunjay Yadav : JM00553988@techmahindra.com : 23-july-2019 : Improvement to the code, now using post syntax
19  * 2. Jayant Madavi : 26-july-2019 : optimization to the previous check-in added currentBuild.result = 'SUCCESS'. TODO: code would be better *    if we use pipeline declarative 
20  */
21  
22 stage_3_merge_result = ''
23 def Get_MDG(project) {
24     // split the project.
25     def values = project.split('/')
26     if ( values.size() > 1 ) {
27         return values[1]
28     }
29     // no prefix, likely just the project name then
30     return project
31 }
32
33 node("${params.NODE}") {
34
35     mdg = Get_MDG("${GERRIT_PROJECT}")
36     println("MDG is ${mdg}")
37
38     if ( params.PROJECT_URL_PREFIX == null )
39     {
40         params.PROJECT_URL_PREFIX  = 'https://osm.etsi.org/gerrit'
41     }
42
43     stage('downstream') {
44         // default to stage_2 (patchset)
45         def stage_name = "stage_2"
46
47         try {
48             switch(GERRIT_EVENT_TYPE) {
49                 case "change-merged":
50                    stage_name = "stage_2-merge"
51                    break
52
53                 case "patchset-created":
54                    stage_name = "stage_2"
55                    break
56             }
57         }
58         catch(caughtError) {
59             println("No gerrit event found")
60         }
61
62         do_stage_4 = false
63         if (params.DO_STAGE_4)
64         {
65             do_stage_4 = params.DO_STAGE_4
66         }
67
68         // pipeline running from gerrit trigger.
69         // kickoff the downstream multibranch pipeline
70         def downstream_params = [
71             string(name: 'GERRIT_BRANCH', value: GERRIT_BRANCH),
72             string(name: 'GERRIT_PROJECT', value: GERRIT_PROJECT),
73             string(name: 'GERRIT_REFSPEC', value: GERRIT_REFSPEC),
74             string(name: 'GERRIT_PATCHSET_REVISION', value: GERRIT_PATCHSET_REVISION),
75             string(name: 'PROJECT_URL_PREFIX', value: params.PROJECT_URL_PREFIX),
76             string(name: 'DOCKER_TAG', value: params.DOCKER_TAG),
77             booleanParam(name: 'TEST_INSTALL', value: params.TEST_INSTALL),
78             booleanParam(name: 'DO_ROBOT', value: params.DO_ROBOT),
79             booleanParam(name: 'DO_STAGE_4', value: do_stage_4),
80         ]
81      
82         if ( params.STAGE )
83         {
84             // go directly to stage 3 (osm system)
85             stage_name = params.STAGE
86             mdg = "osm"
87             if ( ! params.TEST_INSTALL )
88             {
89                 println("disabling stage_3 invocation")
90                 return
91             }
92         }
93         // callout to stage_2.  This is a multi-branch pipeline.
94         downstream_job_name = "${mdg}-${stage_name}/${GERRIT_BRANCH}"
95
96         println("TEST_INSTALL = ${params.TEST_INSTALL}, downstream job: ${downstream_job_name}")
97                 currentBuild.result = 'SUCCESS'
98         try {
99             stage_3_merge_result = build job: "${downstream_job_name}", parameters: downstream_params, propagate: true
100             if (stage_3_merge_result.getResult() != 'SUCCESS') {
101                 project = stage_3_merge_result.getProjectName()
102                 build = stage_3_merge_result.getNumber()
103                 // Jayant if the build fails the below error will cause the pipeline to terminate. 
104                             // error("${project} build ${build} failed")
105                                 currentBuild.result = stage_3_merge_result.getResult()
106             }
107         }
108         catch(caughtError) {
109             echo 'Exception in stage_1'
110             currentBuild.result = 'FAILURE'
111         }
112         finally {
113             try {
114                 if((currentBuild.result != 'SUCCESS') && (env.JOB_NAME == 'daily-stage_4')){
115                     emailext (
116                         subject: "[OSM-Jenkins] Job: ${env.JOB_NAME} Build: ${env.BUILD_NUMBER} Result: ${currentBuild.result}",
117                         body: """ Check console output at "${env.BUILD_URL}"  """,
118                         to: 'OSM_MDL@list.etsi.org',
119                         recipientProviders: [culprits()]
120                     )
121                 }
122             }
123             catch(caughtError) {
124                 echo "Failure in executing email"
125             }
126         }
127     }
128 }
129