Jenkinsfile-GitLab-Docker 26.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/groovy
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

//-------------------------------------------------------------------------------

// Location of the CN executor node
def cn_ci_host = params.Host_CN_CI_Server

// for lock
def cn_ci_resource = params.DockerContainers

// Location of the 2nd CN executor
def new_host_flag = false
def new_host = ""
def new_host_user = ""

//-------------------------------------------------------------------------------
// Pipeline start
pipeline {
  agent {
    label cn_ci_host
  }
  options {
    disableConcurrentBuilds()
    timestamps()
    ansiColor('xterm')
    lock(cn_ci_resource)
    gitLabConnection('OAI GitLab')
    gitlabBuilds(builds: [
      "Build SMF Image",
      "Static Code Analysis",
51
52
53
54
      "Code Formatting Checker",
      "Build AMF-server",
      "Build UMD-server",
      "Build AMF-client"
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    ])

  }

  stages {
    stage ('Verify Parameters') {
      steps {
        script {
          echo '\u2705 \u001B[32mVerify Parameters\u001B[0m'

          JOB_TIMESTAMP = sh returnStdout: true, script: 'date --utc --rfc-3339=seconds | sed -e "s#+00:00##"'
          JOB_TIMESTAMP = JOB_TIMESTAMP.trim()

          if (params.Host_CN_CI_2nd_Server_Flag != null) {
            new_host_flag = params.Host_CN_CI_2nd_Server_Flag
            if (new_host_flag) {
              new_host = params.Host_CN_CI_2nd_Server
              new_host_user = params.Host_CN_CI_2nd_Server_Login
              echo "1st Node   is ${NODE_NAME}"
              echo "2nd Node   is ${new_host}"
            } else {
              echo "Node       is ${NODE_NAME}"
            }
          } else {
            echo "Node       is ${NODE_NAME}"
          }
          echo "Git URL    is ${GIT_URL}"
Raphael Defosseux's avatar
Raphael Defosseux committed
82
          if (params.PrivateGitLabRepository_Credentials == null) {
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
            echo "No credentials to access Private Repository"
            sh "./ci-scripts/fail.sh"
          }
        }
      }
    }
    stage ('Prepare Source Code') {
      steps {
        script {
          sh "git clean -x -d -f > /dev/null 2>&1"
          if ("MERGE".equals(env.gitlabActionType)) {
            gitCommitAuthorEmailAddr = env.gitlabUserEmail
            echo "GitLab Usermail is ${gitCommitAuthorEmailAddr}"
            sh "./ci-scripts/doGitLabMerge.sh --src-branch ${env.gitlabSourceBranch} --src-commit ${env.gitlabMergeRequestLastCommit} --target-branch ${env.gitlabTargetBranch} --target-commit ${GIT_COMMIT}"
          } else {
            echo "Git Branch      is ${GIT_BRANCH}"
            echo "Git Commit      is ${GIT_COMMIT}"
            gitCommitAuthorEmailAddr = sh returnStdout: true, script: 'git log -n1 --pretty=format:%ae ${GIT_COMMIT}'
            gitCommitAuthorEmailAddr = gitCommitAuthorEmailAddr.trim()
            echo "GitLab Usermail is ${gitCommitAuthorEmailAddr}"
            sh "git log -n1 --pretty=format:\"%s\" > .git/CI_COMMIT_MSG"
          }
          sh "tar -cjhf /tmp/openair-smf.tar.bz2 ."
          sh "mv /tmp/openair-smf.tar.bz2 ."
          copyTo2ndServer('openair-smf.tar.bz2', new_host_flag, new_host_user, new_host)
          sh "mkdir -p archives"
          if (new_host_flag) {
            sh "mkdir -p archives/oai-smf-cfg"
          }
        }
      }
      post {
        failure {
          script {
            def message = "OAI " + JOB_NAME + " build (" + BUILD_ID + "): Merge Conflicts -- Cannot perform CI"
            addGitLabMRComment comment: message
            currentBuild.result = 'FAILURE'
          }
        }
      }
    }
    stage('Build Core Network Function') {
      parallel {
        stage ('Build SMF Image') {
          steps {
            script {
              gitlabCommitStatus(name: "Build SMF Image") {
                withCredentials([
                  [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.PrivateGitLabRepository_Credentials}", usernameVariable: 'git_username', passwordVariable: 'git_password']
                  ]) {
                  if ("MERGE".equals(env.gitlabActionType)) {
                    // Building a temporary image
                    myShCmd('docker image prune --force', new_host_flag, new_host_user, new_host)
136
                    myShCmd('docker build --target oai-smf --tag oai-smf:ci-temp --file ci-scripts/Dockerfile.private.ubuntu18.04 --build-arg EURECOM_PROXY="http://proxy.eurecom.fr:8080" --build-arg GITLAB_USERNAME="' + git_username + '" --build-arg GITLAB_PASSWORD="' + git_password + '" --build-arg BUILD_FOR_CI="True" --build-arg CI_SRC_BRANCH="' + env.gitlabSourceBranch + '" --build-arg CI_SRC_COMMIT="' + env.gitlabMergeRequestLastCommit + '" --build-arg CI_DEST_BRANCH="' + env.gitlabTargetBranch + '" . > archives/smf_docker_image_build.log 2>&1', new_host_flag, new_host_user, new_host)
137
138
139
                  } else {
                    // Currently this pipeline only runs for pushes to `develop` branch
                    // First clean image registry
140
141
142
143
144
                    try {
                      myShCmd('docker image rm oai-smf:develop', new_host_flag, new_host_user, new_host)
                    } catch (Exception e) {
                      echo "Maybe a previous build went wrong"
                    }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
                    myShCmd('docker image prune --force', new_host_flag, new_host_user, new_host)
                    myShCmd('docker build --target oai-smf --tag oai-smf:develop --file ci-scripts/Dockerfile.private.ubuntu18.04 --build-arg EURECOM_PROXY="http://proxy.eurecom.fr:8080" --build-arg GITLAB_USERNAME="' + git_username + '" --build-arg GITLAB_PASSWORD="' + git_password + '" --build-arg CI_SRC_BRANCH="develop" . > archives/smf_docker_image_build.log 2>&1', new_host_flag, new_host_user, new_host)
                  }
                }
                myShCmd('docker image ls >> archives/smf_docker_image_build.log', new_host_flag, new_host_user, new_host)
              }
            }
          }
          post {
            always {
              script {
                copyFrom2ndServer('archives/smf_docker_image_build.log', 'archives', new_host_flag, new_host_user, new_host)
              }
            }
            success {
              sh "echo 'OAI-SMF DOCKER IMAGE BUILD: OK' >> archives/smf_docker_image_build.log"
            }
            unsuccessful {
              sh "echo 'OAI-SMF DOCKER IMAGE BUILD: KO' >> archives/smf_docker_image_build.log"
            }
          }
        }
        // Running CPPCHECK in parallel to gain time
        stage ('Static Code Analysis') {
          steps {
            script {
              gitlabCommitStatus(name: "Static Code Analysis") {
                // Running on xenial to have 1.72 version of cppcheck
                myShCmd('docker run --name ci-cn-cppcheck -d ubuntu:xenial /bin/bash -c "sleep infinity"', new_host_flag, new_host_user, new_host)
                myShCmd('docker exec -it ci-cn-cppcheck /bin/bash -c "apt-get update && apt-get upgrade --yes" > archives/cppcheck_install.log', new_host_flag, new_host_user, new_host)
                myShCmd('docker exec -it ci-cn-cppcheck /bin/bash -c "apt-get install --yes cppcheck bzip2" >> archives/cppcheck_install.log', new_host_flag, new_host_user, new_host)

                myShCmd('docker cp ./openair-smf.tar.bz2 ci-cn-cppcheck:/home', new_host_flag, new_host_user, new_host)
                myShCmd('docker exec -it ci-cn-cppcheck /bin/bash -c "cd /home && tar -xjf openair-smf.tar.bz2"', new_host_flag, new_host_user, new_host)
                myShCmd('docker exec -it ci-cn-cppcheck /bin/bash -c "rm -f /home/openair-smf.tar.bz2"', new_host_flag, new_host_user, new_host)

                myShCmd('docker exec -it ci-cn-cppcheck /bin/bash -c "cd /home && cppcheck --enable=warning --force --xml --xml-version=2 --suppressions-list=ci-scripts/cppcheck_suppressions.list src 2> cppcheck.xml 1> cppcheck_build.log"', new_host_flag, new_host_user, new_host)
              }
            }
          }
          post {
            always {
              script {
                myShCmd('docker cp ci-cn-cppcheck:/home/cppcheck.xml archives', new_host_flag, new_host_user, new_host)
                myShCmd('docker cp ci-cn-cppcheck:/home/cppcheck_build.log archives', new_host_flag, new_host_user, new_host)
                copyFrom2ndServer('archives/cppcheck*.*', 'archives', new_host_flag, new_host_user, new_host)
                // no need to keep the cppcheck container
                myShCmd('docker rm -f ci-cn-cppcheck', new_host_flag, new_host_user, new_host)
              }
            }
            success {
              sh "echo 'CPPCHECK: OK' >> archives/cppcheck_install.log"
            }
            unsuccessful {
              sh "echo 'CPPCHECK: KO' >> archives/cppcheck_install.log"
            }
          }
        }
        // Running CLANG-FORMATTING check in parallel to gain time
        stage ('Code Formatting Checker') {
          steps {
            script {
              gitlabCommitStatus(name: "Code Formatting Checker") {
                myShCmd('docker run --name ci-cn-clang-formatter -d ubuntu:bionic /bin/bash -c "sleep infinity"', new_host_flag, new_host_user, new_host)
                myShCmd('docker exec -it ci-cn-clang-formatter /bin/bash -c "apt-get update && apt-get upgrade --yes" > archives/clang_format_install.log', new_host_flag, new_host_user, new_host)
                myShCmd('docker exec -it ci-cn-clang-formatter /bin/bash -c "apt-get install --yes git tree bzip2" >> archives/clang_format_install.log', new_host_flag, new_host_user, new_host)

                myShCmd('docker cp ./openair-smf.tar.bz2 ci-cn-clang-formatter:/home', new_host_flag, new_host_user, new_host)
                myShCmd('docker exec -it ci-cn-clang-formatter /bin/bash -c "cd /home && tar -xjf openair-smf.tar.bz2"', new_host_flag, new_host_user, new_host)
                myShCmd('docker exec -it ci-cn-clang-formatter /bin/bash -c "rm -f /home/openair-smf.tar.bz2"', new_host_flag, new_host_user, new_host)

                // We install a dedicated version (installed on our CI server).
                myShCmd('docker cp /opt/clang-format/9.0.0/bin/clang-format ci-cn-clang-formatter:/usr/local/bin', new_host_flag, new_host_user, new_host)
                if ("MERGE".equals(env.gitlabActionType)) {
219
                  myShCmd('docker exec -it ci-cn-clang-formatter /bin/bash -c "cd /home && ./ci-scripts/checkCodingFormattingRules.sh --src-branch ' + env.gitlabSourceBranch +' --target-branch ' + env.gitlabTargetBranch + '"', new_host_flag, new_host_user, new_host)
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
                } else {
                  myShCmd('docker exec -it ci-cn-clang-formatter /bin/bash -c "cd /home && ./ci-scripts/checkCodingFormattingRules.sh"', new_host_flag, new_host_user, new_host)
                }
              }
            }
          }
          post {
            always {
              script {
                myShCmd('docker cp ci-cn-clang-formatter:/home/src/oai_rules_result.txt src', new_host_flag, new_host_user, new_host)
                // May not have been generated
                try {
                  myShCmd('docker cp ci-cn-clang-formatter:/home/src/oai_rules_result_list.txt src', new_host_flag, new_host_user, new_host)
                } catch (Exception e) {
                  echo "Failed to copy src/oai_rules_result_list.txt! It may not have been generated. That's OK!"
                }
                copyFrom2ndServer('archives/clang_format*.*', 'archives', new_host_flag, new_host_user, new_host)
                copyFrom2ndServer('src/oai_rules*.*', 'src', new_host_flag, new_host_user, new_host)
                // no need to keep the clang-formatter container
                myShCmd('docker rm -f ci-cn-clang-formatter', new_host_flag, new_host_user, new_host)
              }
            }
          }
        }
      }
    }
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
    stage('Build Test Images') {
      stages {
        stage('Build Test AMF-server') {
          steps {
            script {
              gitlabCommitStatus(name: "Build AMF-server") {
                withCredentials([
                  [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.PrivateGitLabRepository_Credentials}", usernameVariable: 'git_username', passwordVariable: 'git_password']
                  ]) {
                  if ("MERGE".equals(env.gitlabActionType)) {
                    myShCmd('docker build --target test-amf-server --tag test-amf-server:test-deploy --file ci-scripts/Dockerfile.private.amf-server.ubuntu18.04 --build-arg EURECOM_PROXY="http://proxy.eurecom.fr:8080" --build-arg GITLAB_USERNAME="' + git_username + '" --build-arg GITLAB_PASSWORD="' + git_password + '" --build-arg BUILD_FOR_CI="True" --build-arg CI_SRC_BRANCH="' + env.ghprbSourceBranch + '" --build-arg CI_SRC_COMMIT="' + env.ghprbActualCommit + '" --build-arg CI_DEST_BRANCH="develop" . > archives/amf_server_docker_image_build.log 2>&1', new_host_flag, new_host_user, new_host)
                  } else {
                    myShCmd('docker build --target test-amf-server --tag test-amf-server:test-deploy --file ci-scripts/Dockerfile.private.amf-server.ubuntu18.04 --build-arg EURECOM_PROXY="http://proxy.eurecom.fr:8080" --build-arg GITLAB_USERNAME="' + git_username + '" --build-arg GITLAB_PASSWORD="' + git_password + '" --build-arg CI_SRC_BRANCH="develop" . > archives/amf_server_docker_image_build.log 2>&1', new_host_flag, new_host_user, new_host)
                  }
                }
                myShCmd('docker image ls >> archives/amf_server_docker_image_build.log', new_host_flag, new_host_user, new_host)
              }
            }
          }
          post {
            always {
              script {
                copyFrom2ndServer('archives/amf_server_docker_image_build.log', 'archives', new_host_flag, new_host_user, new_host)
              }
            }
            success {
              sh "echo 'TEST-AMF-SERVER DOCKER IMAGE BUILD: OK' >> archives/amf_server_docker_image_build.log"
            }
            unsuccessful {
              sh "echo 'TEST-AMF-SERVER DOCKER IMAGE BUILD: KO' >> archives/amf_server_docker_image_build.log"
            }
          }
        }
        stage('Build Test UMD-server') {
          steps {
            script {
              gitlabCommitStatus(name: "Build UMD-server") {
                withCredentials([
                  [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.PrivateGitLabRepository_Credentials}", usernameVariable: 'git_username', passwordVariable: 'git_password']
                  ]) {
                  if ("MERGE".equals(env.gitlabActionType)) {
Raphael Defosseux's avatar
Raphael Defosseux committed
287
                    myShCmd('docker build --target test-udm-server --tag test-udm-server:test-deploy --file ci-scripts/Dockerfile.private.udm-server.ubuntu18.04 --build-arg EURECOM_PROXY="http://proxy.eurecom.fr:8080" --build-arg GITLAB_USERNAME="' + git_username + '" --build-arg GITLAB_PASSWORD="' + git_password + '" --build-arg BUILD_FOR_CI="True" --build-arg CI_SRC_BRANCH="' + env.ghprbSourceBranch + '" --build-arg CI_SRC_COMMIT="' + env.ghprbActualCommit + '" --build-arg CI_DEST_BRANCH="develop" . > archives/udm_server_docker_image_build.log 2>&1', new_host_flag, new_host_user, new_host)
288
                  } else {
Raphael Defosseux's avatar
Raphael Defosseux committed
289
                    myShCmd('docker build --target test-udm-server --tag test-udm-server:test-deploy --file ci-scripts/Dockerfile.private.udm-server.ubuntu18.04 --build-arg EURECOM_PROXY="http://proxy.eurecom.fr:8080" --build-arg GITLAB_USERNAME="' + git_username + '" --build-arg GITLAB_PASSWORD="' + git_password + '" --build-arg CI_SRC_BRANCH="develop" . > archives/udm_server_docker_image_build.log 2>&1', new_host_flag, new_host_user, new_host)
290
291
                  }
                }
Raphael Defosseux's avatar
Raphael Defosseux committed
292
                myShCmd('docker image ls >> archives/udm_server_docker_image_build.log', new_host_flag, new_host_user, new_host)
293
294
295
296
297
298
              }
            }
          }
          post {
            always {
              script {
Raphael Defosseux's avatar
Raphael Defosseux committed
299
                copyFrom2ndServer('archives/udm_server_docker_image_build.log', 'archives', new_host_flag, new_host_user, new_host)
300
301
302
              }
            }
            success {
Raphael Defosseux's avatar
Raphael Defosseux committed
303
              sh "echo 'TEST-UMD-SERVER DOCKER IMAGE BUILD: OK' >> archives/udm_server_docker_image_build.log"
304
305
            }
            unsuccessful {
Raphael Defosseux's avatar
Raphael Defosseux committed
306
              sh "echo 'TEST-UMD-SERVER DOCKER IMAGE BUILD: KO' >> archives/udm_server_docker_image_build.log"
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
            }
          }
        }
        stage('Build Test AMF-client') {
          steps {
            script {
              gitlabCommitStatus(name: "Build AMF-client") {
                withCredentials([
                  [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.PrivateGitLabRepository_Credentials}", usernameVariable: 'git_username', passwordVariable: 'git_password']
                  ]) {
                  if ("MERGE".equals(env.gitlabActionType)) {
                    myShCmd('docker build --target test-amf-client --tag test-amf-client:test-deploy --file ci-scripts/Dockerfile.private.amf-client.ubuntu18.04 --build-arg EURECOM_PROXY="http://proxy.eurecom.fr:8080" --build-arg GITLAB_USERNAME="' + git_username + '" --build-arg GITLAB_PASSWORD="' + git_password + '" --build-arg BUILD_FOR_CI="True" --build-arg CI_SRC_BRANCH="' + env.ghprbSourceBranch + '" --build-arg CI_SRC_COMMIT="' + env.ghprbActualCommit + '" --build-arg CI_DEST_BRANCH="develop" . > archives/amf_client_docker_image_build.log 2>&1', new_host_flag, new_host_user, new_host)
                  } else {
                    myShCmd('docker build --target test-amf-client --tag test-amf-client:test-deploy --file ci-scripts/Dockerfile.private.amf-client.ubuntu18.04 --build-arg EURECOM_PROXY="http://proxy.eurecom.fr:8080" --build-arg GITLAB_USERNAME="' + git_username + '" --build-arg GITLAB_PASSWORD="' + git_password + '" --build-arg CI_SRC_BRANCH="develop" . > archives/amf_client_docker_image_build.log 2>&1', new_host_flag, new_host_user, new_host)
                  }
                }
                myShCmd('docker image ls >> archives/amf_client_docker_image_build.log', new_host_flag, new_host_user, new_host)
              }
            }
          }
          post {
            always {
              script {
                copyFrom2ndServer('archives/amf_client_docker_image_build.log', 'archives', new_host_flag, new_host_user, new_host)
              }
            }
            success {
              sh "echo 'TEST-AMF-CLIENT DOCKER IMAGE BUILD: OK' >> archives/amf_client_docker_image_build.log"
            }
            unsuccessful {
              sh "echo 'TEST-AMF-CLIENT DOCKER IMAGE BUILD: KO' >> archives/amf_client_docker_image_build.log"
            }
          }
        }
      }
    }
    stage('Deploy Simple Test') {
      stages {
        stage ('Create Networks') {
          steps {
            script {
              myShCmd('docker network create --attachable --subnet 192.168.28.0/24 --ip-range 192.168.28.0/24 ci-sbi', new_host_flag, new_host_user, new_host)
              myShCmd('docker network create --attachable --subnet 192.168.29.0/24 --ip-range 192.168.29.0/24 ci-n4', new_host_flag, new_host_user, new_host)
            }
          }
        }
        stage ('Deploy Containers') {
          steps {
            script {
              if ("MERGE".equals(env.gitlabActionType)) {
                myShCmd('docker run --name ci-oai-smf -d oai-smf:ci-temp /bin/bash -c "sleep infinity"', new_host_flag, new_host_user, new_host)
              } else {
                myShCmd('docker run --name ci-oai-smf -d oai-smf:develop /bin/bash -c "sleep infinity"', new_host_flag, new_host_user, new_host)
              }
              // SBI --> eth1
              myShCmd('docker network connect ci-sbi ci-oai-smf', new_host_flag, new_host_user, new_host)
              // N4 --> eth2
              myShCmd('docker network connect ci-n4 ci-oai-smf', new_host_flag, new_host_user, new_host)
              // Test containers
              myShCmd('docker run --name ci-test-amf-server -d test-amf-server:test-deploy /bin/bash -c "sleep infinity"', new_host_flag, new_host_user, new_host)
              myShCmd('docker network connect ci-sbi ci-test-amf-server', new_host_flag, new_host_user, new_host)
              myShCmd('docker run --name ci-test-udm-server -d test-udm-server:test-deploy /bin/bash -c "sleep infinity"', new_host_flag, new_host_user, new_host)
              myShCmd('docker network connect ci-sbi ci-test-udm-server', new_host_flag, new_host_user, new_host)
              myShCmd('docker run --name ci-test-amf-client -d test-amf-client:test-deploy /bin/bash -c "sleep infinity"', new_host_flag, new_host_user, new_host)
              myShCmd('docker network connect ci-sbi ci-test-amf-client', new_host_flag, new_host_user, new_host)
              // SPGWU as UPF
              myShCmd('docker run --name ci-oai-spgwu -d oai-spgwu-tiny:develop /bin/bash -c "sleep infinity"', new_host_flag, new_host_user, new_host)
              // N4 --> eth1
              myShCmd('docker network connect ci-n4 ci-oai-spgwu', new_host_flag, new_host_user, new_host)
            }
          }
        }
        stage ('Test') {
          steps {
            script {
              sh "sleep 30"
            }
          }
        }
        stage ('Undeploy Containers') {
          steps {
            script {
              myShCmd('docker rm -f ci-oai-smf ci-test-amf-server ci-test-udm-server ci-test-amf-client ci-oai-spgwu', new_host_flag, new_host_user, new_host)
            }
          }
        }
        stage ('Delete Networks') {
          steps {
            script {
              myShCmd('docker network rm ci-sbi ci-n4', new_host_flag, new_host_user, new_host)
            }
          }
        }
      }
    }
402
403
  }
  post {
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
    success {
      script {
        if ("MERGE".equals(env.gitlabActionType)) {
          def message = "OAI " + JOB_NAME + " build (" + BUILD_ID + "): passed (" + BUILD_URL + ")"
          echo "This is a MERGE event"
          addGitLabMRComment comment: message
        }
      }
    }
    unsuccessful {
      script {
        if ("MERGE".equals(env.gitlabActionType)) {
          def message = "OAI " + JOB_NAME + " build (" + BUILD_ID + "): failed (" + BUILD_URL + ")"
          echo "This is a MERGE event"
          addGitLabMRComment comment: message
        }
      }
    }
422
423
    cleanup {
      script {
424
425
426
427
428
429
430
431
432
433
434
435
        // Deleting containers in case of errors
        try {
        } catch (Exception e) {
          echo "We failed to remove any existing containers"
          myShCmd('docker rm -f ci-oai-smf', new_host_flag, new_host_user, new_host)
        }
        // Deleting networks in case of errors
        try {
          myShCmd('docker network rm ci-sbi ci-n4', new_host_flag, new_host_user, new_host)
        } catch (Exception e) {
          echo "We failed to remove any existing networks"
        }
436
437
438
439
440
441
442
443
        // Removing temporary / intermediate images
        try {
          if ("MERGE".equals(env.gitlabActionType)) {
            myShCmd('docker image rm --force oai-smf:ci-temp', new_host_flag, new_host_user, new_host)
          }
        } catch (Exception e) {
          echo "We failed to delete the OAI-SMF temp image"
        }
444
        try {
Raphael Defosseux's avatar
Raphael Defosseux committed
445
          myShCmd('docker image rm --force test-amf-server:test-deploy test-udm-server:test-deploy test-amf-client:test-deploy', new_host_flag, new_host_user, new_host)
446
447
448
        } catch (Exception e) {
          echo "We failed to delete the test temp images"
        }
449
450
451
452
453
454
455
456
457
458
459
        try {
          myShCmd('docker image prune --force', new_host_flag, new_host_user, new_host)
        } catch (Exception e) {
          echo "We failed to prune all unneeded intermediate images"
        }

        // Zipping all archived log files
        sh "zip -r -qq docker_logs.zip archives"
        if (fileExists('docker_logs.zip')) {
          archiveArtifacts artifacts: 'docker_logs.zip'
        }
Raphael Defosseux's avatar
Raphael Defosseux committed
460
461
462
463
464
465
466
467
468
469
470

        // Generating the HTML report
        if ("MERGE".equals(env.gitlabActionType)) {
          sh "python3 ci-scripts/generateHtmlReport.py --job_name=${JOB_NAME} --job_id=${BUILD_ID} --job_url=${BUILD_URL} --git_url=${GIT_URL} --git_src_branch=${env.gitlabSourceBranch} --git_src_commit=${env.gitlabMergeRequestLastCommit} --git_pull_request=True --git_target_branch=${env.gitlabTargetBranch} --git_target_commit=${GIT_COMMIT}"
        } else {
          sh "python3 ci-scripts/generateHtmlReport.py --job_name=${JOB_NAME} --job_id=${BUILD_ID} --job_url=${BUILD_URL} --git_url=${GIT_URL} --git_src_branch=${GIT_BRANCH} --git_src_commit=${GIT_COMMIT}"
        }
        sh "sed -i -e 's#TEMPLATE_TIME#${JOB_TIMESTAMP}#' test_results_oai_smf.html"
        if (fileExists('test_results_oai_smf.html')) {
          archiveArtifacts artifacts: 'test_results_oai_smf.html'
        }
Raphael Defosseux's avatar
Raphael Defosseux committed
471
472
473
474
475
476
477
478
479
480
481
482

        // Sending email to commiter
        emailext attachmentsPattern: '*results*.html',
          body: '''Hi,

Here are attached HTML report files for $PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!

Regards,
OAI CI Team''',
          replyTo: 'no-reply@openairinterface.org',
          subject: '$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!',
          to: gitCommitAuthorEmailAddr
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
      }
    }
  }
}

def copyTo2ndServer(filename, flag, user, host) {
  if (flag) {
    if ("openair-smf.tar.bz2".equals(filename)) {
      sh "ssh ${user}@${host} 'rm -rf /tmp/CI-CN-SMF'"
      sh "ssh ${user}@${host} 'mkdir -p /tmp/CI-CN-SMF'"
    }
    sh "scp ${filename} ${user}@${host}:/tmp/CI-CN-SMF"
    if ("openair-smf.tar.bz2".equals(filename)) {
      sh "ssh ${user}@${host} 'cd /tmp/CI-CN-SMF && tar -xjf ${filename}'"
      sh "ssh ${user}@${host} 'mkdir -p /tmp/CI-CN-SMF/archives'"
      sh "ssh ${user}@${host} 'mkdir -p /tmp/CI-CN-SMF/archives/oai-smf-cfg'"
    }
  }
}

def copyFrom2ndServer(filename, target, flag, user, host) {
  if (flag) {
    sh "scp ${user}@${host}:/tmp/CI-CN-SMF/${filename} ${target}"
  }
}

def myShCmd(cmd, flag, user, host) {
  if (flag) {
    sh "ssh -t -t ${user}@${host} 'cd /tmp/CI-CN-SMF && ${cmd}'"
  } else {
    sh "${cmd}"
  }
}

def myShRetCmd(cmd, flag, user, host) {
  if (flag) {
    ret = sh returnStdout: true, script: "ssh -t -t ${user}@${host} 'cd /tmp/CI-CN-SMF && ${cmd}'"
  } else {
    ret = sh returnStdout: true, script: "${cmd}"
  }
  ret = ret.trim()
  return ret
}