I have the following script in a .gsh file:
build = Thread.currentThread().executable
String jobName = build.getEnvVars()["JOB_NAME"]
println "JOBNAME: " + jobName
When I execute this script within a normal freestyle project it does work fine in the step Execute system Groovy script
output: JOBNAME: playground/Testing
.
However if I execute the same script within a pipeline:
stage('Test') {
steps {
script {
jobName = load 'C:/Tools/getTag.gsh'
}
}
}
I do get an RejectedAccessException.
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field java.lang.Thread executable
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:426)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:410)
at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:353)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:357)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at Script1.run(Script1.groovy:8)
How can I execute this script within the pipeline?
Notes:
- I can't approve it like Jenkins RejectedAccessException as it is already approved and works within the freestyle project.