为什么 Jenkins 运行 是沙箱内的共享管道库?
Why is Jenkins running a shared pipeline library inside the sandbox?
我创建了一个 git 存储库,其中包含位于 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 回购添加到管理 Jenkins -> 配置系统中的 "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: Scripts not permitted to use method groovy.lang.GroovyObject getProperty java.lang.String (com.me.JobTriggerInfo.wasStartedByTimer)
我的理解是,全球管道库将 运行 在沙箱之外,基于 official Jenkins docs。
我错过了什么?我需要做什么才能将此代码从全球管道库而不是沙箱中获取到 运行?
根本原因是我的脚本中缺少括号。脚本节中的行必须是:
startedByTimer = new com.me.JobTriggerInfo(this).wasStartedByTimer()
令人困惑的是报告的错误消息没有直接表明这是问题所在。
我创建了一个 git 存储库,其中包含位于 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 回购添加到管理 Jenkins -> 配置系统中的 "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: Scripts not permitted to use method groovy.lang.GroovyObject getProperty java.lang.String (com.me.JobTriggerInfo.wasStartedByTimer)
我的理解是,全球管道库将 运行 在沙箱之外,基于 official Jenkins docs。
我错过了什么?我需要做什么才能将此代码从全球管道库而不是沙箱中获取到 运行?
根本原因是我的脚本中缺少括号。脚本节中的行必须是:
startedByTimer = new com.me.JobTriggerInfo(this).wasStartedByTimer()
令人困惑的是报告的错误消息没有直接表明这是问题所在。