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