Issue
Hi there I run into problem that if I want to run retry with condition job keeps failing. Heres with added condition phrase:
WorkflowScript: 56: Invalid parameter "conditions", did you mean "count"? @ line 56, column 27.
retry(count: 120, conditions: !Jenkins.instance.getItem('secret-name-of-job').getLastBuild().getExecution().isComplete()) {
^
also while I was trying to do not add conditions explicitly another error appear
WorkflowScript: 56: Arguments to "retry" must be explicitly named. @ line 56, column 9.
retry(count: 120, !Jenkins.instance.getItem('secret-name-of-job').getLastBuild().getExecution().isComplete()) {
Any idea what I'm doing wrong ? Code from jenkinsfile:
steps {
retry(count: 120, conditions: !Jenkins.instance.getItem('secret-name-of-job').getLastBuild().getExecution().isComplete()) {
echo "Sleeping for 1 minute since e2e build is running..."
sleep(time: 1, unit: 'MINUTES')
} more code }
EDIT With help of @ycr and few tries I was able to achieve waiting until other job is finished
Code:
script {
retry(count: 120) {
if (Jenkins.instance.getItem('magic-secret-job').getLastBuild().getExecution().isComplete() == false) {
echo "Waiting since e2e build is running..."
sleep(time: 1, unit: "MINUTES")
throw new Error('e2e is still running')
}
} some_other_code }
Conslusions:
- retry needs to be under script if you want to use if/loops
- If you create condition first time, it mayfail because you need to approve methods in "In-process Script Approval" section
- You need to throw exception to trigger retry once more time
- Still not sure how
conditions
works, as @ycr mentioned it has to be some sort of extension ofErrorCondition
Solution
It seems conditions
accepts a list of objects that implements ErrorCondition. Check here.
private transient final @CheckForNull List<ErrorCondition> conditions;
As far as I observed there are few pre implemented ErrorConditions you can use. (SynchronousResumeNotSupportedErrorCondition
, AgentErrorCondition
, KubernetesAgentErrorCondition
etc.), So you have to use one of these or you may have to implement a custom condition yourself and add it to the runtime extending ErrorCondition
.
Having said that, here is how you can get what you need without conditions. Just add a try-catch
around your execution and handle the error in catch
block. Check the following.
retry(count: 3) {
try {
echo "Sleeping for 1 minute since e2e build is running..."
sleep(time: 5, unit: 'SECONDS')
} catch(e) {
if(Jenkins.instance.getItem('test').getLastBuild().getExecution().isComplete()) {
// Let's error out the the condition is true which will trigger a retry
throw e
}
echo "Condition failed, means no retry, so complete the Job"
}
}
Answered By - ycr
Answer Checked By - Mary Flores (JavaFixing Volunteer)