update to jenkins file
[test.git] / Jenkinsfile
1 def Get_MDG(project) {
2     // split the project.
3     def values = project.split('/')
4     if ( values.size() > 1 ) {
5         return values[1]
6     }
7     // no prefix, likely just the project name then
8     return project
9 }
10
11 def project_checkout() {
12     // checkout the project
13     git url: "https://osm.etsi.org/gerrit/${env.GERRIT_PROJECT}"
14
15     sh "git fetch origin ${env.GERRIT_REFSPEC}"
16     if (env.GERRIT_PATCHSET_REVISION.size() > 0 ) {
17         sh "git checkout -f ${env.GERRIT_PATCHSET_REVISION}"
18     }
19 }
20
21 def devops_checkout() {
22     dir('devops') {
23         git url: 'https://osm.etsi.org/gerrit/osm/devops'
24     }
25 }
26
27 node {
28     mdg = Get_MDG("${env.GERRIT_PROJECT}")
29     println("MDG is ${mdg}")
30
31     if ( env.GERRIT_EVENT_TYPE.equals('change-merged') ) {
32         def downstream_params = [
33             string(name: 'GERRIT_BRANCH', value: env.GERRIT_BRANCH),
34             string(name: 'GERRIT_PROJECT', value: env.GERRIT_PROJECT),
35             string(name: 'GERRIT_REFSPEC', value: env.GERRIT_REFSPEC),
36             string(name: 'GERRIT_PATCHSET_REVISION', value: env.GERRIT_PATCHSET_REVISION),
37         ]
38         result = build job: "${mdg}/${env.GERRIT_BRANCH}", parameters: downstream_params, propagate: true
39         if (result.getResult() != 'SUCCESS') {
40             project = result.getProjectName()
41             build = result.getNumber()
42             error("${project} build ${build} failed")
43         }
44     }
45     else {
46         stage('Prepare') {
47             sh 'env'
48             devops_checkout()
49         }
50
51         stage('Checkout') {
52             project_checkout()
53         }
54
55         container_name = "${env.GERRIT_PROJECT}-${env.GERRIT_BRANCH}"
56
57
58         stage('Docker-Build') {
59             sh "docker build -t ${container_name} ."
60         }
61
62         withDockerContainer("${container_name}") {
63             stage('Docker-Setup') {
64                 sh '''
65                    groupadd -o -g $(id -g) -r jenkins
66                    useradd -o -u $(id -u) --create-home -r -g  jenkins jenkins
67                    '''
68             }
69             stage('Test') {
70                 sh 'devops-stages/stage-test.sh'
71             }
72             stage('Build') {
73                 sh 'devops-stages/stage-build.sh'
74             }
75             stage('Archive') {
76                 sh 'devops-stages/stage-archive.sh'
77                 archiveArtifacts artifacts: "dists/**,pool/${mdg}/*.deb", fingerprint: true
78             }
79         }
80      }
81 }