如何在存在代理条件的情况下重试Jenkins Pipeline阶段

gpfsuwkq  于 2022-11-02  发布在  Jenkins
关注(0)|答案(1)|浏览(389)

在我们公司,当每个测试获得其自己的节点时,我们执行并行测试执行......节点是aws点类型代理,根据策略,它可以由aws获取,并在进行时终止。我们希望重新执行节点被aws终止的阶段,我们遇到了管道重试步骤,它可以被设置为在代理被中断的情况下重试管道阶段。要使用它,我们需要传递对象的嵌套选择数组-在我的例子中,“agent”的单个对象,我尝试用不同的语法尝试执行下一个代码:

import hudson.model.*
import jenkins.model.*

pipeline {
    agent { label 'aws-fleet-spot-small' }
    stages {
        stage('test parallel') {
            steps {
                script {
                    def stepsForParallel = [:]
                    stepsForParallel["stage1"] = {
                        retry(count: 3, conditions: [agent{ label 'aws-fleet-spot-small' }]){
                            node("aws-fleet-spot-small") {
                                stage("stage1") {
                                    script {
                                        echo ('stage11111')
                                    }
                                }
                            }
                        }
                    }
                    stepsForParallel["stage2"] = {
                        node("aws-fleet-spot-small") {
                            stage("stage2") {
                                script {
                                    echo ('stage2222')
                                }
                            }
                        }
                    }
                    parallel stepsForParallel
                }
            }
        }
    }
}

retry(count:3, conditions:[agent])

我还发现了这个阻塞溢出线程Retry with condition jenkins pipeline,并使用代码:

import org.jenkinsci.plugins.workflow.support.steps.AgentErrorCondition
retry(count: 3, conditions: [new AgentErrorCondition()){

retry(count:3, conditions: [AgentErrorCondition])

根据jenkins代码,它期望ErrorCondition实现类arr/list作为条件键的值**,但我得到编译异常**
有谁能给我建议正确的语法或指向一个工作的例子吗?

wwwo4jvm

wwwo4jvm1#

在我们公司,我们有完全相同的设置,下面的代码在AWS节点被取走或该节点上发生其他问题时重试该步骤:

def class PipelineHelper implements Serializable {
    def steps

    PipelineHelper(steps) { this.steps = steps }

    void retry(final Closure<?> action, int maxAttempts, final int count = 0) {
        steps.echo "Trying action, attempt count is: ${count}"
        try {
            action.call();
        } catch (final x) {
            steps.echo "Exception: ${x.toString()}"
            def x_causes = [x.getClass()]
            def exc = x
            while (exc.getCause()) {
                exc = exc.getCause()
                x_causes += exc.getClass()
            }
            steps.echo "Causes: ${x_causes.toString()}"

            def allowed_causes = [
                // Pipeline stopped org.jenkinsci.plugins.workflow.steps.FlowInterruptedException,
                org.jenkinsci.plugins.workflow.support.steps.ExecutorStepExecution.RemovedNodeCause,
                org.jenkinsci.plugins.workflow.support.steps.AgentOfflineException,//Unable to create live FilePath
                hudson.remoting.ChannelClosedException,
                java.nio.file.FileSystemException, // no space left on device
                java.io.IOException,
            ]

            if (x_causes.intersect(allowed_causes)) {
                if (count <= maxAttempts) {
                    steps.sleep(10)
                    steps.echo "Retrying from failed stage."
                    return retry(action, maxAttempts, count + 1)
                } else {
                    steps.echo "Max attempts reached. Will not retry."
                    throw x
                }
            } else {
                steps.echo 'Aborting'
                throw x
            }
        }
    }
}

然后以下列方式 Package 程式码:

def pipelineHelper = new pipelineHelper(this)
                    pipelineHelper.retry( {
                        node("aws-fleet-spot-small") {
                            echo ('stage11111')
                        }
                    }, MAX_RETRIES)

相关问题