Jenkins Pipeline string and for-loop

13,309

At which point you want to split this into particular texts? In general this part is missing .split(' ').

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

If you really want to do that in your shell directly add escaped quotes and use a variable

sh "test=\"${text}\";for value in $test; do echo $value; done"
Share:
13,309

Related videos on Youtube

hem1234
Author by

hem1234

Updated on June 04, 2022

Comments

  • hem1234
    hem1234 almost 2 years

    I'm creating a jenkins pipeline which has a string as a variable of 1 or more items

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

    I basically want to go in a loop and for each item run an action. for example echo each one in turn. The echo would look at the string and return each item in a for loop where there are 1 or more results

    expected result:

    test1.var1.eu-20190414121923517200000001

    test2.var2.ue1-20190414121925623400000002

    test3.var3.ue1-20190414121926583500000003

    I've tried a few things including adding a sh to run a for loop

    #!/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"
            }
          }
        }
      }
    }
    

    expected result

    test1.var1.eu-20190414121923517200000001

    test2.var2.ue1-20190414121925623400000002

    test3.var3.ue1-20190414121926583500000003

    actual result:

    [Pipeline] End of Pipeline [Office365connector] No webhooks to notify groovy.lang.MissingPropertyException: No such property: value for class: > groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:264) at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:288) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:292) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)

    • hakamairi
      hakamairi almost 5 years
      What's the result you get now?
    • hem1234
      hem1234 almost 5 years
      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:
    • hem1234
      hem1234 almost 5 years
      I have also tried below with the same error:
    • hem1234
      hem1234 almost 5 years
      ` script { env.text="test1.var1.eu-20190414121923517200000001 test2.var2.ue1-20190414121925623400000002 test3.var3.ue1-20190414121926583500000003" sh "echo ${text}" sh(script: "for value in ${text}; do echo $value; done", returnStdout: true).trim() } `
  • hem1234
    hem1234 almost 5 years
    the next thing i'm trying to do is generate 'text' from a call as below: ``` env.ASG_GROUP_NAME = sh (script: """ aws autoscaling describe-auto-scaling-groups --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 --output text """, returnStdout: true).trim() ```
  • hem1234
    hem1234 almost 5 years
    with the for loop, that returns the whole value and not split
  • hakamairi
    hakamairi almost 5 years
    if your original question was answered, please accept the answer and start a new question.
  • hem1234
    hem1234 almost 5 years