blob: 30f1c7b6a2e88729de076b17518abf1a3aa1f5ef [file] [log] [blame]
Jakub Josefd75c1d52017-03-15 18:06:03 +01001/**
azvyagintsev3d301402019-01-08 13:23:59 +02002 * Groovy syntax code testing pipeline
3 * CREDENTIALS_ID - gerrit credentials id
4 * GRADLE_IMAGE - gradle image name
5 *
6 **/
Jakub Josefd75c1d52017-03-15 18:06:03 +01007
Jakub Josefd75c1d52017-03-15 18:06:03 +01008common = new com.mirantis.mk.Common()
9
azvyagintsev3d301402019-01-08 13:23:59 +020010// Guess username for acessing git repo
11String git_user = ''
12if (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 Josefe1407ac2017-03-30 14:10:19 +020018}
19
azvyagintsev3d301402019-01-08 13:23:59 +020020// Use gerrit parameters if set with fallback to job param
azvyagintseve8ac11a2019-01-08 13:46:24 +020021String git_repo_url = env.GERRIT_HOST ? "${env.GERRIT_SCHEME}://${git_user}${env.GERRIT_HOST}:${env.GERRIT_PORT}/${env.GERRIT_PROJECT}" : env.DEFAULT_GIT_URL
22String git_ref = env.GERRIT_REFSPEC ?: env.DEFAULT_GIT_REF
azvyagintsev3d301402019-01-08 13:23:59 +020023String git_credentials_id = env.CREDENTIALS_ID
24
25String docker_registry = env.DOCKER_REGISTRY ?: 'docker-dev-virtual.docker.mirantis.net'
26String docker_image_name = env.IMAGE_NAME ?: 'mirantis/openstack-ci/jenkins-job-tests:latest'
27
28String slave_label = env.SLAVE_LABEL ?: 'docker'
29
30String docker_image = (docker_registry ? "${docker_registry}/" : '') + "${docker_image_name}"
31
32String gradle_report_dir = 'build/reports/codenarc'
33String gradle_report_path = gradle_report_dir + '/main.*'
34String gradle_log_path = gradle_report_dir + '/main.log'
35
36String jjb_dir = 'jenkins-jobs'
37
38// Make codenarc happy
39String scm_class = 'GitSCM'
40
41String reporting_config = '''
42codenarcMain {
43 reports {
44 text.enabled = true
45 html.enabled = true
46 }
Jakub Josef83379312017-03-29 18:12:34 +020047}
azvyagintsev3d301402019-01-08 13:23:59 +020048'''
49
50// Set current build description
51if (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}
69timeout(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 Josefd75c1d52017-03-15 18:06:03 +0100109 }
azvyagintsev3d301402019-01-08 13:23:59 +0200110
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 Josefa63f9862018-01-11 17:58:38 +0100134 }
azvyagintsev3d301402019-01-08 13:23:59 +0200135 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100136
azvyagintsev3d301402019-01-08 13:23:59 +0200137 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 Josef27424bc2017-05-22 16:56:27 +0200173}