How to retry a Jenkins Pipeline stage with an agent condition

in out company we execute parallel test executions when each test gets it’s own node… The nodes are an aws spot type agents, which by policy can be taken by aws and get terminated while in progress. We want to re execute the stages which nodes were terminated by aws and we came across the Pipeline retry step retry-the-body-up-to-n-times step with condition, which can be set to retry a pipeline stage in case agent was interrupted. To use it we need to pass array of Nested Choice of Objects - in my case single object of “agent” i tried to execute the next code with different syntax attempts:

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
                }
            }
        }
    }
}

or

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

i also found this stuck overflow thread Retry with condition jenkins pipeline and use the code :

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

or

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

which by the jenkins code, expects ErrorCondition implementing classes arr/list as a value to the conditions key but i get compilation exceptions

Can anyone please suggest me the right syntax or point to a working example?

jenkins

Working example in the buildPlugin library that is used to build Jenkins plugins on ci.jenkins.io:

Line 51 of the buildPlugin implementation

Thanks for your reply…
i executed →

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: [kubernetesAgent(handleNonKubernetes: true)]){
                            node("aws-fleet-spot-small") {
                                stage("stage1") {
                                    script {
                                        echo ('stage11111')
                                        sh 'sleep 1'
                                        echo 'after sleep 1'
                                        error 'error 1'
                                    }
                                }
                            }
                        }
                    }
                    stepsForParallel["stage2"] = {
                        node("aws-fleet-spot-small") {
                            stage("stage2") {
                                script {
                                    echo ('stage2222')
                                    sh 'sleep 3'
                                    echo 'after sleep 2'
                                    error 'error 22'
                                }
                            }
                        }
                    }
                    
                        parallel stepsForParallel
                }
            }
        }
    }
}

but it got en exception →

Also: hudson.AbortException: error 22 12:06:01 at org.jenkinsci.plugins.workflow.steps.ErrorStep$Execution.run(ErrorStep.java:63) 12:06:01 at org.jenkinsci.plugins.workflow.steps.ErrorStep$Execution.run(ErrorStep.java:50) 12:06:01 at org.jenkinsci.plugins.workflow.steps.SynchronousStepExecution.start(SynchronousStepExecution.java:37) 12:06:01 at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:319) 12:06:01 at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:193) 12:06:01 at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122) 12:06:01 at sun.reflect.GeneratedMethodAccessor1245.invoke(Unknown Source) 12:06:01 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 12:06:01 at java.lang.reflect.Method.invoke(Method.java:498) 12:06:01 at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) 12:06:01 at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) 12:06:01 at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213) 12:06:01 at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022) 12:06:01 at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42) 12:06:01 at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) 12:06:01 at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) 12:06:01 at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:163) 12:06:01 at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23) 12:06:01 at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:158) 12:06:01 at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:161) 12:06:01 at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:165) 12:06:01 at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135) 12:06:01 at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135) 12:06:01 at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135) 12:06:01 at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135) 12:06:01 at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135) 12:06:01 at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135) 12:06:01 at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17) 12:06:01 at WorkflowScript.run(WorkflowScript:32) 12:06:01 at cps.transform(Native Method) 12:06:01 at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86) 12:06:01 at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113) 12:06:01 at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83) 12:06:01 at sun.reflect.GeneratedMethodAccessor897.invoke(Unknown Source) 12:06:01 java.lang.NoSuchMethodError: No such DSL method ‘kubernetesAgent’ found among steps

Do i need to use a specific plugin for it ?
I tried to look for kubernetesAgent object doc but couldn’t find it , Can u please point me to “agent” which is another option conditions can receive?