Jenkins管道字符串和for循环

时间:2019-05-16 14:25:41

标签: shell for-loop jenkins split jenkins-pipeline

我正在创建一个jenkins管道,该管道具有一个字符串作为变量,包含1个或多个项目

  

text =“ test1.var1.eu-20190414121923517200000001   test2.var2.ue1-20190414121925623400000002   test3.var3.ue1-20190414121926583500000003“

我基本上想循环进入,为每个项目运行一个动作。例如,依次回显每个人。回声将查看字符串并在for循环中返回每个项目,其中有1个或多个结果

预期结果:

  

test1.var1.eu-20190414121923517200000001

     

test2.var2.ue1-20190414121925623400000002

     

test3.var3.ue1-20190414121926583500000003

我尝试了一些事情,包括添加sh来运行for循环

#!/usr/local/bin/groovy

pipeline {
  parameters {
    choice(choices: "1\n2\n3", description: 'The length of time for the environment to remain up', name: 'hours')
  }

  stages {
    stage('get and update hours') {
      steps {
        script {
          env.text="test1.var1.eu-20190414121923517200000001 test2.var2.ue1-20190414121925623400000002 test3.var3.ue1-20190414121926583500000003"
          sh "echo ${text}"
          sh "for value in ${text}; do echo $value; done"
        }
      }
    }
  }
}

预期结果

  

test1.var1.eu-20190414121923517200000001

     

test2.var2.ue1-20190414121925623400000002

     

test3.var3.ue1-20190414121926583500000003

实际结果:

  

[管道]管道末端   [Office365connector]没有Webhook通知   groovy.lang.MissingPropertyException:无此类属性:类的值:> groovy.lang.Binding     在groovy.lang.Binding.getVariable(Binding.java:63)     在org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:264)     在org.kohsuke.groovy.sandbox.impl.Checker $ 6.call(Checker.java:288)     在org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:292)       在org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)

1 个答案:

答案 0 :(得分:0)

您想在何时将其拆分为特定文本?通常,这部分缺少.split(' ')

def texts = text.split(' ')
for (txt in texts) {
    sh "echo ${txt}"
}

如果您真的想在shell中这样做,请直接添加转义的引号并使用变量

sh "test=\"${text}\";for value in $test; do echo $value; done"