代码非常简单,我只想将修订列表发布到松弛状态。但是,导致我出现问题的部分是当我实际上试图从git获取修订列表时。
问题代码
@NonCPS
def getRevisionList(currentCommit, lastSuccessfulCommit) {
def commits = sh(
script: "git rev-list $currentCommit $lastSuccessfulCommit",
returnStdout: true
).split('\n')
echo "$commits"
}
完整代码:
pipeline {
environment {
failureMessage = ""
}
agent {
node {
label 'gsacsp-build02.reisys.com'
}
}
stages {
stage('Parse Commits') {
steps {
script {
def currentBuild = currentBuild.rawBuild
def currentCommit = commitHashForBuild(currentBuild)
def lastSuccessfulCommit = getLastSuccessfulCommit()
getRevisionList(currentCommit, lastSuccessfulCommit)
}
}
}
}
post {
always {
withCredentials([string(credentialsId: 'BOT_SLACK_HOOK', variable: 'BOT_SLACK_HOOK')]) {
script {
failureMessage = readFile "jenkinshelpers/slackfailuremessage.json"
sh "curl -X POST -H 'Content-type: application/json' --data '$failureMessage' $BOT_SLACK_HOOK"
}
}
}
}
}
//Groovy Helper Methods
@NonCPS
def getRevisionList(currentCommit, lastSuccessfulCommit) {
def commits = sh(
script: "git rev-list $currentCommit $lastSuccessfulCommit",
returnStdout: true
).split('\n')
echo "$commits"
}
@NonCPS
def getLastSuccessfulCommit() {
def lastSuccessfulHash = null
def lastSuccessfulBuild = currentBuild.rawBuild.getPreviousSuccessfulBuild()
if ( lastSuccessfulBuild ) {
lastSuccessfulHash = commitHashForBuild( lastSuccessfulBuild )
}
return lastSuccessfulHash
}
@NonCPS
def commitHashForBuild(build) {
def scmAction = build?.actions.find { action -> action instanceof jenkins.scm.api.SCMRevisionAction }
return scmAction?.revision?.hash
}
詹金斯给我的错误是这样的:
an exception which occurred:
in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@76bde0fe
in field com.cloudbees.groovy.cps.impl.CallEnv.caller
in object com.cloudbees.groovy.cps.impl.FunctionCallEnv@662f031a
in field com.cloudbees.groovy.cps.Continuable.e
in object org.jenkinsci.plugins.workflow.cps.SandboxContinuable@1156ea7f
in field org.jenkinsci.plugins.workflow.cps.CpsThread.program
in object org.jenkinsci.plugins.workflow.cps.CpsThread@5bada334
in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.threads
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@42bbb563
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@42bbb563
原因:java.io.NotSerializableException:org.jenkinsci.plugins.workflow.job.WorkflowRun
更多的是,我正在遵循以下内容,但是却出现了错误。我曾尝试以原始方式运行它,但也没有运气。
答案 0 :(得分:4)
您看到的异常是由以下行引起的:
def currentBuild = currentBuild.rawBuild
currentBuild.rawBuild
返回一个不可序列化的对象,因此必须在@NonCPS
方法内部调用以避免产生此异常。尝试简化您的辅助方法,以便它们访问currentBuild
中的@NonCPS
变量:
@NonCPS
def getRevisionList(currentCommit, lastSuccessfulCommit) {
def commits = sh(
script: "git rev-list $currentCommit $lastSuccessfulCommit",
returnStdout: true
).split('\n')
echo "$commits"
}
@NonCPS
def getLastSuccessfulCommit() {
def lastSuccessfulHash = null
def lastSuccessfulBuild = currentBuild.rawBuild.getPreviousSuccessfulBuild()
if ( lastSuccessfulBuild ) {
lastSuccessfulHash = commitHashForBuild( lastSuccessfulBuild )
}
return lastSuccessfulHash
}
@NonCPS
def commitHashForBuild() {
def scmAction = currentBuild?.rawBuild?.actions?.find { action -> action instanceof jenkins.scm.api.SCMRevisionAction }
return scmAction?.revision?.hash
}
currentBuild.rawBuild -带有further APIs的
hudson.model.Run
,仅适用于沙箱外部的受信任库或管理员批准的脚本;该值将不是Serializable
,因此您只能在标记为@NonCPS
的方法内访问它
来源:https://qa.nuxeo.org/jenkins/pipeline-syntax/globals#currentBuild