Jakub Josef | d75c1d5 | 2017-03-15 18:06:03 +0100 | [diff] [blame] | 1 | /** |
azvyagintsev | 3d30140 | 2019-01-08 13:23:59 +0200 | [diff] [blame] | 2 | * Groovy syntax code testing pipeline |
| 3 | * CREDENTIALS_ID - gerrit credentials id |
| 4 | * GRADLE_IMAGE - gradle image name |
| 5 | * |
| 6 | **/ |
Jakub Josef | d75c1d5 | 2017-03-15 18:06:03 +0100 | [diff] [blame] | 7 | |
Jakub Josef | d75c1d5 | 2017-03-15 18:06:03 +0100 | [diff] [blame] | 8 | common = new com.mirantis.mk.Common() |
| 9 | |
azvyagintsev | 3d30140 | 2019-01-08 13:23:59 +0200 | [diff] [blame] | 10 | // Guess username for acessing git repo |
| 11 | String git_user = '' |
| 12 | if (env.GERRIT_USER) { |
| 13 | git_user = "${env.GERRIT_USER}@" |
| 14 | } else if (env.CREDENTIALS_ID) { |
| 15 | def mkCommon = new com.mirantis.mk.Common() |
| 16 | def cred = mkCommon.getCredentials(env.CREDENTIALS_ID, 'key') |
| 17 | git_user = "${cred.username}@" |
Jakub Josef | e1407ac | 2017-03-30 14:10:19 +0200 | [diff] [blame] | 18 | } |
| 19 | |
azvyagintsev | 3d30140 | 2019-01-08 13:23:59 +0200 | [diff] [blame] | 20 | // Use gerrit parameters if set with fallback to job param |
azvyagintsev | e8ac11a | 2019-01-08 13:46:24 +0200 | [diff] [blame] | 21 | String git_repo_url = env.GERRIT_HOST ? "${env.GERRIT_SCHEME}://${git_user}${env.GERRIT_HOST}:${env.GERRIT_PORT}/${env.GERRIT_PROJECT}" : env.DEFAULT_GIT_URL |
| 22 | String git_ref = env.GERRIT_REFSPEC ?: env.DEFAULT_GIT_REF |
azvyagintsev | 3d30140 | 2019-01-08 13:23:59 +0200 | [diff] [blame] | 23 | String git_credentials_id = env.CREDENTIALS_ID |
| 24 | |
| 25 | String docker_registry = env.DOCKER_REGISTRY ?: 'docker-dev-virtual.docker.mirantis.net' |
| 26 | String docker_image_name = env.IMAGE_NAME ?: 'mirantis/openstack-ci/jenkins-job-tests:latest' |
| 27 | |
| 28 | String slave_label = env.SLAVE_LABEL ?: 'docker' |
| 29 | |
| 30 | String docker_image = (docker_registry ? "${docker_registry}/" : '') + "${docker_image_name}" |
| 31 | |
| 32 | String gradle_report_dir = 'build/reports/codenarc' |
| 33 | String gradle_report_path = gradle_report_dir + '/main.*' |
| 34 | String gradle_log_path = gradle_report_dir + '/main.log' |
| 35 | |
| 36 | String jjb_dir = 'jenkins-jobs' |
| 37 | |
| 38 | // Make codenarc happy |
| 39 | String scm_class = 'GitSCM' |
| 40 | |
| 41 | String reporting_config = ''' |
| 42 | codenarcMain { |
| 43 | reports { |
| 44 | text.enabled = true |
| 45 | html.enabled = true |
| 46 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 47 | } |
azvyagintsev | 3d30140 | 2019-01-08 13:23:59 +0200 | [diff] [blame] | 48 | ''' |
| 49 | |
| 50 | // Set current build description |
| 51 | if (env.GERRIT_CHANGE_URL) { |
| 52 | currentBuild.description = """ |
| 53 | <p> |
| 54 | Triggered by change: <a href="${env.GERRIT_CHANGE_URL}">${env.GERRIT_CHANGE_NUMBER},${env.GERRIT_PATCHSET_NUMBER}</a><br/> |
| 55 | Project: <b>${env.GERRIT_PROJECT}</b><br/> |
| 56 | Branch: <b>${env.GERRIT_BRANCH}</b><br/> |
| 57 | Subject: <b>${env.GERRIT_CHANGE_SUBJECT}</b><br/> |
| 58 | </p> |
| 59 | """ |
| 60 | } else { |
| 61 | currentBuild.description = """ |
| 62 | <p> |
| 63 | Triggered manually<br/> |
| 64 | Git repository URL: <b>${git_repo_url}</b><br/> |
| 65 | Git revision: <b>${git_ref}</b><br/> |
| 66 | </p> |
| 67 | """ |
| 68 | } |
| 69 | timeout(time: 1, unit: 'HOURS') { |
| 70 | node(slave_label) { |
| 71 | // Get & prepare source code |
| 72 | stage('SCM checkout') { |
| 73 | echo "Checking out git repository from ${git_repo_url} @ ${git_ref}" |
| 74 | |
| 75 | checkout([ |
| 76 | $class : scm_class, |
| 77 | branches : [ |
| 78 | [name: 'FETCH_HEAD'], |
| 79 | ], |
| 80 | userRemoteConfigs: [ |
| 81 | [url: git_repo_url, refspec: git_ref, credentialsId: git_credentials_id], |
| 82 | ], |
| 83 | extensions : [ |
| 84 | [$class: 'WipeWorkspace'], |
| 85 | ], |
| 86 | ]) |
| 87 | |
| 88 | echo 'Checking out mcp-ci/jenkins-jobs for default configs' |
| 89 | String jjb_repo_url = "${env.DEFAULT_GIT_BASE_URL ?: 'https://gerrit.mcp.mirantis.net'}/mcp-ci/jenkins-jobs" |
| 90 | checkout([ |
| 91 | $class : scm_class, |
| 92 | userRemoteConfigs: [ |
| 93 | [url: jjb_repo_url, credentialsId: env.CREDENTIALS_ID], |
| 94 | ], |
| 95 | extensions : [ |
| 96 | [$class: 'RelativeTargetDirectory', relativeTargetDir: jjb_dir] |
| 97 | ], |
| 98 | ]) |
| 99 | } |
| 100 | |
| 101 | // Run test |
| 102 | stage('CodeNarc') { |
| 103 | // Check existence of configuration files and use ones from jenkins-jobs if not exists |
| 104 | sh "test -f build.gradle || cp ${jjb_dir}/build.gradle ." |
| 105 | sh "test -f codenarcRules.groovy || cp ${jjb_dir}/codenarcRules.groovy ." |
| 106 | // Remove not needed anymore jenkins-jobs project |
| 107 | dir(jjb_dir) { |
| 108 | deleteDir() |
Jakub Josef | d75c1d5 | 2017-03-15 18:06:03 +0100 | [diff] [blame] | 109 | } |
azvyagintsev | 3d30140 | 2019-01-08 13:23:59 +0200 | [diff] [blame] | 110 | |
| 111 | // Force HTML and plain text reports |
| 112 | sh "sed -ri '/^\\s*reportFormat\\s*=/ d' build.gradle" // Remove existing report config |
| 113 | sh "echo '${reporting_config}' >> build.gradle" // Append new one report configuration |
| 114 | |
| 115 | String userID = sh([script: 'id -u', returnStdout: true]).trim() |
| 116 | String docker_args = [ |
| 117 | '-u root', |
| 118 | '-t', |
| 119 | '--privileged', |
| 120 | "-e 'WORKSPACE=${env.WORKSPACE}'", |
| 121 | "-w '${env.WORKSPACE}'", |
| 122 | "-v '${env.WORKSPACE}':'${env.WORKSPACE}'", |
| 123 | "--name '${env.BUILD_TAG}'", |
| 124 | ].join(' ') |
| 125 | |
| 126 | def dockerImage = docker.image(docker_image) |
| 127 | dockerImage.pull() |
| 128 | |
| 129 | sh "mkdir -p ${gradle_report_dir}" |
| 130 | catchError { |
| 131 | dockerImage.withRun(docker_args, '/bin/cat') { |
| 132 | sh "docker exec -t -u root ${env.BUILD_TAG} usermod -u ${userID} jenkins" |
| 133 | sh "docker exec -t -u jenkins ${env.BUILD_TAG} gradle --no-daemon --info --console=plain --offline check 2>&1 | tee ${gradle_log_path}" |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 134 | } |
azvyagintsev | 3d30140 | 2019-01-08 13:23:59 +0200 | [diff] [blame] | 135 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 136 | |
azvyagintsev | 3d30140 | 2019-01-08 13:23:59 +0200 | [diff] [blame] | 137 | String gradle_log = readFile([file: gradle_log_path]) |
| 138 | |
| 139 | // Don't fail immediately to archive artifacts |
| 140 | catchError { |
| 141 | // Using pipe without shell option `pipefail` hides errors by the exit status of the latest command |
| 142 | // Check gradle output explicitly |
| 143 | if (gradle_log ==~ /(?ms).*Build failed with an exception.*/) { |
| 144 | error 'TEST FAILED!' |
| 145 | } |
| 146 | |
| 147 | // Normally compilation failure doesn't fail the build |
| 148 | if (gradle_log ==~ /(?ms).*Compilation failed.*/) { |
| 149 | error 'COMPILATION FAILED!' |
| 150 | } |
| 151 | |
| 152 | // Fail if there are internal errors |
| 153 | if (gradle_log ==~ /(?ms).*Error processing filePath.*/) { |
| 154 | error 'ERROR PROCESSING SOME FILE(S)!' |
| 155 | } |
| 156 | } |
| 157 | sh 'cat build/reports/codenarc/main.txt' |
| 158 | common.infoMsg("CodeNarc HTML report: ${env.BUILD_URL}artifact/build/reports/codenarc/main.html") |
| 159 | |
| 160 | if (currentBuild.resultIsWorseOrEqualTo('UNSTABLE')) { |
| 161 | setGerritReview customUrl: "- ${env.JOB_NAME} ${env.BUILD_URL}artifact/build/reports/codenarc/main.html" |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // Save results |
| 166 | stage('Record test results') { |
| 167 | archiveArtifacts([ |
| 168 | artifacts : gradle_report_path, |
| 169 | allowEmptyArchive: true, |
| 170 | ]) |
| 171 | } |
| 172 | } |
Jakub Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame] | 173 | } |