blob: c4ae04023ff552a913d8d231e8466b880e9bd88a [file] [log] [blame]
#!groovy
def main() {
String gitUrl = "${env.GERRIT_SCHEME}://${env.GERRIT_HOST}:${env.GERRIT_PORT}/${env.GERRIT_PROJECT}"
String gitRef = env.GERRIT_REFSPEC
stage('SCM checkout') {
echo "Checking out git repository from ${gitUrl} @ ${gitRef}"
checkout \
$class: 'GitSCM',
branches: [[
name: 'FETCH_HEAD'
]],
userRemoteConfigs: [[
url: gitUrl,
refspec: gitRef,
credentialsId: env.GIT_CREDENTIALS_ID
]],
extensions: [[
$class: 'WipeWorkspace'
]]
}
stage('Codenarc') {
String corenarcRulesFile = 'codenarcRules.groovy'
if (!fileExists(corenarcRulesFile)) {
writeFile \
file: corenarcRulesFile,
text: env.DEFAULT_RULES
}
sh '''#!/bin/bash -ex
codenarc \
-maxPriority1Violations=0 \
-maxPriority2Violations=0 \
-maxPriority3Violations=0 \
-excludes='**/codenarcRules.groovy' \
-rulesetfiles=file:codenarcRules.groovy \
-report=console \
-report=html:report.html \
| tee report.log
if [ "${PIPESTATUS[0]}" != '0' ]; then
exit 1
fi
if grep -q 'Compilation failed' report.log ; then
exit 1
fi
if grep -q 'Error processing' report.log ; then
exit 1
fi
'''
}
stage('Report') {
archiveArtifacts \
artifacts: 'report.html',
allowEmptyArchive: true
}
}
String podTpl = """
apiVersion: "v1"
kind: "Pod"
spec:
securityContext:
runAsUser: 1000
containers:
- name: "codenarc"
image: "${env.DOCKER_IMAGE}"
command:
- "cat"
securityContext:
privileged: false
tty: true
"""
if (env.K8S_CLUSTER == 'unset') {
node(env.NODE_LABEL) {
docker.image(env.DOCKER_IMAGE).inside('--entrypoint=""') {
main()
}
}
} else {
podTemplate(
cloud: env.K8S_CLUSTER,
yaml: podTpl,
showRawYaml: false
) {
node(POD_LABEL) {
container('codenarc') {
main()
}
}
}
}