【问题标题】:Passing variables inside jenkins pipeline closure body在詹金斯管道封闭体内传递变量
【发布时间】:2021-01-08 22:52:45
【问题描述】:

我创建了一个管道函数来重试阶段 3 次:

def restart(body){
    retry(3){
        body()
    }
}

我这样称呼它:

def prepareStage(accountName, slave_tag) {
    return {
        restart(){
            node(slave_tag){
                stage("${target} ${accountName}") {
                    build job: "pipelines/${accountName}/${env.BRANCH_NAME}"
                }
            }
        }
    }
}

它工作正常,但现在我想传递另一个变量“重试”来决定它是否应该重试 3 次,如下所示:

def restart(body, retries){
    if (retries == false){
        body()
    }
    else {
        retry(3){
            body()
        }
    }
}

def prepareStage(accountName, slave_tag, retries) {
    return {
        restart(retries){
            node(slave_tag){
                stage("${target} ${accountName}") {
                    build job: "pipelines/${accountName}/${env.BRANCH_NAME}"
                }
            }
        }
    }
}

但我不断收到“没有这样的 DSL 方法重启”

【问题讨论】:

  • 我无法重现您收到的错误消息,但我注意到另一个错误。您需要将输入参数的顺序更改为restartretries 应该是第一个,body 应该是第二个,这取决于你调用函数的方式。
  • 解决了,谢谢@Vasiliki Siakka。我只需要反转到这个“def restart(retries, body)”

标签: jenkins groovy jenkins-pipeline


【解决方案1】:

感谢@Vasiliki Siakka 评论解决了:

def restart(retries, body){
    if (retries == false){
        body()
    }
    else {
        retry(3){
            body()
        }
    }
}

def prepareStage(accountName, slave_tag, retries) {
    return {
        restart(retries){
            node(slave_tag){
                stage("${target} ${accountName}") {
                    build job: "pipelines/${accountName}/${env.BRANCH_NAME}"
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2019-01-09
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多