我创建了一个 git repo,其中包含以下文件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
}
}
然后,我将该 git repo 添加到 Manage Jenkins -> Configure System 中的“Global Pipeline Libraries”部分。
然后我使用以下管道脚本创建了一个简单的管道项目:
@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()
}
}
当我运行作业时,它失败了:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:脚本不允许使用方法 groovy.lang.GroovyObject getProperty java.lang.String (com.me.JobTriggerInfo.wasStartedByTimer)
我的理解是,基于Jenkins 官方文档,全球管道库将在沙箱之外运行。
我错过了什么?我需要做什么才能让此代码从全局管道库而不是沙箱中运行?