Jenkinsfile-获取内部版本之间的所有更改 [英] Jenkinsfile - get all changes between builds
问题描述
参考此问题在使用多分支管道时是否可以从中获取等效信息?具体来说-自上次成功构建以来的提交列表.
With reference to this question is there a way to get the equivalent information from when using the mult-branch pipeline? Specifically - the list of commits since the last successful build.
当前,我们使用以下
def scmAction = build?.actions.find { action ->
action instanceof jenkins.scm.api.SCMRevisionAction
}
return scmAction?.revision?.hash
,但是如果推送了多个提交,这只会返回触发构建的最后一个提交.我接受新分支的第一个构建可能会含糊不清,但是获取在可能的情况下触发构建的提交列表 将非常有用.
but this only returns the last commit that triggered the build if multiple commits were pushed. I accept that the very first build of a new branch might be ambiguious but getting a list of commits that triggered the build when possible would be very useful.
推荐答案
我找到了一个似乎对我们有用的解决方案.它围绕着获取currentBuild
提交哈希值,然后是lastSuccessfulBuild
提交哈希值.首先,我们编写了一个实用程序方法来获取给定Jenkins构建对象的提交哈希:
I have found a solution that seems to work for us. It revolves around getting the currentBuild
commit hash and then the lastSuccessfulBuild
commit hash. First we wrote a utility method for getting a commit hash of a given Jenkins build object:
def commitHashForBuild(build) {
def scmAction = build?.actions.find { action -> action instanceof jenkins.scm.api.SCMRevisionAction }
return scmAction?.revision?.hash
}
然后使用它来获取lastSuccessfulBuild
的哈希值:
then use that to get the lastSuccessfulBuild
's hash:
def getLastSuccessfulCommit() {
def lastSuccessfulHash = null
def lastSuccessfulBuild = currentBuild.rawBuild.getPreviousSuccessfulBuild()
if ( lastSuccessfulBuild ) {
lastSuccessfulHash = commitHashForBuild(lastSuccessfulBuild)
}
return lastSuccessfulHash
}
最后在sh
函数中将二者结合起来以获取提交列表
finally combine those two in a sh
function to get the list of commits
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中的各种内容.例如.您可以使用此数据获取自上次成功构建以来所有已更改文件的列表.
you can then use the commits
array to query various things in Git as your build requires. E.g. you can use this data to get a list of all changed files since the last successful build.
我已将其放入完整的示例Jenkinsfile 要点以显示其如何在上下文中融合在一起
I have put this into a complete example Jenkinsfile Gist to show how it fits together in context.
一个可能的改进是使用Java/Groovy本地Git库,而不是使用sh
步骤.
A possible improvement would be to use a Java/Groovy native Git library instead of shelling out to a sh
step.
这篇关于Jenkinsfile-获取内部版本之间的所有更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!