在Job的所有已定义工件存储库中搜索最新的Build Number

时间:2016-09-27 15:10:22

标签: java groovy artifactory

我可以使用下面的groovy代码找到最新的内部版本号,但现在我的要求有点改变了。目前我正在尝试仅在libs-snaphot-local artifactory存储库中找到最新的构建号,但现在我想在另外三个存储库中搜索相同作业名称的最新构建号,而不是仅搜索libs-snapshot-local

三个额外的存储库是libs-alpha-local,libs-stage-local和libs-release-local。所以代码应该像它将搜索所有四个人工产品库中的最新构建号,例如

libs-snapshot-local is having build number    3,2 
libs-alpha-local is having build number       8,4 
libs-stage-local is having build number       5,6 
libs-release-local is having build number     9,1

so the latest build number would be 9

下面是我的代码,它只在一个存储库中搜索

import groovy.json.*
import hudson.model.*
import jenkins.model.Jenkins

def applicationLatestBuild = getLatestBuild('application')
def DiscoveryProductsLatestBuild = getLatestBuild('DiscoveryProduct')

//String[] testArray = ["libs-snapshot-local", "libs-alpha-local", "libs-stage-local", "libs-release-local"]

def thr= Thread.currentThread().executable

def getLatestBuild( jobName ) {
  def searchUrl = "http://xyz.test.com:9090/api/search/artifact?name=${jobName}&repos=libs-snapshot-local"
  def conn = searchUrl.toURL().openConnection()
  conn.setRequestProperty("X-Result-Detail", "info, properties")
  def searchResultTxt = conn.content.text
  //println "Found: ${searchResultTxt}"
  def searchResults = new JsonSlurper().parseText(searchResultTxt)
  def builds = searchResults.results.findAll{it.properties["build.number"] != null}.collect { Integer.parseInt(it.properties["build.number"][0]) }.sort().unique().reverse()
  builds[0]
}

def pa = new ParametersAction([
new StringParameterValue("applicationLatestBuild", "${applicationLatestBuild}"),
  new StringParameterValue("DiscoveryProductsLatestBuild", "${DiscoveryProductsLatestBuild}"),
])


// add variable to current job
thr.addAction(pa)
println (applicationLatestBuild)
println(DiscoveryProductsLatestBuild)

1 个答案:

答案 0 :(得分:0)

以下是答案

您需要在此行中添加其他存储库,它将为您提供所需的结果。

def searchUrl =&#34; http://xyz.test.com:9090/api/search/artifact?name= $ {jobName}&amp; repos = libs-snapshot-local,libs-alpha-local,libs-stage-local,libs-release-local&#34; < / p>

import groovy.json.*
import hudson.model.*
import jenkins.model.Jenkins

def applicationLatestBuild = getLatestBuild('application')
def DiscoveryProductsLatestBuild = getLatestBuild('DiscoveryProduct')

//String[] testArray = ["libs-snapshot-local", "libs-alpha-local", "libs-stage-local", "libs-release-local"]

def thr= Thread.currentThread().executable

def getLatestBuild( jobName ) {
  def searchUrl = "http://xyz.test.com:9090/api/search/artifact?name=${jobName}&repos=libs-snapshot-local,libs-alpha-local,libs-stage-local,libs-release-local"
  def conn = searchUrl.toURL().openConnection()
  conn.setRequestProperty("X-Result-Detail", "info, properties")
  def searchResultTxt = conn.content.text
  //println "Found: ${searchResultTxt}"
  def searchResults = new JsonSlurper().parseText(searchResultTxt)
  def builds = searchResults.results.findAll{it.properties["build.number"] != null}.collect { Integer.parseInt(it.properties["build.number"][0]) }.sort().unique().reverse()
  builds[0]
}

def pa = new ParametersAction([
new StringParameterValue("applicationLatestBuild", "${applicationLatestBuild}"),
  new StringParameterValue("DiscoveryProductsLatestBuild", "${DiscoveryProductsLatestBuild}"),
])


// add variable to current job
thr.addAction(pa)
println (applicationLatestBuild)
println(DiscoveryProductsLatestBuild)