在詹金斯管道封闭体内传递变量

时间:2021-01-08 14:11:09

标签: jenkins groovy jenkins-pipeline

我创建了一个管道函数来重试阶段 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 方法重启”

1 个答案:

答案 0 :(得分: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}"
                }
            }
        }
    }
}