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

时间:2019-04-24 18:47:45

标签: groovy jenkins-groovy

我在Groovy中定义了列表,例如:

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

我如何提供此列表以for循环播放另一个舞台剧本变量$ {service}

script {

    sh("""

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

因此,如果列表包含2个项目,则该Playbook应该运行,然后每个项目必须运行2次。

让我说清楚点吧。

我有一个普通的脚本,它具有多个阶段:

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

1 个答案:

答案 0 :(得分:0)

您的问题定义得不够准确,无法确定您要查找的内容,但是下面的常规代码:


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 --extra-vars=@processor-create.yml

fake execution>>
ansible-playbook -i localhost, cleanup.yml --extra-vars=@processor-update.yml

fake execution>>
ansible-playbook -i localhost, cleanup.yml --extra-vars=@read-service.yml

应该注意的是,由于上面的代码使用的是纯groovy,并且在groovy中没有开箱即用的scriptsh,所以我将sh方法模拟为在标准输出上打印结果。