Meridio: update node to 2204
[infra/cicd.git] / jjb / nsm / Jenkinsfile
index 4362fc663ea2302429a1b893a3d78499e478d166..e30b92eb6188a32b645e4df0d787262a08983a0b 100644 (file)
@@ -24,7 +24,10 @@ in_progress = 'In Progress.'
 completed = 'Completed.'
 failed = 'Failed'
 
-node ('nordix-nsm-build-ubuntu1804') {
+exception_message_exec = 'failed to execute the following command: '
+exception_message_code_generation = 'Generated code verification failed'
+
+node('nordix-nsm-build-ubuntu2204') {
     build_number = env.BUILD_NUMBER
     workspace = env.WORKSPACE
     ws("${workspace}/${build_number}") {
@@ -38,37 +41,53 @@ node ('nordix-nsm-build-ubuntu1804') {
         def image_registry = params.IMAGE_REGISTRY
         def local_version =  "${env.JOB_NAME}-${build_number}"
 
-        stage('Clone/Checkout') {
-            git branch: default_branch, url: git_project
-            checkout([
-                $class: 'GitSCM',
-                branches: [[name: current_branch]],
-                extensions: [],
-                userRemoteConfigs: [[
-                    refspec: '+refs/pull/*/head:refs/remotes/origin/pr/*',
-                    url: git_project
-                ]]
-            ])
-            sh 'git show'
-        }
-        stage('Verify') {
-            Verify().call()
-        }
-        stage('Docker login') {
-            wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [[password: env.HARBOR_USERNAME, var: 'HARBOR_USERNAME'], [password: env.HARBOR_PASSWORD, var: 'HARBOR_PASSWORD'], [password: image_registry, var: 'IMAGE_REGISTRY']]]) {
-                sh '''#!/bin/bash -eu
-                echo ${HARBOR_PASSWORD} | docker login --username ${HARBOR_USERNAME} --password-stdin ${IMAGE_REGISTRY}
-                '''
+        timeout(30) {
+            stage('Clone/Checkout') {
+                git branch: default_branch, url: git_project
+                checkout([
+                    $class: 'GitSCM',
+                    branches: [[name: current_branch]],
+                    extensions: [],
+                    userRemoteConfigs: [[
+                        refspec: '+refs/pull/*/head:refs/remotes/origin/pr/*',
+                        url: git_project
+                    ]]
+                ])
+                sh 'git show'
+            }
+            stage('Verify') {
+                Verify().call()
+                if (currentBuild.result == 'FAILURE') {
+                    Error('Failed at verification stage').call()
+                }
+            }
+            stage('Docker login') {
+                if (env.DRY_RUN != 'true') {
+                    withCredentials([usernamePassword(credentialsId: 'nordix-cicd-harbor-credentials', passwordVariable: 'HARBOR_PASSWORD', usernameVariable: 'HARBOR_USERNAME')]) {
+                        sh '''#!/bin/bash -eu
+                    echo $HARBOR_PASSWORD | docker login --username $HARBOR_USERNAME --password-stdin $IMAGE_REGISTRY
+                    '''
+                    }
+                } else {
+                    Utils.markStageSkippedForConditional('Docker login')
+                }
+            }
+            stage('Base Image') {
+                BaseImage(version, build_steps, image_registry, local_version).call()
+            }
+            stage('Images') {
+                Images(image_names, version, build_steps, image_registry, local_version).call()
+                if (currentBuild.result == 'FAILURE') {
+                    Error('Failed to build image(s)').call()
+                }
+            }
+            stage('E2E') {
+                if (e2e_enabled == 'true' && env.DRY_RUN != 'true') {
+                    E2e(e2e_enabled).call()
+                } else {
+                    Utils.markStageSkippedForConditional('E2E')
+                }
             }
-        }
-        stage('Base Image') {
-            BaseImage(version, build_steps, image_registry, local_version).call()
-        }
-        stage('Images') {
-            Images(image_names, version, build_steps, image_registry, local_version).call()
-        }
-        stage('E2E') {
-            E2e(e2e_enabled).call()
         }
         stage('Cleanup') {
             Cleanup()
@@ -95,16 +114,15 @@ def UnitTests() {
     return {
         def context = 'Unit Tests'
         stage('Unit Tests') {
+            def command = 'make test'
             try {
                 SetBuildStatus(in_progress, context, pending)
-                sh '''
-                    . \${HOME}/.profile
-                    make test
-                '''
+                ExecSh(command).call()
                 SetBuildStatus(completed, context, success)
             } catch (Exception e) {
                 SetBuildStatus(failed, context, failure)
-                Error(e).call()
+                unstable "${exception_message_exec} ${command}"
+                currentBuild.result = 'FAILURE'
             }
         }
     }
@@ -115,16 +133,15 @@ def Linter() {
     return {
         def context = 'Linter'
         stage('Linter') {
+            def command = 'make lint'
             try {
                 SetBuildStatus(in_progress, context, pending)
-                sh '''
-                    . \${HOME}/.profile
-                   make lint
-                '''
+                ExecSh(command).call()
                 SetBuildStatus(completed, context, success)
             } catch (Exception e) {
                 SetBuildStatus(failed, context, failure)
-                Error(e).call()
+                unstable "${exception_message_exec} ${command}"
+                currentBuild.result = 'FAILURE'
             }
         }
     }
@@ -138,54 +155,34 @@ def Linter() {
 def GeneratedCode() {
     return {
         def context = 'Generated code verification'
-        def exception_message = 'Generated code verification failed'
         SetBuildStatus(in_progress, context, pending)
         stage('go mod tidy') {
+            def command = 'go mod tidy'
             try {
-                sh '''
-                    . \${HOME}/.profile
-                    go mod tidy
-                '''
+                ExecSh(command).call()
                 if (GetModifiedFiles() != '') {
-                    throw new Exception(exception_message)
+                    throw new Exception(exception_message_code_generation)
                 }
             } catch (Exception e) {
                 SetBuildStatus(failed, context, failure)
-                sh 'git diff'
-                sh 'git status -s'
-                Error(e).call()
+                Error(exception_message_exec + command).call()
             }
         }
         stage('go generate ./...') {
+            def command = 'make generate'
             try {
-                sh '''
-                    . \${HOME}/.profile
-                    make generate
-                '''
+                ExecSh(command).call()
                 if (GetModifiedFiles() != '') {
-                    throw new Exception(exception_message)
+                    throw new Exception(exception_message_code_generation)
                 }
             } catch (Exception e) {
                 SetBuildStatus(failed, context, failure)
-                sh 'git diff'
-                sh 'git status -s'
-                Error(e).call()
+                Error(exception_message_exec + command).call()
             }
         }
         stage('Proto') {
             // TODO: protoc version could be different
             Utils.markStageSkippedForConditional('Proto')
-        // try {
-        //     sh 'make proto'
-        //     if (GetModifiedFiles() != '') {
-        //         throw new Exception(exception_message)
-        //     }
-        // } catch (Exception e) {
-        //     SetBuildStatus(failed, context, failure)
-        //     sh 'git diff'
-        //     sh 'git status -s'
-        //     Error(e).call()
-        // }
         }
         SetBuildStatus(completed, context, success)
     }
@@ -216,13 +213,15 @@ def Build(image, version, build_steps, registry, local_version) {
             def in_progress_message = "${in_progress} (${build_steps})"
             def completed_message = "${completed} (${build_steps})"
             def failed_message = "${failed} (${build_steps})"
+            def command = "make ${image} VERSION=${version} BUILD_STEPS='${build_steps}' REGISTRY=${registry} LOCAL_VERSION=${local_version} BASE_IMAGE=${base_image}:${local_version}"
             try {
                 SetBuildStatus(in_progress_message, context, pending)
-                sh "make ${image} VERSION=${version} BUILD_STEPS='${build_steps}' REGISTRY=${registry} LOCAL_VERSION=${local_version} BASE_IMAGE=${base_image}:${local_version}"
+                ExecSh(command).call()
                 SetBuildStatus(completed_message, context, success)
             } catch (Exception e) {
                 SetBuildStatus(failed_message, context, failure)
-                Error(e).call()
+                unstable "${exception_message_exec} ${command}"
+                currentBuild.result = 'FAILURE'
             }
         }
     }
@@ -231,20 +230,16 @@ def Build(image, version, build_steps, registry, local_version) {
 // Run the E2e Tests
 // Currently skipped
 def E2e(e2e_enabled) {
-    if (e2e_enabled == 'true') {
-        return {
-            echo 'make e2e' // todo
-        }
-    } else {
-        return {
-            Utils.markStageSkippedForConditional('E2E')
-        }
+    return {
+        echo 'make e2e' // todo
     }
 }
 
 // Raise error in Jenkins job
 def Error(e) {
     return {
+        sh 'git diff'
+        sh 'git status -s'
         Cleanup()
         error e
     }
@@ -255,17 +250,33 @@ def Cleanup() {
     cleanWs()
 }
 
+// Execute command
+def ExecSh(command) {
+    return {
+        if (env.DRY_RUN != 'true') {
+            sh """
+                . \${HOME}/.profile
+                ${command}
+            """
+        } else {
+            echo "${command}"
+        }
+    }
+}
+
 // Set the commit status on Github
 // https://plugins.jenkins.io/github/#plugin-content-pipeline-examples
 def SetBuildStatus(String message, String context, String state) {
-    step([
-        $class: 'GitHubCommitStatusSetter',
-        reposSource: [$class: 'ManuallyEnteredRepositorySource', url: 'https://github.com/Nordix/Meridio'],
-        commitShaSource: [$class: 'ManuallyEnteredShaSource', sha: GetCommitSha()],
-        contextSource: [$class: 'ManuallyEnteredCommitContextSource', context: context],
-        errorHandlers: [[$class: 'ChangingBuildStatusErrorHandler', result: 'UNSTABLE']],
-        statusResultSource: [ $class: 'ConditionalStatusResultSource', results: [[$class: 'AnyBuildResult', message: message, state: state]] ]
-  ])
+    if (env.DRY_RUN != 'true') {
+        step([
+            $class: 'GitHubCommitStatusSetter',
+            reposSource: [$class: 'ManuallyEnteredRepositorySource', url: 'https://github.com/Nordix/Meridio'],
+            commitShaSource: [$class: 'ManuallyEnteredShaSource', sha: GetCommitSha()],
+            contextSource: [$class: 'ManuallyEnteredCommitContextSource', context: context],
+            errorHandlers: [[$class: 'ShallowAnyErrorHandler']], // Prevent GitHubCommitStatusSetter to set the job status to unstable
+            statusResultSource: [ $class: 'ConditionalStatusResultSource', results: [[$class: 'AnyBuildResult', message: message, state: state]] ]
+        ])
+    }
 }
 
 // Return the current commit sha