blob: 30f1c7b6a2e88729de076b17518abf1a3aa1f5ef [file] [log] [blame]
/**
* 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,
])
}
}
}