hem*_*234 3 shell split for-loop jenkins jenkins-pipeline
我正在创建一个詹金斯管道,其中有一个字符串作为 1 个或多个项目的变量
文本=“test1.var1.eu-20190414121923517200000001 test2.var2.ue1-20190414121925623400000002 test3.var3.ue1-20190414121926583500000003”
我基本上想循环并为每个项目运行一个操作。例如依次回显每一个。echo 将查看字符串并返回 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"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
预期结果
test1.var1.eu-20190414121923517200000001
test2.var2.ue1-20190414121925623400000002
test3.var3.ue1-20190414121926583500000003
实际结果:
[管道] 管道结束 [Office365connector] 没有 webhooks 通知 groovy.lang.MissingPropertyException:没有此类属性:类的值:> groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63) at 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}"
}
Run Code Online (Sandbox Code Playgroud)
如果您确实想在 shell 中执行此操作,请直接添加转义引号并使用变量
sh "test=\"${text}\";for value in $test; do echo $value; done"
Run Code Online (Sandbox Code Playgroud)