Uploaded image for project: 'Red Hat OpenShift Dev Spaces (formerly CodeReady Workspaces) '
  1. Red Hat OpenShift Dev Spaces (formerly CodeReady Workspaces)
  2. CRW-2835

Implement a groovy lint checker for crw_jenkins merge requests

XMLWordPrintable

    • False
    • None
    • False

      Today, we have a single MR check that will:

      • fetch jenkins source code
      • build it
      • download the internet
      • run it
      • download more of the internet
      • run job-configurator in a clean room to see if the MR breaks any job configs

      This is great when you have big breaking changes or your groovy won't compile at all.

      But more often than not, there's a small runtime bug that the job-configurator generation won't catch, which means you can wait 20-40 mins to find out your MR is OK to merge, then 2 mins to run job-configurator, then you fire up the new/updated job ... and it craps out on a groovy syntax typo.

      So... why not add a linter check to the groovy code, which can also be triggered on all MRs, which can quickly check for groovy syntax errors, and fail the MR check in minutes instead of 20-40 mins?

      Update: there in-built Jenkins groovy linter existed: https://www.jenkins.io/doc/book/pipeline/development/#linter

      Solution (requires node 8.x, so doesn't work on "rhel7" nodes):

             stage("Run Pipeline Linter against changed jenkinsfile's"){
                  steps{
                      sh """
                         set +ex
                         cd ${CONFIG_DIR}
                         
                         JENKINSFILES=\$(git diff --name-only origin/${gitlabTargetBranch}...origin/${gitlabSourceBranch} | grep -i ".jenkinsfile\\|.groovy")
                         
                         checkStatus=0
                         for jenkinsfile_path in \$JENKINSFILES ; do 
                            if [ -f "\$jenkinsfile_path" ]; then                         
                                result=0
                                
                                ## files having lines started with 'pipeline {' 
                                if grep -e '^[[:space:]]*pipeline[[:space:]]*{' \$jenkinsfile_path ; then
                                    echo -e "Validating declarative pipeline '\$jenkinsfile_path'..." 
                                    
                                    result=\$(curl -sfX POST -F "jenkinsfile=<\$jenkinsfile_path" https://main-jenkins-csb-crwqe.apps.ocp-c1.prod.psi.redhat.com/pipeline-model-converter/validate)
                                    echo -e "> \$result\\n\\n"
                                    
                                    if [[ \$result != "Jenkinsfile successfully validated." ]]; then
                                       checkStatus=1
                                    fi
                                    
                                else
                                    echo -e "Validating scripted pipeline '\$jenkinsfile_path'..." 
                                    
                                    if ! sudo npm list -g | grep npm-groovy-lint ; then
                                       sudo npm install -g npm-groovy-lint
                                    fi
                                    
                                    npm-groovy-lint --failonerror -l error --parse -f \$jenkinsfile_path
                                    result=\$(echo \$?)
      
                                    if [[ \$result != 0 ]]; then
                                       checkStatus=1
                                    fi                              
                                fi
                            fi
                         done
                         
                         exit \$checkStatus
                      """
                  }
              }
      

      Update from nickboldt: nvm is installed on rhel8 Jenkins node, along with node v12.22.5 and v14.18.2

            dnochevn Dmytro Nochevnov
            nickboldt Nick Boldt
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: