Skip to content
Snippets Groups Projects
Jenkinsfile-GitLab-Docker 21.64 KiB
#!/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
 */

//-------------------------------------------------------------------------------
// Abstraction function to send social media messages:
// like on Slack or Mattermost
def sendSocialMediaMessage(pipeChannel, pipeColor, pipeMessage) {
  if (params.pipelineUsesSlack != null) {
    if (params.pipelineUsesSlack) {
      slackSend channel: pipeChannel, color: pipeColor, message: pipeMessage
    }
  }
}

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

// for lock
def cn_ci_resource = params.DockerContainers
def ds_tester_ci_resource = params.DsTester

// When triggered by upstream, specify which tag to use
def upstreamTagToUse = params.upstreamTagToUse

// Location of the CN tester
def dsT_host_flag = false
def dsT_host = ""
def dsT_host_user = ""

// dsTester tag to use
def dsTesterTag = params.DSTESTER_TAG

// Flags
def scmEvent = false
def upstreamEvent = false
def deployed = true
def mini = false
def basic = false
def slice = false

// Default tags / branches  --> could be passed on by upstream job or by PR content
def nrfTag = params.nrfTag
def nrfBranch = params.nrfBranch
def amfTag = params.amfTag
def amfBranch = params.amfBranch
def smfTag = params.smfTag
def smfBranch = params.smfBranch
def spgwuTag = params.spgwuTag
def spgwuBranch = params.spgwuBranch

def ausfTag = params.ausfTag
def ausfBranch = params.ausfBranch
def udmTag = params.udmTag
def udmBranch = params.udmBranch
def udrTag = params.udrTag
def udrBranch = params.udrBranch
def nssfTag = params.udrTag
def nssfBranch = params.udrBranch

//-------------------------------------------------------------------------------
// Pipeline start
pipeline {
  agent {
    label cn_ci_host
  }
  options {
    disableConcurrentBuilds()
    timestamps()
    ansiColor('xterm')
    lock(cn_ci_resource)
  }
  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()

          echo "Node       is ${NODE_NAME}"

          if (params.DS_Tester_Server_Flag != null) {
            dsT_host_flag = params.DS_Tester_Server_Flag
            if (dsT_host_flag) {
              def allParametersPresent = true
              if (params.DS_Tester_Server_Name == null) {
                allParametersPresent = false
              } else {
                dsT_host = params.DS_Tester_Server_Name
              }
              if (params.DS_Tester_Server_Login == null) {
                allParametersPresent = false
              } else {
                dsT_host_user = params.DS_Tester_Server_Login
              }
              if (allParametersPresent) {
                echo "DS Tester  is on ${dsT_host}"
              } else {
                echo "Some DS Tester parameters are missing!"
                sh "./ci-scripts/fail.sh"
              }
            }
          }

          // Find out the cause of the trigger
          for (cause in currentBuild.getBuildCauses()) {
            if (cause.toString() ==~ /.*UpstreamCause.*/) {
              upstreamEvent = true
            //} else {
            //  scmEvent = true
            }
          }

          if (upstreamEvent) {
            if (params.NRF_TAG != null) {
              nrfTag = params.NRF_TAG
              echo "Upstream Job passed NRF_TAG to use: ${nrfTag}"
              mini = true
              basic = true
            }
            if (params.NRF_BRANCH != null) {
              nrfBranch = params.NRF_BRANCH
              echo "Upstream Job passed NRF_BRANCH to use: ${nrfBranch}"
            }
            if (params.AMF_TAG != null) {
              amfTag = params.AMF_TAG
              echo "Upstream Job passed AMF_TAG to use: ${amfTag}"
              mini = true
              basic = true
              slice = true
            }
            if (params.AMF_BRANCH != null) {
              amfBranch = params.AMF_BRANCH
              echo "Upstream Job passed AMF_BRANCH to use: ${amfBranch}"
            }
            if (params.SMF_TAG != null) {
              smfTag = params.SMF_TAG
              echo "Upstream Job passed SMF_TAG to use: ${smfTag}"
              mini = true
              basic = true
            }
            if (params.SMF_BRANCH != null) {
              smfBranch = params.SMF_BRANCH
              echo "Upstream Job passed SMF_BRANCH to use: ${smfBranch}"
            }
            if (params.SPGWU_TAG != null) {
              spgwuTag = params.SPGWU_TAG
              echo "Upstream Job passed SPGWU_TAG to use: ${spgwuTag}"
              mini = true
              basic = true
            }
            if (params.SPGWU_BRANCH != null) {
              spgwuBranch = params.SPGWU_BRANCH
              echo "Upstream Job passed SPGWU_BRANCH to use: ${spgwuBranch}"
            }
            if (params.AUSF_TAG != null) {
              ausfTag = params.AUSF_TAG
              echo "Upstream Job passed AUSF_TAG to use: ${ausfTag}"
              basic = true
            }
            if (params.AUSF_BRANCH != null) {
              ausfBranch = params.AUSF_BRANCH
              echo "Upstream Job passed AUSF_BRANCH to use: ${ausfBranch}"
            }
            if (params.UDM_TAG != null) {
              udmTag = params.UDM_TAG
              echo "Upstream Job passed UDM_TAG to use: ${udmTag}"
              basic = true
            }
            if (params.UDM_BRANCH != null) {
              udmBranch = params.UDM_BRANCH
              echo "Upstream Job passed UDM_BRANCH to use: ${udmBranch}"
            }
            if (params.UDR_TAG != null) {
              udrTag = params.UDR_TAG
              echo "Upstream Job passed UDR_TAG to use: ${udrTag}"
              basic = true
            }
            if (params.UDR_BRANCH != null) {
              udrBranch = params.UDR_BRANCH
              echo "Upstream Job passed UDR_BRANCH to use: ${udrBranch}"
            }
            if (params.NSSF_TAG != null) {
              nssfTag = params.NSSF_TAG
              echo "Upstream Job passed NSSF_TAG to use: ${nssfTag}"
              slice = true
            }
            if (params.NSSF_BRANCH != null) {
              nssfBranch = params.NSSF_BRANCH
              echo "Upstream Job passed NSSF_BRANCH to use: ${nssfBranch}"
            }
            sh "git clean -x -d -f > /dev/null 2>&1"
            sh "git fetch --prune > /dev/null 2>&1"
            sh 'git checkout -f ' + upstreamTagToUse
            sh "zip -r -qq oai-cn5g-fed.zip .git"
            sh "mkdir -p archives"
            sh './scripts/syncComponents.sh --nrf-branch ' + nrfBranch + ' --amf-branch ' + amfBranch + ' --smf-branch ' + smfBranch + ' --spgwu-tiny-branch ' + spgwuBranch + ' --ausf-branch ' + ausfBranch + ' --udm-branch ' + udmBranch + ' --udr-branch ' + udrBranch
          }
          if (scmEvent) {
            mini = true
            basic = true
            slice = true
            sh "git clean -x -d -f > /dev/null 2>&1"
            if ("MERGE".equals(env.gitlabActionType)) {
              sh "./ci-scripts/doGitLabMerge.sh --src-branch ${env.gitlabSourceBranch} --src-commit ${env.gitlabMergeRequestLastCommit} --target-branch ${env.gitlabTargetBranch} --target-commit ${GIT_COMMIT}"
            }
            sh "zip -r -qq oai-cn5g-fed.zip .git"
            sh "mkdir -p archives"
            sh './scripts/syncComponents.sh --nrf-branch develop --amf-branch develop --smf-branch develop --spgwu-tiny-branch develop --ausf-branch develop --udm-branch develop --udr-branch develop'
          }
          if ((!upstreamEvent) && (!scmEvent)) {
            mini = true
            basic = true
            slice = true
            sh "git clean -x -d -f > /dev/null 2>&1"
            sh './scripts/syncComponents.sh --nrf-branch ' + nrfBranch + ' --amf-branch ' + amfBranch + ' --smf-branch ' + smfBranch + ' --spgwu-tiny-branch ' + spgwuBranch + ' --ausf-branch ' + ausfBranch + ' --udm-branch ' + udmBranch + ' --udr-branch ' + udrBranch
            sh "mkdir -p archives"
          }
          // Verify that the images are available
          try {
            sh 'echo "OAI_NRF_TAG: oai-nrf:' + nrfTag +'" > archives/oai_nrf_image_info.log'
            sh 'docker image inspect --format=\'Size = {{.Size}} bytes\' oai-nrf:' + nrfTag + ' >> archives/oai_nrf_image_info.log'
            sh 'docker image inspect --format=\'Date = {{.Created}}\' oai-nrf:' + nrfTag + ' >> archives/oai_nrf_image_info.log'
          } catch (Exception e) {
            error "OAI NRF Image tag to test does not exist!"
          }
          try {
            sh 'echo "OAI_AMF_TAG: oai-amf:' + amfTag +'" > archives/oai_amf_image_info.log'
            sh 'docker image inspect --format=\'Size = {{.Size}} bytes\' oai-amf:' + amfTag + ' >> archives/oai_amf_image_info.log'
            sh 'docker image inspect --format=\'Date = {{.Created}}\' oai-amf:' + amfTag + ' >> archives/oai_amf_image_info.log'
          } catch (Exception e) {
            error "OAI AMF Image tag to test does not exist!"
          }
          try {
            sh 'echo "OAI_SMF_TAG: oai-smf:' + smfTag +'" > archives/oai_smf_image_info.log'
            sh 'docker image inspect --format=\'Size = {{.Size}} bytes\' oai-smf:' + smfTag + ' >> archives/oai_smf_image_info.log'
            sh 'docker image inspect --format=\'Date = {{.Created}}\' oai-smf:' + smfTag + ' >> archives/oai_smf_image_info.log'
          } catch (Exception e) {
            error "OAI SMF Image tag to test does not exist!"
          }
          try {
            sh 'echo "OAI_SPGWU_TAG: oai-spgwu-tiny:' + spgwuTag +'" > archives/oai_spgwu_image_info.log'
            sh 'docker image inspect --format=\'Size = {{.Size}} bytes\' oai-spgwu-tiny:' + spgwuTag + ' >> archives/oai_spgwu_image_info.log'
            sh 'docker image inspect --format=\'Date = {{.Created}}\' oai-spgwu-tiny:' + spgwuTag + ' >> archives/oai_spgwu_image_info.log'
          } catch (Exception e) {
            error "OAI SPGW-U-Tiny Image tag to test does not exist!"
          }
          try {
            sh 'echo "OAI_AUSF_TAG: oai-ausf:' + ausfTag +'" > archives/oai_ausf_image_info.log'
            sh 'docker image inspect --format=\'Size = {{.Size}} bytes\' oai-ausf:' + ausfTag + ' >> archives/oai_ausf_image_info.log'
            sh 'docker image inspect --format=\'Date = {{.Created}}\' oai-ausf:' + ausfTag + ' >> archives/oai_ausf_image_info.log'
          } catch (Exception e) {
            error "OAI AUSF Image tag to test does not exist!"
          }
          try {
            sh 'echo "OAI_UDM_TAG: oai-udm:' + udmTag +'" > archives/oai_udm_image_info.log'
            sh 'docker image inspect --format=\'Size = {{.Size}} bytes\' oai-udm:' + udmTag + ' >> archives/oai_udm_image_info.log'
            sh 'docker image inspect --format=\'Date = {{.Created}}\' oai-udm:' + udmTag + ' >> archives/oai_udm_image_info.log'
          } catch (Exception e) {
            error "OAI UDM Image tag to test does not exist!"
          }
          try {
            sh 'echo "OAI_UDR_TAG: oai-udr:' + udrTag +'" > archives/oai_udr_image_info.log'
            sh 'docker image inspect --format=\'Size = {{.Size}} bytes\' oai-udr:' + udrTag + ' >> archives/oai_udr_image_info.log'
            sh 'docker image inspect --format=\'Date = {{.Created}}\' oai-udr:' + udrTag + ' >> archives/oai_udr_image_info.log'
          } catch (Exception e) {
            error "OAI UDR Image tag to test does not exist!"
          }
          try {
            sh 'echo "OAI_NSSF_TAG: oai-nssf:' + nssfTag +'" > archives/oai_nssf_image_info.log'
            sh 'docker image inspect --format=\'Size = {{.Size}} bytes\' oai-nssf:' + nssfTag + ' >> archives/oai_nssf_image_info.log'
            sh 'docker image inspect --format=\'Date = {{.Created}}\' oai-nssf:' + nssfTag + ' >> archives/oai_nssf_image_info.log'
          } catch (Exception e) {
            error "OAI NSSF Image tag to test does not exist!"
          }
        }
      }
    }
    stage ('Deploy 5G Core Network (mini) and Test with DS Tester') {
      when {
        allOf {
          expression {dsT_host_flag}
          expression {mini == true}
        } 
      }
      steps {
        lock (ds_tester_ci_resource) {
          script {
            echo '\u2705 \u001B[32mDeploy CN5G-Minimal using Docker-Compose and Testing with DS Tester\u001B[0m'
            sh "mkdir -p RESULTS-MINI"
            if (fileExists("dstester")) {
              sh "rm -Rf dstester > /dev/null 2>&1"
            }
            sh "mkdir -p dstester"
            dir ('dstester') {
              withCredentials([
                [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.dsTesterGitLabRepository_Credentials}", usernameVariable: 'git_username', passwordVariable: 'git_token']
              ]) {
                sh "git clone https://${git_username}:${git_token}@github.com/OPENAIRINTERFACE/chasseur.git . > ../git_clone.log 2>&1"
                sh "git checkout -f " + dsTesterTag + " >> ../git_clone.log 2>&1"
                // First put all correct tags to test
                sh 'sed -i -e "s#NRF_TAG#' + nrfTag + '#" -i -e "s#AMF_TAG#' + amfTag + '#" -i -e "s#SMF_TAG#' + smfTag + '#" -i -e "s#SPGWU_TAG#' + spgwuTag + '#" ./jenkins/suits/dc/integration/integration-mini.yaml'
                // Check the route b/w the container and DSTester n/w and add
                sh 'python3 ../ci-scripts/routeCheck.py --mode Add --userName ' + dsT_host_user + ' --hostName ' + dsT_host + ' --docker_compose docker-compose/integration/integration-mini/integration.tplt'
                dir ('jenkins') {
                  try {
                    sh "python3 ./chasseurdocker.py -f ./suits/dc/integration/integration-mini.yaml | tee ../../RESULTS-MINI/dsTester_Summary_mini.txt"
                  } catch (Exception e) {
                    currentBuild.result = 'FAILURE'
                    echo "dsTester FrameWork FAILED"
                  }
                }
                sh 'python3 ../ci-scripts/routeCheck.py --mode Delete --userName ' + dsT_host_user + ' --hostName ' + dsT_host + ' --docker_compose docker-compose/integration/integration-mini/integration.tplt' 
              }
            }
            try {
              sh "python3 ./ci-scripts/toCheckDSTesterResult.py --type mini"
            } catch (Exception e) {
                currentBuild.result = 'FAILURE'
                echo "dsTester Mini ResultCheck FAILED"
            }
          }  
        }
      }
    }
    stage ('Deploy Whole 5G Core Network (Basic) and Test with DS Tester') {
      when {
        allOf {
          expression {dsT_host_flag}
          expression {basic == true}
        } 
      }
      steps {
        lock (ds_tester_ci_resource) {
          script {
            echo '\u2705 \u001B[32mDeploy CN5G-Basic using Docker-Compose and Testing with DS Tester\u001B[0m'
            sh "mkdir -p RESULTS-BASIC"
            if (fileExists("dstester")) {
              sh "rm -Rf dstester > /dev/null 2>&1"
            }
            sh "mkdir -p dstester"
            dir ('dstester') {
              withCredentials([
                [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.dsTesterGitLabRepository_Credentials}", usernameVariable: 'git_username', passwordVariable: 'git_token']
              ]) {
                sh "git clone https://${git_username}:${git_token}@github.com/OPENAIRINTERFACE/chasseur.git . > ../git_clone.log 2>&1"
                sh "git checkout -f " + dsTesterTag + " >> ../git_clone.log 2>&1"
                // First put all correct tags to test
                sh 'sed -i -e "s#NRF_TAG#' + nrfTag + '#" -i -e "s#AMF_TAG#' + amfTag + '#" -i -e "s#SMF_TAG#' + smfTag + '#" -i -e "s#UPF_TAG#' + spgwuTag + '#" -i -e "s#AUSF_TAG#' + ausfTag + '#" -i -e "s#UDM_TAG#' + udmTag + '#" -i -e "s#UDR_TAG#' + udrTag + '#" ./jenkins/suits/dc/integration/integration-basic.yaml'
                // Check the route b/w the container and DSTester n/w and add
                sh 'python3 ../ci-scripts/routeCheck.py --mode Add --userName ' + dsT_host_user + ' --hostName ' + dsT_host + ' --docker_compose docker-compose/integration/integration-basic/integration.tplt'
                dir ('jenkins') {
                  try {
                    sh "python3 ./chasseurdocker.py -f ./suits/dc/integration/integration-basic.yaml | tee ../../RESULTS-BASIC/dsTester_Summary_basic.txt"
                  } catch (Exception e) {
                    currentBuild.result = 'FAILURE'
                    echo "dsTester FrameWork FAILED"
                  }
                }
              sh 'python3 ../ci-scripts/routeCheck.py --mode Delete --userName ' + dsT_host_user + ' --hostName ' + dsT_host + ' --docker_compose docker-compose/integration/integration-basic/integration.tplt'
              }
            }
            try{
            sh "python3 ./ci-scripts/toCheckDSTesterResult.py --type basic"
            } catch (Exception e) {
              currentBuild.result = 'FAILURE'
              echo "dsTester Basic ResultCheck FAILED"
            }
          }  
        }
      }
    }
    stage ('Deploy 5G Core Network (slice) and Test with DS Tester') {
      when {
        allOf {
          expression {dsT_host_flag}
          expression {slice == true}
        } 
      }
      steps {
        lock (ds_tester_ci_resource) {
          script {
            echo '\u2705 \u001B[32mDeploy CN5G-Slicing using Docker-Compose and Testing with DS Tester\u001B[0m'
            sh "mkdir -p RESULTS-SLICE"
            if (fileExists("dstester")) {
              sh "rm -Rf dstester > /dev/null 2>&1"
            }
            sh "mkdir -p dstester"
            dir ('dstester') {
              withCredentials([
                [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.dsTesterGitLabRepository_Credentials}", usernameVariable: 'git_username', passwordVariable: 'git_token']
              ]) {
                sh "git clone https://${git_username}:${git_token}@github.com/OPENAIRINTERFACE/chasseur.git . > ../git_clone.log 2>&1"
                sh "git checkout -f " + dsTesterTag + " >> ../git_clone.log 2>&1"
                // First put all correct tags to test
                sh 'sed -i -e "s#NRF_TAG#' + nrfTag + '#" -i -e "s#AMF_TAG#' + amfTag + '#" -i -e "s#SMF_TAG#' + smfTag + '#" -i -e "s#UPF_TAG#' + spgwuTag + '#" -i -e "s#AUSF_TAG#' + ausfTag + '#" -i -e "s#UDM_TAG#' + udmTag + '#" -i -e "s#UDR_TAG#' + udrTag + '#" -i -e "s#NSSF_TAG#' + nssfTag + '#" ./jenkins/suits/dc/integration/integration-slice.yaml'
                // Check the route b/w the container and DSTester n/w and add
                sh 'python3 ../ci-scripts/routeCheck.py --mode Add --userName ' + dsT_host_user + ' --hostName ' + dsT_host + ' --docker_compose docker-compose/integration/integration-slice/integration.tplt'
                dir ('jenkins') {
                  try {
                    sh "python3 ./chasseurdocker.py -f ./suits/dc/integration/integration-slice.yaml | tee ../../RESULTS-SLICE/dsTester_Summary_slice.txt"
                  } catch (Exception e) {
                    currentBuild.result = 'FAILURE'
                    echo "dsTester FrameWork FAILED"
                  }
                }
                sh 'python3 ../ci-scripts/routeCheck.py --mode Delete --userName ' + dsT_host_user + ' --hostName ' + dsT_host + ' --docker_compose docker-compose/integration/integration-slice/integration.tplt' 
              }
            }
            try {
              sh "python3 ./ci-scripts/toCheckDSTesterResult.py --type slice"
            } catch (Exception e) {
                currentBuild.result = 'FAILURE'
                echo "dsTester Slicing ResultCheck FAILED"
            }
          }  
        }
      }
    }
  }
  post {
    always {
      script {
        // Generating the HTML report
        if (mini == true){
          sh 'python3 ./ci-scripts/dsTestGenerateHTMLReport.py --job_name ' + JOB_NAME + ' --job_id ' + BUILD_ID + ' --job_url ' + BUILD_URL + ' --type mini'
        }
        if (slice == true){
          sh 'python3 ./ci-scripts/dsTestGenerateHTMLReport.py --job_name ' + JOB_NAME + ' --job_id ' + BUILD_ID + ' --job_url ' + BUILD_URL + ' --type slice'
        }
        if (basic == true){
          sh 'python3 ./ci-scripts/dsTestGenerateHTMLReport.py --job_name ' + JOB_NAME + ' --job_id ' + BUILD_ID + ' --job_url ' + BUILD_URL + ' --type basic'
        }

        // Zipping all archived log files
        sh "zip -r -qq cn5g_fed_docker_logs.zip archives RESULTS*"
        if (fileExists('cn5g_fed_docker_logs.zip')) {
          archiveArtifacts artifacts: 'cn5g_fed_docker_logs.zip'
        }
        listOfFiles = sh returnStdout: true, script: 'ls test_results*.html'
        String[] htmlFiles = listOfFiles.split("\\n")
        for (htmlFile in htmlFiles) {
          archiveArtifacts artifacts: htmlFile
        }
      }
    }
  }
}