0

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:

Vulcano
  • 415
  • 10
  • 25
  • It is possible that the `build` variable is already used by Jenkins and available to you. Try getting your build with `myBuild = build.getClass()` – feedy Mar 11 '22 at 16:36
  • Also, you should have the job name already provided to you by jenkins, just do `println( "${env.JOB_NAME}")` – feedy Mar 11 '22 at 16:43

0 Answers0