Sergey Otpuschennikov | aad1ae0 | 2020-09-11 19:33:51 +0400 | [diff] [blame^] | 1 | #!groovy |
| 2 | |
| 3 | def main() { |
| 4 | String gitUrl = "${env.GERRIT_SCHEME}://${env.GERRIT_HOST}:${env.GERRIT_PORT}/${env.GERRIT_PROJECT}" |
| 5 | String gitRef = env.GERRIT_REFSPEC |
| 6 | |
| 7 | stage('SCM checkout') { |
| 8 | echo "Checking out git repository from ${gitUrl} @ ${gitRef}" |
| 9 | |
| 10 | checkout \ |
| 11 | $class: 'GitSCM', |
| 12 | branches: [[ |
| 13 | name: 'FETCH_HEAD' |
| 14 | ]], |
| 15 | userRemoteConfigs: [[ |
| 16 | url: gitUrl, |
| 17 | refspec: gitRef, |
| 18 | credentialsId: env.GIT_CREDENTIALS_ID |
| 19 | ]], |
| 20 | extensions: [[ |
| 21 | $class: 'WipeWorkspace' |
| 22 | ]] |
| 23 | } |
| 24 | |
| 25 | stage('Codenarc') { |
| 26 | String corenarcRulesFile = 'codenarcRules.groovy' |
| 27 | if (!fileExists(corenarcRulesFile)) { |
| 28 | writeFile \ |
| 29 | file: corenarcRulesFile, |
| 30 | text: env.DEFAULT_RULES |
| 31 | } |
| 32 | sh '''#!/bin/bash -ex |
| 33 | codenarc \ |
| 34 | -maxPriority1Violations=0 \ |
| 35 | -maxPriority2Violations=0 \ |
| 36 | -maxPriority3Violations=0 \ |
| 37 | -excludes='**/codenarcRules.groovy' \ |
| 38 | -rulesetfiles=file:codenarcRules.groovy \ |
| 39 | -report=console \ |
| 40 | -report=html:report.html \ |
| 41 | | tee report.log |
| 42 | if [ "${PIPESTATUS[0]}" != '0' ]; then |
| 43 | exit 1 |
| 44 | fi |
| 45 | if grep -q 'Compilation failed' report.log ; then |
| 46 | exit 1 |
| 47 | fi |
| 48 | if grep -q 'Error processing' report.log ; then |
| 49 | exit 1 |
| 50 | fi |
| 51 | ''' |
| 52 | } |
| 53 | |
| 54 | stage('Report') { |
| 55 | archiveArtifacts \ |
| 56 | artifacts: 'report.html', |
| 57 | allowEmptyArchive: true |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | String podTpl = """ |
| 62 | apiVersion: "v1" |
| 63 | kind: "Pod" |
| 64 | spec: |
| 65 | securityContext: |
| 66 | runAsUser: 1000 |
| 67 | containers: |
| 68 | - name: "codenarc" |
| 69 | image: "${env.DOCKER_IMAGE}" |
| 70 | command: |
| 71 | - "cat" |
| 72 | securityContext: |
| 73 | privileged: false |
| 74 | tty: true |
| 75 | """ |
| 76 | if (env.K8S_CLUSTER == 'unset') { |
| 77 | node(env.NODE_LABEL) { |
| 78 | docker.image(env.DOCKER_IMAGE).inside('--entrypoint=""') { |
| 79 | main() |
| 80 | } |
| 81 | } |
| 82 | } else { |
| 83 | podTemplate( |
| 84 | cloud: env.K8S_CLUSTER, |
| 85 | yaml: podTpl, |
| 86 | showRawYaml: false |
| 87 | ) { |
| 88 | node(POD_LABEL) { |
| 89 | container('codenarc') { |
| 90 | main() |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |