jenkins pipeline 将字符串转换成数组

2020-07-10  本文已影响0人  _fishman

这种方式会吧dev,test,uat,prd所有的参数都被当作字符串循环处理了

pipeline {
   agent any

    parameters {
      extendedChoice description: '', 
      multiSelectDelimiter: ',', 
      name: 'profile', 
      quoteValue: false, 
      saveJSONParameterToFile: false, 
      type: 'PT_CHECKBOX', 
      value: 'dev,test,uat,prd', 
      visibleItemCount: 5
  }


   stages {
      stage('test') {
         steps {
            script {
                println profile
                for (i in profile){
                    println "${i}"
                    
                }
            }
         }
      }
   }
image.png

这个时候就需要把字符串转换成数组形式,groovy中使用split()方法分割字符串并返回数组形式

pipeline {
   agent any

    parameters {
      extendedChoice description: '', 
      multiSelectDelimiter: ',', 
      name: 'profile', 
      quoteValue: false, 
      saveJSONParameterToFile: false, 
      type: 'PT_CHECKBOX', 
      value: 'dev,test,uat,prd', 
      visibleItemCount: 5
    }


   stages {
      stage('test') {
         steps {
            script {
                println profile
                def list =profile.split(',')
                for (i in list){
                    println "${i}"
                    
                }
            }
         }
      }
   }
}

参考文档

上一篇 下一篇

猜你喜欢

热点阅读