2016-06-28 131 views

回答

11

好吧,我设法凑齐了一些东西。我敢肯定,我可以更好地遍历数组,但这里就是我得到了现在:

node('Android') { 
    passedBuilds = [] 

    lastSuccessfulBuild(passedBuilds, currentBuild); 

    def changeLog = getChangeLog(passedBuilds) 
    echo "changeLog ${changeLog}" 
} 

def lastSuccessfulBuild(passedBuilds, build) { 
    if ((build != null) && (build.result != 'SUCCESS')) { 
     passedBuilds.add(build) 
     lastSuccessfulBuild(passedBuilds, build.getPreviousBuild()) 
    } 
} 

@NonCPS 
def getChangeLog(passedBuilds) { 
    def log = "" 
    for (int x = 0; x < passedBuilds.size(); x++) { 
     def currentBuild = passedBuilds[x]; 
     def changeLogSets = currentBuild.rawBuild.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] 
       log += "* ${entry.msg} by ${entry.author} \n" 
      } 
     } 
    } 
    return log; 
    } 
3

基于从CaptRespect我想出了使用下面的脚本在声明管道答案:

def changes = "Changes:\n" 
build = currentBuild 
while(build != null && build.result != 'SUCCESS') { 
    changes += "In ${build.id}:\n" 
    for (changeLog in build.changeSets) { 
     for(entry in changeLog.items) { 
      for(file in entry.affectedFiles) { 
       changes += "* ${file.path}\n" 
      } 
     } 
    } 
    build = build.previousBuild 
} 
echo changes 

这在stage->when->expression部件中仅在某些文件被更改时才运行一个阶段是非常有用的。尽管如此,我还没有得到这个部分,我很想从中创建一个共享库,并使它能够传递一些匹配模式来检查。

编辑:Check the docs顺便说一句,以防你想深入一点。您应该可以将所有object.getSomeProperty()调用转换为entry.someProperty

+0

不错。有一个github回购与一些管道的例子,如果你关心contrubute:https://github.com/jenkinsci/pipeline-examples – CaptRespect

+0

这不是在声明式管道风格,但脚本管道! – Lincoln

+1

@Lincoln,是。你不能用声明来做。您需要做的是将其包装在脚本块中或从共享库中使用它。 – andsens

相关问题