为Groovy提供列表以循环运行任务多次

问题描述 投票:-1回答:1

我有在Groovy中定义的列表,如:

all_services:[processor-create, processor-update, read-service]

我怎样才能将这个列表提供给for循环播放变量$ {service}

script {

    sh("""

    ansible-playbook -i localhost, cleanup.yml --extra-vars=@${service}.yml
    """)
    ...

因此,如果列表有2个项目,则应该运行该剧本,然后每个项目必须运行2次。

让我说清楚。

我有groovy脚本它有多个阶段:

stages {
    stage('Prepare') {
        agent any
        steps {
            script {
                if (params.DEPLOY_ALL_SERVICES == true){
                   all_services = new ArrayList(Arrays.asList("${params.ALL_SERVICES}".split("\\+")))
                   println "all_services:" + all_services
                } else{
                   if (params.DEPLOY_ALL_EX_SERVICES == true){
                       all_ex_services = new ArrayList(Arrays.asList("${params.ALL_EX_SERVICES}".split("\\+")))
                       println "deploy all ex services:" + all_account_services
                       all_services += all_ex_services
                   }
                   println "All Services:" + all_services
                }
            }
        }
    }
    stage('Create conf'){
        agent any
        steps {
            script {
                def services = "$all_services"
                println services    // it works till here, it's printing the list, if add single quotes to list then hopefully it should loop through it
                services.each {service ->
                    sh("""
                      ansible-playbook -i localhost, cleanup.yml --extra-vars=@${service}.yml
                     """)
                 }
            }
        }
    }
}

当我执行管道时,它能够在控制台上打印列表,但是无法将该列表提供给def services =“$ all_services”,它没有得到所有列表来执行循环。

控制台输出println服务:

       [processor-create, processor-update, ex-service]


an exception which occurred:
in field com.cloudbees.groovy.cps.impl.FunctionCallEnv.locals
in object com.cloudbees.groovy.cps.impl.FunctionCallEnv@1a3dd25b
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@7f249352
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.LoopBlockScopeEnv@59936027
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@6a3531fb
in field com.cloudbees.groovy.cps.impl.CallEnv.caller
groovy jenkins-groovy
1个回答
0
投票

你的问题确实不够明确,无法确切知道你在寻找什么,但是下面的groovy代码:


def services = ['processor-create', 'processor-update', 'read-service']

services.each { service -> 
  sh("ansible-playbook -i localhost, cleanup.yml --extra-vars=@${service}.yml")
}

def sh(str) {
  println "fake execution>> \n${str}\n"
}

演示iteration of a collectionstring interpolation。将上述内容保存在solution.groovy中并执行它会产生:

~> groovy solution.groovy
fake execution>>
ansible-playbook -i localhost, cleanup.yml [email protected]

fake execution>>
ansible-playbook -i localhost, cleanup.yml [email protected]

fake execution>>
ansible-playbook -i localhost, cleanup.yml [email protected]

应该注意的是,由于上面的代码使用的是纯粹的groovy并且在groovy中没有开箱即用的scriptsh,我嘲笑了sh方法只是在stdout上打印结果。

© www.soinside.com 2019 - 2024. All rights reserved.