我找到了一個解決方案,似乎爲我們工作。它圍繞着獲得散列currentBuild
,然後是lastSuccessfulBuild
提交散列。首先,我們寫了一個實用的方法用於獲取給定的詹金斯的提交哈希建對象:
def commitHashForBuild(build) {
def scmAction = build?.actions.find { action -> action instanceof jenkins.scm.api.SCMRevisionAction }
return scmAction?.revision?.hash
}
然後用它來獲取lastSuccessfulBuild
的哈希:
def getLastSuccessfulCommit() {
def lastSuccessfulHash = null
def lastSuccessfulBuild = currentBuild.rawBuild.getPreviousSuccessfulBuild()
if (lastSuccessfulBuild) {
lastSuccessfulHash = commitHashForBuild(lastSuccessfulBuild)
}
return lastSuccessfulHash
}
終於在一個sh
結合這兩個函數來獲得的提交
def lastSuccessfulCommit = getLastSuccessfulCommit()
def currentCommit = commitHashForBuild(currentBuild.rawBuild)
if (lastSuccessfulCommit) {
commits = sh(
script: "git rev-list $currentCommit \"^$lastSuccessfulCommit\"",
returnStdout: true
).split('\n')
println "Commits are: $commits"
}
列表,然後你可以使用commits
陣列來查詢各種東西的Git作爲構建需要。例如。您可以使用此數據獲取自上次成功構建以來所有已更改文件的列表。
我已經把它放到了一個完整的example Jenkinsfile的要點,以顯示它如何適應上下文。
可能的改進是使用Java/Groovy原生Git庫,而不是將其移植到sh
步驟。