Sushant Sushant - 2 months ago 45
Groovy Question

Get the latest Build Number from artifactory for multiple jobs through Groovy script.Running from jenkins

I am trying to write a small groovy script which will give the latest build number from the snapshot artifactory repository and running the script from jenkins.I am able to get the latest build number successfully for a single job

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

// get current thread / Executor
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]
}

Result is :

Result: 168


I am trying to do it for multiple jobs but I am not able to do so can someone tell me how could I do this in array or for multiple jobs.Below is my code I am trying to write

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)

Answer

Below code is working fine and giving me the latest build number from the snapshot repository for the respected jobs.

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)