Last active
May 20, 2024 09:05
-
-
Save daipresents/c7f982ffce9c1bda73b4387d6b1ae3da to your computer and use it in GitHub Desktop.
Sample of Jenkinsfile for retry block.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Started by user daipresents | |
[Pipeline] node | |
Running on test-instance-1 in /var/jenkins/workspace/retry-sample | |
[Pipeline] { | |
[Pipeline] stage | |
[Pipeline] { (job 1) | |
[Pipeline] echo | |
job 1 | |
[Pipeline] echo | |
default: currentBuild.result: null | |
[Pipeline] retry | |
[Pipeline] { | |
[Pipeline] echo | |
execute job 1 | |
[Pipeline] } | |
ERROR: Execution failed | |
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.lang.Exception | |
at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectNew(StaticWhitelist.java:187) | |
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onNewInstance(SandboxInterceptor.java:130) | |
at org.kohsuke.groovy.sandbox.impl.Checker$3.call(Checker.java:191) | |
at org.kohsuke.groovy.sandbox.impl.Checker.checkedConstructor(Checker.java:188) | |
・・・ | |
Retrying | |
[Pipeline] { | |
[Pipeline] echo | |
execute job 1 | |
[Pipeline] } | |
ERROR: Execution failed | |
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.lang.Exception | |
at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectNew(StaticWhitelist.java:187) | |
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onNewInstance(SandboxInterceptor.java:130) | |
at org.kohsuke.groovy.sandbox.impl.Checker$3.call(Checker.java:191) | |
at org.kohsuke.groovy.sandbox.impl.Checker.checkedConstructor(Checker.java:188) | |
・・・ | |
Retrying | |
[Pipeline] { | |
[Pipeline] echo | |
execute job 1 | |
[Pipeline] } | |
[Pipeline] // retry | |
[Pipeline] echo | |
catch exeption. currentBuild.result: FAILURE | |
[Pipeline] echo | |
result: currentBuild.result: FAILURE | |
[Pipeline] } | |
[Pipeline] // stage | |
[Pipeline] stage | |
[Pipeline] { (job 2) | |
[Pipeline] echo | |
job 2 | |
[Pipeline] echo | |
default: currentBuild.result: FAILURE | |
[Pipeline] retry | |
[Pipeline] { | |
[Pipeline] echo | |
execute job 2 | |
[Pipeline] echo | |
Job was successful. currentBuild.result: FAILURE | |
[Pipeline] } | |
[Pipeline] // retry | |
[Pipeline] echo | |
result: currentBuild.result: FAILURE | |
[Pipeline] } | |
[Pipeline] // stage | |
[Pipeline] } | |
[Pipeline] // node | |
[Pipeline] End of Pipeline | |
Finished: FAILURE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node{ | |
def job_list = ['job 1', 'job 2'] | |
for (job in job_list) { | |
stage(job){ | |
println ("${job}") | |
println("default: currentBuild.result: ${currentBuild.result}") | |
try{ | |
retry(3) { | |
println("execute ${job}") | |
if (job == 'job 1') { | |
throw new Exception() | |
} | |
currentBuild.result = "SUCCESS" | |
println("Job was successful. currentBuild.result: ${currentBuild.result}") | |
} | |
} catch (e) { | |
currentBuild.result = "FAILURE" | |
println("catch exeption. currentBuild.result: ${currentBuild.result}") | |
} | |
println("result: currentBuild.result: ${currentBuild.result}") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
please use error keyword to define a custom exception, see how-to-throw-exception-in-jenkins-pipeline
otherwise we will get RejectedAccessException:
"org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.lang.Exception"