Skip to content

Instantly share code, notes, and snippets.

@daipresents
Last active July 13, 2019 12:42
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save daipresents/c7f982ffce9c1bda73b4387d6b1ae3da to your computer and use it in GitHub Desktop.
Save daipresents/c7f982ffce9c1bda73b4387d6b1ae3da to your computer and use it in GitHub Desktop.
Sample of Jenkinsfile for retry block.
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
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}")
}
}
}
@jhsea3do
Copy link

jhsea3do commented Jul 26, 2018

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment