2016-06-28 57 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

相關問題