通过Groovy脚本从多个作业的artifactory获取最新的Build Number。从jenkins运行

时间:2016-09-20 01:53:48

标签: java jenkins gradle groovy artifactory

我正在尝试编写一个小的groovy脚本,它将从快照工件库中提供最新的构建号,并从jenkins运行脚本。我能够为单个作业成功获取最新的构建号

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

    def PoidonServicesLatestBuild = getLatestBuild('PoidonServices')
    def applicationLatestBuild = getLatestBuild(application)

    def getLatestBuild( jobName ) {
      def searchUrl = "http://xyz.nbc.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("PoidonServicesLatestBuild", "${PoidonServicesLatestBuild}"),
      new StringParameterValue("applicationLatestBuild ", "${ applicationLatestBuild }"),
    ])

// add variable to current job
thr.executable.addAction(pa)

我正在尝试为多个工作做这件事,但是我无法做到这一点有人能告诉我怎么能在数组或多个工作中这样做。下面是我试写的代码

{{1}}

1 个答案:

答案 0 :(得分:0)

下面的代码工作正常,并从快照存储库中为我提供了最新的构建号,用于受尊重的作业。

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

def applicationdataLatestBuild = getLatestBuild('PoidonServices')
def CareDiscoveryProductsLatestBuild = getLatestBuild('application')


def thr= Thread.currentThread().executable

def getLatestBuild( jobName ) {
  def searchUrl = "http://xyz.nbc.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()
  println "Using ${jobName} build ${builds[0]}"
  builds[0]
}

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


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