【问题标题】:Jenkins Pipeline string and for-loopJenkins Pipeline 字符串和 for 循环
【发布时间】:2019-10-03 20:50:14
【问题描述】:

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

text="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"
        }
      }
    }
  }
}

预期结果

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)

【问题讨论】:

  • 你现在得到了什么结果?
  • 15:56:29 [test] Running shell script 15:56:29 + echo test1.var1.eu-20190414121923517200000001 test2.var2.ue1-20190414121925623400000002 test3.var3.ue1-20190414121926583500000003 15:56:29 test1.var1.eu-20190414121923517200000001 test2.var2.ue1-20190414121925623400000002 test3.var3.ue1-20190414121926583500000003 [Pipeline] } [Pipeline] // timestamps [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline [Office365connector] No webhooks to notify groovy.lang.MissingPropertyException: No such property: value for class:
  • 我也在下面尝试过同样的错误:
  • ` script { env.text="test1.var1.eu-20190414121923517200000001 test2.var2.ue1-20190414121925623400000002 test3.var3.ue1-20190414121926583500000003 "为 ${text} 中的值;做 echo $value;完成",returnStdout: true).trim() } `

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


【解决方案1】:

您想在什么时候将其拆分为特定的文本?一般来说,这部分缺少.split(' ')

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

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

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

【讨论】:

  • 接下来我要做的是从调用中生成“文本”,如下所示:``` env.ASG_GROUP_NAME = sh (script: """ aws autoscaling describe-auto-scaling-组 --region ${AWS_REGION} --query "AutoScalingGroups[?Tags[?Key=='Environment' && Value=='${ENVIRONMENT}']] | [?Tags[?Key=='Service' && Value =='${SERVICE_NAME}']] | [? Tags[?Key=='Env_Type' && Value =='${ENV_TYPE}']] | [? Tags[?Key=='Role' && Value == '${list[PMC_ROLE]}']]".AutoScalingGroupName --输出文本 """, returnStdout: true).trim() ```
  • 使用 for 循环,返回整个值而不是拆分
  • 如果您的原始问题已得到解答,请接受答案并开始新问题。
猜你喜欢
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多