From 403db5f66ebdc652fc683a1dccc083e2376ecd4e Mon Sep 17 00:00:00 2001
From: Raphael Defosseux <raphael.defosseux@eurecom.fr>
Date: Sat, 6 Feb 2021 18:58:46 +0100
Subject: [PATCH] CI: adding conditional stages in the parent pipeline based on
 labels in Merge Requests

Signed-off-by: Raphael Defosseux <raphael.defosseux@eurecom.fr>
---
 ci-scripts/Jenkinsfile-gitlab               | 113 +++++++++++++++-----
 ci-scripts/checkGitLabMergeRequestLabels.sh | 108 +++++++++++++++++++
 2 files changed, 194 insertions(+), 27 deletions(-)
 create mode 100755 ci-scripts/checkGitLabMergeRequestLabels.sh

diff --git a/ci-scripts/Jenkinsfile-gitlab b/ci-scripts/Jenkinsfile-gitlab
index 9a6f3639548..ff661543c00 100644
--- a/ci-scripts/Jenkinsfile-gitlab
+++ b/ci-scripts/Jenkinsfile-gitlab
@@ -38,6 +38,10 @@ def nodeExecutor = params.nodeExecutor
 // VM Lockable resources
 def vmResource = params.vmLockableResource
 
+// Tags to shorten pipeline duration
+def doMandatoryTests = false
+def doFullTestsuite = false
+
 pipeline {
     agent {
         label nodeExecutor
@@ -46,31 +50,6 @@ pipeline {
         disableConcurrentBuilds()
         timestamps()
         gitLabConnection('OAI GitLab')
-        gitlabBuilds(builds: [
-            "Build gNB-USRP",
-            "Build nr-UE-USRP",
-            "Build eNB-USRP",
-            "Build basic-sim",
-            "Build phy-sim",
-            "Build eNB-ethernet",
-            "Build UE-ethernet",
-            "Analysis with cppcheck",
-            "Test phy-sim",
-            "Test basic-sim",
-            "Test L1-sim",
-            "Test RF-sim",
-            "Test L2-sim",
-            "Test-Mono-FDD-Band7",
-            "Test-Mono-TDD-Band40",
-            "Test-IF4p5-FDD-Band7",
-            "Test-IF4p5-TDD-Band40",
-            "Test-Mono-FDD-Band13-LTE-M",
-            "Test-IF4p5-TDD-Band38-Multi-RRU",
-            "Test-eNB-OAI-UE-FDD-Band7",
-            "Test-Mono-FDD-Band13-X2-HO",
-            "Test-TDD-Band78-gNB-NR-UE",
-            "Test-OCP-FDD-Band7"
-        ])
         ansiColor('xterm')
     }
 
@@ -93,6 +72,33 @@ pipeline {
                     if (fileExists("flexran")) {
                         sh "rm -Rf flexran > /dev/null 2>&1"
                     }
+
+                    echo '\u2705 \u001B[32mVerify Labels\u001B[0m'
+                    if ("MERGE".equals(env.gitlabActionType)) {
+                        LABEL_CHECK = sh returnStdout: true, script: 'ci-scripts/checkGitLabMergeRequestLabels.sh --mr-id ' + env.gitlabMergeRequestIid
+                        LABEL_CHECK = LABEL_CHECK.trim()
+                        if (LABEL_CHECK == 'NONE') {
+                            def message = "OAI " + JOB_NAME + " build (" + BUILD_ID + "): Your merge request has none of the mandatory labels:\n\n"
+                            message += " - BUILD-ONLY\n"
+                            message += " - 4G-LTE\n"
+                            message += " - 5G-NR\n"
+                            message += " - CI\n\n"
+                            message += "Not performing CI due to lack of labels"
+                            addGitLabMRComment comment: message
+                            error('Not performing CI due to lack of labels')
+                        } else if (LABEL_CHECK == 'FULL') {
+                            doMandatoryTests = true
+                            doFullTestsuite = true
+                        } else if (LABEL_CHECK == 'SHORTEN-5G') {
+                            doMandatoryTests = true
+                        } else {
+                            def message = "OAI " + JOB_NAME + " build (" + BUILD_ID + "): We will perform only build stages on your Merge Request"
+                            addGitLabMRComment comment: message
+                        }
+                    } else {
+                        doMandatoryTests = true
+                        doFullTestsuite = true
+                    }
                 }
             }
         }
@@ -135,7 +141,7 @@ pipeline {
                         // For the moment, there is no fail criteria. Just a notification of number of files that do not follow
                         sh "./ci-scripts/checkCodingFormattingRules.sh"
                     }
-                    if (doFlexranCtrlTest) {
+                    if (doFlexranCtrlTest && doMandatoryTests) {
                         sh "mkdir flexran"
                         dir ('flexran') {
                             withCredentials([
@@ -375,6 +381,9 @@ pipeline {
                 stage ("VM-based tests") {
                     stages {
                         stage ("Test physical simulators") {
+                            when {
+                                expression {doMandatoryTests}
+                            }
                             steps {
                               lock (vmResource) {
                                 script {
@@ -393,7 +402,7 @@ pipeline {
                         }
                         stage ("Build Flexran Controller") {
                             when {
-                                expression {doFlexranCtrlTest}
+                                expression {doFlexranCtrlTest && doMandatoryTests}
                             }
                             steps {
                               lock (vmResource) {
@@ -410,6 +419,9 @@ pipeline {
                             }
                         }
                         stage ("Test basic simulator") {
+                            when {
+                                expression {doMandatoryTests}
+                            }
                             steps {
                               lock (vmResource) {
                                 script {
@@ -427,6 +439,9 @@ pipeline {
                             }
                         }
                         stage ("Test L1 simulator") {
+                            when {
+                                expression {doMandatoryTests}
+                            }
                             steps {
                               lock (vmResource) {
                                 script {
@@ -444,6 +459,9 @@ pipeline {
                             }
                         }
                         stage ("Test RF simulator") {
+                            when {
+                                expression {doMandatoryTests}
+                            }
                             steps {
                               lock (vmResource) {
                                 script {
@@ -461,6 +479,9 @@ pipeline {
                             }
                         }
                         stage ("Test L2 simulator") {
+                            when {
+                                expression {doFullTestsuite}
+                            }
                             steps {
                               lock (vmResource) {
                                 script {
@@ -478,6 +499,9 @@ pipeline {
                             }
                         }
                         stage ("Destroy all Virtual Machines") {
+                            when {
+                                expression {doMandatoryTests}
+                            }
                             steps {
                               lock (vmResource) {
                                 sh "./ci-scripts/oai-ci-vm-tool destroy --job-name ${JOB_NAME} --build-id ${BUILD_ID}"
@@ -487,6 +511,9 @@ pipeline {
                     }
                 }
                 stage ("Test MONOLITHIC - FDD - Band 7 - B210") {
+                    when {
+                        expression {doFullTestsuite}
+                    }
                     steps {
                         script {
                             triggerSlaveJob ('eNB-CI-FDD-Band7-B210', 'Test-Mono-FDD-Band7')
@@ -506,6 +533,9 @@ pipeline {
                     }
                 }
                 stage ("Test MONOLITHIC - TDD - Band 40 - B210") {
+                    when {
+                        expression {doFullTestsuite}
+                    }
                     steps {
                         script {
                             triggerSlaveJob ('eNB-CI-TDD-Band40-B210', 'Test-Mono-TDD-Band40')
@@ -525,6 +555,9 @@ pipeline {
                     }
                 }
                 stage ("Test IF4p5 - FDD - Band 7 - B210") {
+                    when {
+                        expression {doFullTestsuite}
+                    }
                     steps {
                         script {
                             sh "sleep 60"
@@ -545,6 +578,9 @@ pipeline {
                     }
                 }
                 stage ("Test IF4p5 - TDD - Band 40 - B210") {
+                    when {
+                        expression {doFullTestsuite}
+                    }
                     steps {
                         script {
                             sh "sleep 60"
@@ -565,6 +601,9 @@ pipeline {
                     }
                 }
                 stage ("Test MONOLITHIC - FDD - Band 13 - B210") {
+                    when {
+                        expression {doFullTestsuite}
+                    }
                     steps {
                         script {
                             sh "sleep 60"
@@ -585,6 +624,9 @@ pipeline {
                     }
                 }
                 stage ("Test X2 Handover - FDD - Band 13 - B210") {
+                    when {
+                        expression {doFullTestsuite}
+                    }
                     steps {
                         script {
                             sh "sleep 60"
@@ -605,6 +647,9 @@ pipeline {
                     }
                 }
                 stage ("Test IF4p5 - TDD - Band 38 - B210 - MultiRRU") {
+                    when {
+                        expression {doFullTestsuite}
+                    }
                     steps {
                         script {
                             triggerSlaveJob ('eNB-CI-IF4p5-TDD-Band38-MultiRRU-B210', 'Test-IF4p5-TDD-Band38-Multi-RRU')
@@ -624,6 +669,9 @@ pipeline {
                     }
                 }
                 stage ("Test OAI UE - FDD - Band 20 - B200") {
+                    when {
+                        expression {doFullTestsuite}
+                    }
                     steps {
                         script {
                             triggerSlaveJobNoGitLab ('UE-CI-FDD-Band20-B200')
@@ -643,6 +691,9 @@ pipeline {
                     }
                 }
                 stage ("Test OAI UE - OAI eNB - FDD - Band 7 - B200") {
+                    when {
+                        expression {doFullTestsuite}
+                    }
                     steps {
                         script {
                             // Delayed trigger on slave job, so it is always the last one to run
@@ -664,6 +715,9 @@ pipeline {
                     }
                 }
                 stage ("Test OAI NR UE - OAI gNB - TDD - Band 78 - N300") {
+                    when {
+                        expression {doMandatoryTests}
+                    }
                     steps {
                         script {
                             triggerSlaveJob ('gNB-nrUE-MONO-TDD-Band78-N300', 'Test-TDD-Band78-gNB-NR-UE')
@@ -683,6 +737,9 @@ pipeline {
                     }
                 }
                 stage ("Test OAI OCP-eNB - FDD - Band 7 - B210") {
+                    when {
+                        expression {doFullTestsuite}
+                    }
                     steps {
                         script {
                             triggerSlaveJob ('OCPeNB-FDD-Band7-B210', 'Test-OCP-FDD-Band7')
@@ -705,6 +762,7 @@ pipeline {
             post {
                 always {
                     script {
+                      if (doMandatoryTests) {
                         dir ('archives') {
                             sh "if [ -d basic_sim/test ] || [ -d phy_sim/test ] || [ -d l2_sim/test ]; then zip -r -qq vm_tests_logs.zip */test ; fi"
                         }
@@ -720,6 +778,7 @@ pipeline {
                                 archiveArtifacts artifacts: 'test_simulator_results.html'
                             }
                         }
+                      }
                     }
                 }
             }
diff --git a/ci-scripts/checkGitLabMergeRequestLabels.sh b/ci-scripts/checkGitLabMergeRequestLabels.sh
new file mode 100755
index 00000000000..7e4ea1c7de5
--- /dev/null
+++ b/ci-scripts/checkGitLabMergeRequestLabels.sh
@@ -0,0 +1,108 @@
+#!/bin/bash
+#/*
+# * 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
+# */
+
+function usage {
+    echo "OAI GitLab merge request applying script"
+    echo "   Original Author: Raphael Defosseux"
+    echo ""
+    echo "Usage:"
+    echo "------"
+    echo ""
+    echo "    checkGitLabMergeRequestLabels.sh [OPTIONS]"
+    echo ""
+    echo "Options:"
+    echo "------------------"
+    echo ""
+    echo "    --mr-id ####"
+    echo "    Specify the ID of the merge request."
+    echo ""
+    echo "    --help OR -h"
+    echo "    Print this help message."
+    echo ""
+}
+
+if [ $# -ne 2 ] && [ $# -ne 1 ]
+then
+    echo "Syntax Error: not the correct number of arguments"
+    echo ""
+    usage
+    exit 1
+fi
+
+while [[ $# -gt 0 ]]
+do
+key="$1"
+
+case $key in
+    -h|--help)
+    shift
+    usage
+    exit 0
+    ;;
+    --mr-id)
+    MERGE_REQUEST_ID="$2"
+    shift
+    shift
+    ;;
+    *)
+    echo "Syntax Error: unknown option: $key"
+    echo ""
+    usage
+    exit 1
+esac
+done
+
+LABELS=`curl --silent "https://gitlab.eurecom.fr/api/v4/projects/oai%2Fopenairinterface5g/merge_requests/$MERGE_REQUEST_ID" | jq '.labels' || true`
+
+IS_MR_BUILD_ONLY=`echo $LABELS | grep -c BUILD-ONLY || true`
+IS_MR_CI=`echo $LABELS | grep -c CI || true`
+IS_MR_4G=`echo $LABELS | grep -c 4G-LTE || true`
+IS_MR_5G=`echo $LABELS | grep -c 5G-NR || true`
+
+# First case: none is present! No CI
+if [ $IS_MR_BUILD_ONLY -eq 0 ] && [ $IS_MR_CI -eq 0 ] && [ $IS_MR_4G -eq 0 ] && [ $IS_MR_5G -eq 0 ]
+then
+    echo "NONE"
+    exit 0
+fi
+
+# Second case: Build-Only
+if [ $IS_MR_BUILD_ONLY -eq 1 ]
+then
+    echo "BUILD-ONLY"
+    exit 0
+fi
+
+# Third case: CI or 4G label --> Full CI run
+if [ $IS_MR_4G -eq 1 ] || [ $IS_MR_CI -eq 1 ] 
+then
+    echo "FULL"
+    exit 0
+fi
+
+# Fourth case: 5G label
+if [ $IS_MR_BUILD_ONLY -eq 0 ] && [ $IS_MR_CI -eq 0 ] && [ $IS_MR_4G -eq 0 ] && [ $IS_MR_5G -eq 1 ]
+then
+    echo "SHORTEN-5G"
+    exit 0
+fi
+
-- 
GitLab