pipeline报错误的方法:其中使用到了changeLogSets = currentBuild.changeSets方法,jenkins自带的环境参数,获取repo的变化信息。
def getChangeString() {
MAX_MSG_LEN = 100
def changeString = "\n"
echo "Gathering SCM changes"
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
commit_msg = entry.msg.take(MAX_MSG_LEN)
commit_id = entry.commitId
commit_time = entry.timestamp
commit_author = entry.author
changeString += "----${commit_author},${commit_id},${commit_msg},${commit_time}\n"
"--${truncated_msg} [${truncated_cmt}]\n"
}
}
if (!changeString) {
changeString = "No commit in this build"
}
return changeString
}
查看了官网关于currentBuild.changeSets方法的文档,描述为:
changeSets
a list of changesets coming from distinct SCM checkouts; each has a kind
and is a list of commits; each commit has a commitId
, timestamp
, msg
, author
, and affectedFiles
each of which has an editType
and path
; the value will not generally be Serializable
so you may only access it inside a method marked @NonCPS
需要在方法前加上@NonCPS,因为该参数得到的值通常是不可序列化的
然后再执行,发现还有同样的问题,后来通过查找找到了解决办法,需要对方法中的变量加上def定义,参考资料为:
https://issues.jenkins-ci.org/browse/JENKINS-40274
最终pipeline写法为:
@NonCPS
def getChangeString() {
MAX_MSG_LEN = 100
def changeString = ""
echo "Gathering SCM changes"
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
def commit_msg = entry.msg.take(MAX_MSG_LEN)
def commit_id = entry.commitId
def commit_time = entry.timestamp
def commit_author = entry.author
changeString += "\n----author: ${commit_author},commit id: ${commit_id},commit message: ${commit_msg}\n"
}
}
if (!changeString) {
changeString = "No new commit in this build"
}else {
echo "${changeString}"
}