Jenkins - abort running build if new one is started

前端 未结 9 2521
失恋的感觉
失恋的感觉 2020-12-04 15:51

I use Jenkins and Multibranch Pipeline. I have a job for each active git branch. New build is triggered by push in git repository. What I want is to abort running builds in

9条回答
  •  难免孤独
    2020-12-04 16:01

    I also compiled a version from the previously given ones with a few minor tweaks:

    • the while() loop generated multiple outputs for each build
    • the UserInterruption currently expects a userId instead of a reasoning string, and will not show a reasoning string anywhere. Therefore this just provides the userId
    def killOldBuilds(userAborting) {
        def killedBuilds = []
        while(currentBuild.rawBuild.getPreviousBuildInProgress() != null) {
            def build = currentBuild.rawBuild.getPreviousBuildInProgress()
            def exec = build.getExecutor()
    
            if (build.number != currentBuild.number && exec != null && !killedBuilds.contains(build.number)) {
                exec.interrupt(
                        Result.ABORTED,
                        // The line below actually requires a userId, and doesn't output this text anywhere
                        new CauseOfInterruption.UserInterruption(
                                "${userAborting}"
                        )
                )
                println("Aborted previous running build #${build.number}")
                killedBuilds.add(build.number)
            }
        }
    }
    

提交回复
热议问题