Initial commit for new pipeline in NBI 96/9696/1
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Fri, 11 Sep 2020 09:13:47 +0000 (09:13 +0000)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Fri, 11 Sep 2020 09:13:47 +0000 (09:13 +0000)
Change-Id: I60c6ec968359583a5fff23a91cba9848bddc7256
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
Jenkinsfile
Jenkinsfile.new [new file with mode: 0644]
devops-stages/new-pipeline/stage-lint.sh [new file with mode: 0755]
devops-stages/new-pipeline/stage-test.sh [new file with mode: 0755]

index e283f05..789b3c7 100644 (file)
@@ -31,16 +31,58 @@ def devops_checkout() {
 }
 
 node('docker') {
-    checkout scm
-    devops_checkout()
-
-    ci_stage_2 = load "devops/jenkins/ci-pipelines/ci_stage_2.groovy"
-    ci_stage_2.ci_pipeline( 'NBI',
-                           params.PROJECT_URL_PREFIX,
-                           params.GERRIT_PROJECT,
-                           params.GERRIT_BRANCH,
-                           params.GERRIT_REFSPEC,
-                           params.GERRIT_PATCHSET_REVISION,
-                           params.TEST_INSTALL,
-                           params.ARTIFACTORY_SERVER)
+    stage('CURRENT PIPELINE') {
+        checkout scm
+        devops_checkout()
+
+        ci_stage_2 = load "devops/jenkins/ci-pipelines/ci_stage_2.groovy"
+        ci_stage_2.ci_pipeline('NBI',
+                               params.PROJECT_URL_PREFIX,
+                               params.GERRIT_PROJECT,
+                               params.GERRIT_BRANCH,
+                               params.GERRIT_REFSPEC,
+                               params.GERRIT_PATCHSET_REVISION,
+                               // params.TEST_INSTALL,
+                               false
+                               params.ARTIFACTORY_SERVER)
+    }
+    stage('NEW PIPELINE') {
+        try {
+            switch(GERRIT_EVENT_TYPE) {
+                case "change-merged":
+                   stage_name = "merge"
+                   break
+
+                case "patchset-created":
+                   stage_name = "patchset"
+                   break
+            }
+        }
+        catch(caughtError) {
+            println("No gerrit event found")
+        }
+
+        def downstream_params = [
+            string(name: 'GERRIT_BRANCH', value: params.GERRIT_BRANCH),
+            string(name: 'GERRIT_PROJECT', value: params.GERRIT_PROJECT),
+            string(name: 'GERRIT_REFSPEC', value: params.GERRIT_REFSPEC),
+            string(name: 'GERRIT_PATCHSET_REVISION', value: params.GERRIT_PATCHSET_REVISION),
+            string(name: 'PROJECT_URL_PREFIX', value: params.PROJECT_URL_PREFIX),
+            booleanParam(name: 'TEST_INSTALL', value: params.TEST_INSTALL),
+            // next params will have to be updated with the proper values
+            string(name: 'IMAGENAME', value: 'opensourcemano/nbi'),
+            string(name: 'DOCKER_REGISTRY, value: 'localhost:5000'),
+            string(name: 'DOCKER_REGISTRY_PROTOCOL', value: 'http://'),
+            string(name: 'DOCKER_CREDENTIALS', value: '')
+        ]
+
+        downstream_job_name = "NBI-${stage_name}"
+        echo "Triggering NEW PIPELINE"
+        build job: "${downstream_job_name}", parameters: downstream_params
+        // build job: "${downstream_job_name}", parameters: downstream_params, propagate: true
+        // It calls the new pipeline defined in Jenkinsfile.new
+        //
+        //build job: '<Job name>', parameters: [[$class: 'StringParameterValue', name: 'param1', value: 'test_param']]
+    }
 }
+
diff --git a/Jenkinsfile.new b/Jenkinsfile.new
new file mode 100644 (file)
index 0000000..d9b53dd
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+  Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
+
+  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.
+*/
+
+pipeline {
+    agent none
+    parameters {
+        string(defaultValue: env.BRANCH_NAME, description: '', name: 'GERRIT_BRANCH')
+        string(defaultValue: 'osm/NBI', description: '', name: 'GERRIT_PROJECT')
+        string(defaultValue: env.GERRIT_REFSPEC, description: '', name: 'GERRIT_REFSPEC')
+        string(defaultValue: env.GERRIT_PATCHSET_REVISION, description: '', name: 'GERRIT_PATCHSET_REVISION')
+        string(defaultValue: 'https://osm.etsi.org/gerrit', description: '', name: 'PROJECT_URL_PREFIX')
+        booleanParam(defaultValue: false, description: '', name: 'TEST_INSTALL')
+        // string(defaultValue: 'artifactory-osm', description: '', name: 'ARTIFACTORY_SERVER')
+        // New parameters for Docker image build
+        string(defaultValue: 'opensourcemano/nbi', description: 'Docker Image Name', name: 'IMAGENAME')
+        string(defaultValue: 'localhost:5000', description: 'Docker Registry', name: 'DOCKER_REGISTRY')
+        string(defaultValue: 'http://', description: 'Docker Registry protocol', name: 'DOCKER_REGISTRY_PROTOCOL')
+        string(defaultValue: '', description: 'ID of Docker Registry Credentials', name: 'DOCKER_CREDENTIALS') // `defaultValue` to be updated with actual ID in Jenkins whenever needed
+    }
+    stages {
+        stage('LICENSE SCAN') {
+            agent { label 'osm1' }
+            steps {
+                echo "Clones Devops repo:"
+                dir('devops') {
+                    git url: "${params.PROJECT_URL_PREFIX}/osm/devops", branch: "${params.GERRIT_BRANCH}"
+                }
+                echo "License scan:"
+                sh "devops/tools/license_scan.sh"
+            }
+        }
+        stage('PRE-CHECKS AND UNIT TESTS') {
+            agent {
+                dockerfile {
+                    filename 'Dockerfile.testing'
+                }
+            }
+            stages {
+                stage('Linting tests') {
+                    steps {
+                        echo "Linting tests (flake8):"
+                        sh "devops-stages/new-pipeline/stage-lint.sh"
+                    }
+                }
+                stage('Unit tests') {
+                    steps {
+                        echo "Unit tests (unittest):"
+                        sh "devops-stages/new-pipeline/stage-test.sh"
+                    }
+                }
+            }
+            post {
+                always {
+                    echo "Saves all the results:"
+                    // archiveArtifacts artifacts: 'relative/path/to/files/**/*.txt', fingerprint: true
+                }
+            }
+        }
+        stage('DOCKER IMAGE BUILD AND PUSH') {
+            agent { label 'osm2' }
+            stages {
+                stage('Build image') {
+                    steps{
+                        echo "Building ${params.IMAGENAME}:${env.BUILD_NUMBER} image:"
+                        script {
+                            dockerImage = docker.build "${params.IMAGENAME}:${env.BUILD_NUMBER}"
+                        }
+                    }
+                }
+                stage('Upload to private registry') {
+                    steps{
+                        echo "Here it would upload the image with appropriate tags: build number, latest:"
+                        // script {
+                        //     docker.withRegistry( params.DOCKER_REGISTRY_PROTOCOL + params.DOCKER_REGISTRY, params.DOCKER_CREDENTIALS ) {
+                        //         dockerImage.push("${env.BUILD_NUMBER}")
+                        //         dockerImage.push("${params.GERRIT_BRANCH}-latest")
+                        //     }
+                        // }
+                    }
+                }
+                stage('Remove unused Docker image') {
+                    steps{
+                        echo "Here it will remove the docker image"
+                        // sh "docker rmi ${params.IMAGENAME}:${env.BUILD_NUMBER}"
+
+                        // // Name format for private registry
+                        // sh "docker rmi ${params.DOCKER_REGISTRY}/${params.IMAGENAME}:${env.BUILD_NUMBER}"
+                        // sh "docker rmi ${params.DOCKER_REGISTRY}/${params.IMAGENAME}:${params.GERRIT_BRANCH}-latest"
+                        // // Name format for default public registry
+                        // //sh "docker rmi ${IMAGENAME}:latest"
+                    }
+                }
+            }
+        }
+        stage('UNIT TESTS OVER RUNNING CONTAINER') {
+            agent {
+                docker {
+                    image "${params.DOCKER_REGISTRY}/${params.IMAGENAME}:${env.BUILD_NUMBER}"
+                }
+            }
+            steps {
+                echo "The image has been successfully instantiated"
+                echo "(no additional unit tests over running container are required for this module)"
+            }
+        }
+        stage('INVOKE E2E SMOKE TESTS') {
+            agent { label 'system' }
+            steps {
+                echo "Triggering E2E tests pipeline"
+                build job: 'E2E-install-and-testing'
+                // As last stage, here it should call the "Deployment and E2E Testing" pipeline with the appropriate parameters to:
+                // - Run smoke tests only
+                // - Avoid pushing images to public Docker Registry (this will be done once Daily)
+                //
+                //build job: '<Job name>', parameters: [[$class: 'StringParameterValue', name: 'param1', value: 'test_param']]
+            }
+        }
+    }
+}
+
diff --git a/devops-stages/new-pipeline/stage-lint.sh b/devops-stages/new-pipeline/stage-lint.sh
new file mode 100755 (executable)
index 0000000..dd38113
--- /dev/null
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
+#
+# 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.
+
+flake8 osm_nbi setup.py --max-line-length 120 --exclude .svn,CVS,.gz,.git,__pycache__,.tox,local,temp,osm_im,.temp-tox --ignore W291,W293,E226,E402,W504
+
diff --git a/devops-stages/new-pipeline/stage-test.sh b/devops-stages/new-pipeline/stage-test.sh
new file mode 100755 (executable)
index 0000000..9ac499d
--- /dev/null
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
+#
+# 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.
+
+coverage erase
+nose2 -C --coverage osm_nbi --plugin nose2.plugins.junitxml -s osm_nbi/tests
+coverage report --omit='*tests*'
+coverage html -d ./cover --omit='*tests*'
+coverage xml -o coverage.xml --omit='*tests*'
+python3 -m unittest discover osm_nbi/tests -v
+