I've created a git repo with the following file located at src/com/me
:
package com.me
import com.cloudbees.groovy.cps.NonCPS
class JobTriggerInfo implements Serializable {
def script
JobTriggerInfo(script)
{
this.script = script
}
// Source originally from:
// https://hopstorawpointers.blogspot.com/2016/10/performing-nightly-build-steps-with.html
@NonCPS
wasStartedByTimer() {
def startedByTimer = false
try {
def buildCauses = script.currentBuild.rawBuild.getCauses()
for ( buildCause in buildCauses ) {
if (buildCause != null) {
def causeDescription = buildCause.getShortDescription()
script.echo "shortDescription: ${causeDescription}"
if (causeDescription.contains("Started by timer")) {
startedByTimer = true
}
}
}
} catch(theError) {
script.echo "Error getting build cause"
}
return startedByTimer
}
}
I then added that git repo to the "Global Pipeline Libraries" section in Manage Jenkins -> Configure System.
Then I created a simple pipeline project with the following pipeline script:
@Library('JSL')
import com.me.JobTriggerInfo
node {
stage('Preparation') {
echo 'Hello World'
startedByTimer = false
script {
startedByTimer = new com.me.JobTriggerInfo(this).wasStartedByTimer
}
echo 'Was started by timer?'
echo startedByTimer.toString()
}
}
When I run the job, it fails with:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method groovy.lang.GroovyObject getProperty java.lang.String (com.me.JobTriggerInfo.wasStartedByTimer)
My understanding is that a Global Pipeline Library will run outside the sandbox, based on the official Jenkins docs.
What am I missing? What do I need to do to get this code to run from a Global Pipeline Library and not in a sandbox?