2016-09-20 36 views
0

我想寫一個小的groovy腳本,將從快照artifactory存儲庫中提供最新的內部版本號,並從jenkins.I運行腳本能夠成功地獲得最新的版本號爲單個工作從artifactory獲取最新的內部版本通過Groovy腳本的多個工作。運行從詹金斯

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 

我試圖做多的工作,但我不能這樣做,可有人告訴我,我怎麼可能在陣列或多個這樣做jobs.Below是我的代碼我想寫

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) 

回答

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)