Jenkinsfile-GitLab-Docker 16.3 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
#!/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 = ""

36
37
38
39
// Tags/Branches to use
def smf_tag = "develop"
def smf_branch = "develop"

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//-------------------------------------------------------------------------------
// 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",
55
      "Code Formatting Checker",
56
      "Test with DsTester"
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
82
83
84
85
86
87
88
89
90
91
92
93
94
    ])

  }

  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}"
        }
      }
    }
    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}"
95
96
            smf_tag = "ci-tmp"
            smf_branch = env.gitlabSourceBranch
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
          } 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") {
130
131
132
133
134
135
136
137
                myShCmd('docker image prune --force', new_host_flag, new_host_user, new_host)
                if ("PUSH".equals(env.gitlabActionType)) {
                  // Currently this pipeline only runs for pushes to `develop` branch
                  // First clean image registry
                  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"
138
139
                  }
                }
140
                myShCmd('docker build --target oai-smf --tag oai-smf:' + smf_tag + ' --file docker/Dockerfile.ubuntu18.04 --build-arg NEEDED_GIT_PROXY="http://proxy.eurecom.fr:8080" . > archives/smf_docker_image_build.log 2>&1', new_host_flag, new_host_user, new_host)
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
                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)
ismail's avatar
ismail committed
166
167
                myShCmd('docker exec 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 ci-cn-cppcheck /bin/bash -c "apt-get install --yes cppcheck bzip2" >> archives/cppcheck_install.log', new_host_flag, new_host_user, new_host)
168
169

                myShCmd('docker cp ./openair-smf.tar.bz2 ci-cn-cppcheck:/home', new_host_flag, new_host_user, new_host)
ismail's avatar
ismail committed
170
171
                myShCmd('docker exec 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 ci-cn-cppcheck /bin/bash -c "rm -f /home/openair-smf.tar.bz2"', new_host_flag, new_host_user, new_host)
172

ismail's avatar
ismail committed
173
                myShCmd('docker exec 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)
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
              }
            }
          }
          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)
ismail's avatar
ismail committed
201
202
                myShCmd('docker exec 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 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)
203
204

                myShCmd('docker cp ./openair-smf.tar.bz2 ci-cn-clang-formatter:/home', new_host_flag, new_host_user, new_host)
ismail's avatar
ismail committed
205
206
                myShCmd('docker exec 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 ci-cn-clang-formatter /bin/bash -c "rm -f /home/openair-smf.tar.bz2"', new_host_flag, new_host_user, new_host)
207
208
209
210

                // 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)) {
ismail's avatar
ismail committed
211
                  myShCmd('docker exec 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)
212
                } else {
ismail's avatar
ismail committed
213
                  myShCmd('docker exec ci-cn-clang-formatter /bin/bash -c "cd /home && ./ci-scripts/checkCodingFormattingRules.sh"', new_host_flag, new_host_user, new_host)
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
                }
              }
            }
          }
          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)
              }
            }
          }
        }
      }
    }
238
239
240
241
    stage ('Testing in CN-5G-FED environment') {
      steps {
        script {
          gitlabCommitStatus(name: "Test with DsTester") {
ismail's avatar
ismail committed
242
            localStatus = build job: params.CN5G_FED_PipelineName,
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
              parameters: [
                string(name: 'SMF_TAG', value: String.valueOf(smf_tag)),
                string(name: 'SMF_BRANCH', value: String.valueOf(smf_branch))
              ], propagate: false
            localResult = localStatus.getResult()

            if (localStatus.resultIsBetterOrEqualTo('SUCCESS')) {
              echo "Federation Test Job is OK"
            } else {
              echo "Federation Test Job is KO"
              sh "ci-scripts/fail.sh"
            }
          }
        }
      }
      post {
        always {
          script {
ismail's avatar
ismail committed
261
            copyArtifacts(projectName: params.CN5G_FED_PipelineName,
262
263
264
265
266
267
                          filter: '*_results_oai_cn5g.html',
                          selector: lastCompleted())
          }
        }
      }
    }
268
269
  }
  post {
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
    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
        }
      }
    }
288
289
290
291
292
    cleanup {
      script {
        // Removing temporary / intermediate images
        try {
          if ("MERGE".equals(env.gitlabActionType)) {
293
            myShCmd('docker image rm --force oai-smf:ci-tmp', new_host_flag, new_host_user, new_host)
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
          }
        } catch (Exception e) {
          echo "We failed to delete the OAI-SMF temp image"
        }
        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
309
310
311
312
313
314
315
316
317
318
319

        // 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'
        }
320
321
322
323
        if (fileExists('deploy_results_oai_cn5g.html')) {
          sh "sed -i -e 's#TEMPLATE_TIME#${JOB_TIMESTAMP}#' *_results_oai_cn5g.html"
          archiveArtifacts artifacts: '*_results_oai_cn5g.html'
        }
Raphael Defosseux's avatar
Raphael Defosseux committed
324
325

        // Sending email to commiter
ismail's avatar
ismail committed
326
327
328
329
330
        if (params.sendToCommitterEmail != null) {
          if (params.sendToCommitterEmail) {
            emailext attachmentsPattern: '*results*.html',
              body: '''Hi,

Raphael Defosseux's avatar
Raphael Defosseux committed
331
332
333
334
335

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

Regards,
OAI CI Team''',
ismail's avatar
ismail committed
336
337
338
339
340
341
              replyTo: 'no-reply@openairinterface.org',
              subject: '$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!',
              to: gitCommitAuthorEmailAddr
          }
        }

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
      }
    }
  }
}

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
}