我正在创建一个jenkins管道,其中string是一个或多个项目的变量

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

我基本上想要循环中的go并为每个项目运行一个动作。例如,依次回应每一个。 echo将查看string并返回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]没有要通知的webhooks   groovy.lang.MissingPropertyException:没有这样的属性:class的值:> groovy.lang.Binding     at 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)

分析解答

那时你想把它分成特定的文本?一般来说,这部分缺少.split(' ')

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

如果你真的想在你的shell中直接添加转义引号并使用变量

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