Idan Adar
Idan Adar

Reputation: 44516

How to properly use variables in for loops in a Jenkinsfile

I have the following Jenkinsfile script block. When the job is executed the end-user ticks several checkboxes (Extended Choice Parameters) and the selected values go to ReposToUpdate and npmDependencies.

Then, in the Jenkinsfile is executed, I get the following error:

java.lang.NullPointerException: Cannot get property '$repoName' on null object

The majority of the Jenkinsfile can be disregarded (it pertains to the goal, not the problem mentioned above).

def repoList = ReposToUpdate.tokenize(",");
def moduleList = npmDependencies.tokenize(",");

pipeline {
   agent {
      label '****' 
   }

   stages {
      stage ("Update package.json") {
         steps {
            script {
               for (String repoName : repoList) {
                  sshagent (credentials: ['****']) {
                     sh '''
                        git clone -b master git@****.com:****/${repoName}.git
                        cd ${repoName}
                        stat -t . > folderStat1.txt
                     '''

                     for (String moduleName : moduleList) {
                        sh '''
                           cd ${repoName}
                           ncu -u -f "${moduleName}"
                           stat -t . > folderStat2.txt
                        '''
                     }

                     def folderStat1 = readFile('folderStat1.txt').trim()
                     def folderStat2 = readFile('folderStat2.txt').trim()

                     if (folderStat1 == folderStat2) {
                        slackSend (
                           color: '#199515',
                           message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies match the latest package versions."
                        )
                     }
                     else {
                        sh '''
                           cd ${repoName}

                           git config --global user.name "****"
                           git config --global user.email ****
                           git commit -am 'Bump common packages version number [ci skip]'
                           git push origin master

                           cd ..
                           rm -rf ${repoName}
                        '''

                        slackSend (
                           color: '#199515',
                           message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies successfully updated to the latest package versions."
                        )
                     }
                  }
               }
            }
         }
      }
   }

   post {
      failure {
         slackSend (
            color: '#F01717',
            message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER>, Update failed. Review the build logs."
         )
      }
   }
}

Upvotes: 2

Views: 4919

Answers (1)

Oleksandr Slobodian
Oleksandr Slobodian

Reputation: 101

Variable is not accessible in single quotation

sh """ ${variable} """  vs sh ''' ${variable} '''

http://mrhaki.blogspot.com/2009/08/groovy-goodness-string-strings-strings.html

https://blog.art-of-coding.eu/single-or-double-quotation-marks-in-groovy/

Upvotes: 2

Related Questions