groovy Jenkins:修改ChangeSet列表以包含自上次成功构建以来的更改

2jcobegt  于 2022-11-01  发布在  Jenkins
关注(0)|答案(1)|浏览(208)

我正在寻找一种方法来修改Jenkins中的变更集列表,以列出自上次成功构建以来的所有变更,而不是自上次构建以来的所有变更,而不管状态如何。
是否有方法对多分支管线执行此操作?
我需要它在我的changeSet中,因为我需要jira插件来拾取所有这些提交
这是我目前掌握的情况

@NonCPS
    def commitHashForBuild(build) {
      def scmAction = build?.actions.find { action -> action instanceof jenkins.scm.api.SCMRevisionAction }
      return scmAction?.revision?.hash
    }
    def getLastSuccessfulCommit() {
      def lastSuccessfulHash = null
      def lastSuccessfulBuild = currentBuild.rawBuild.getPreviousSuccessfulBuild()
      if ( lastSuccessfulBuild ) {
        lastSuccessfulHash = commitHashForBuild(lastSuccessfulBuild)
      }
      return lastSuccessfulHash 
    }
    def modifyChangeSets(commits){
        currentBuild.rawBuild.changeSets = commits
    }

    def call(Map config) {
        def lastSuccessfulCommit = getLastSuccessfulCommit()
        def currentCommit = commitHashForBuild(currentBuild.rawBuild)
        commits = sh(script: "git rev-list $currentCommit \"^$lastSuccessfulCommit\" --pretty=oneline --abbrev-commit  ", returnStdout: true).trim().split('\n').collect{it}

        modifyChangeSets(commits)
    }

控制台中没有错误,但根本不会生成更改列表,并且会中断状态UI屏幕

kgqe7b3p

kgqe7b3p1#

您可以使用下面的Groovy代码从最后一次成功的构建中获取changeSet。

def allChangeSetsFromLastSuccessfulBuild() {
    def job = Jenkins.instance.getItem("$JOB_NAME")
    def lastSuccessBuild = job.lastSuccessfulBuild.number as int
    def currentBuildId = "$BUILD_ID" as int

    def changeSets = []

    for(int i = lastSuccessBuild + 1; i < currentBuildId; i++) {
        echo "Getting Change Set for the Build ID : ${i}"
        def chageSet = job.getBuildByNumber(i).getChangeSets()
        changeSets.addAll(chageSet)
    }
     changeSets.addAll(currentBuild.changeSets) // Add the current Changeset
     return changeSets
}

完整管道示例

函数getFilesChanged将返回在所有更改集中更改的文件列表。

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                git (url: 'https://github.com/xxx/sample.git', branch: 'main')
                script {
                    def changeSets = allChangeSetsFromLastSuccessfulBuild()
                    echo "ChangeSet Size : ${changeSets.size()}"
                    echo "Files Changed : ${getFilesChanged(changeSets)}"
                }
            }
        }
    }
}

def allChangeSetsFromLastSuccessfulBuild() {
    def job = Jenkins.instance.getItem("$JOB_NAME")
    def lastSuccessBuild = job.lastSuccessfulBuild.number as int
    def currentBuildId = "$BUILD_ID" as int

    def changeSets = []

    for(int i = lastSuccessBuild + 1; i < currentBuildId; i++) {
        echo "Getting Change Set for the Build ID : ${i}"
        def chageSet = job.getBuildByNumber(i).getChangeSets()
        changeSets.addAll(chageSet)
    }
     changeSets.addAll(currentBuild.changeSets) // Add the current Changeset
     return changeSets
}

def getFilesChanged(chgSets) {
    def filesList = []
    def changeLogSets = chgSets
        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 files = new ArrayList(entry.affectedFiles)
                    for (int k = 0; k < files.size(); k++) {
                    def file = files[k]
                    filesList.add(file.path)
            }
        }
    }
    return filesList
}

相关问题