groovy 删除由特定用户构建的旧作业构建

ssgvzors  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(109)

我想知道是否有可能删除由特定用户构建的旧构建?我知道我可以使用jenkins脚本控制台中的一个脚本,并按编号删除旧构建,但有没有办法按运行它们的人过滤它们?thnx!

huwehgph

huwehgph1#

您可以使用下面的Groovy脚本来实现这一点。为了安全起见,我已经注解掉了实际的delete行,并添加了println。请根据需要调整脚本。

def userNameToCheck="admin"
Jenkins.instance.getAllItems(Job.class).each { jobitem ->
          def jobName = jobitem.name
          def jobInfo = Jenkins.instance.getItem(jobName)

         if(jobInfo.getLastBuild()){

            def lastBuildNum=jobInfo.getLastBuild().getNumber()
            def deletedNumRange = Fingerprint.RangeSet.fromString("0-${lastBuildNum}",false);
            def buildCount=jobInfo.getBuilds(deletedNumRange).size()

             if(buildCount==0){
                 println("No build logs found in  ${jobName} and build history count is : ${buildCount}")
             }
             else{
                 jobInfo.getBuilds(deletedNumRange).each { item ->     
                 // Check if the build was executed by the specified user                      
                   if(item.getCause(Cause.UserIdCause).getUserId().equals(userNameToCheck)) {
                     //item.delete()
                    println("Deleting a Build. For JOB : " + jobName + " for user "   + item.getCause(Cause.UserIdCause).getUserId())
                   }                            
                 }
             }
           } else {
                  println("No builds found in ${jobName}")
           }
   }

相关问题