如何在Jenkinsfile

时间:2017-08-08 06:34:21

标签: loops jenkins groovy jenkins-pipeline

我有以下Jenkinsfile脚本块。执行作业时,最终用户勾选几个复选框(扩展选择参数),所选值将转到ReposToUpdatenpmDependencies

然后,在执行Jenkinsfile时,我收到以下错误:

  

java.lang.NullPointerException:无法在null对象上获取属性“$ repoName”

Jenkinsfile的大部分都可以被忽略(它与目标有关,而不是上面提到的问题)。

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."
         )
      }
   }
}

1 个答案:

答案 0 :(得分:1)