1

我目前使用的是Build Flow插件,它似乎已被放棄,以支持Jenkins 2.0中的管道。從Jenkins 2.0中的並行執行訪問構建管道

遇到一些問題,使用新的管道重新構建我們現有的作業。

目前,我有類似下面的代碼:

ignore(FAILURE) { 
    join = parallel([ 
    job1: {build('job1')}, 
    job2: {build('job2')}, 
    job3: {build('job3')} 
    ]) 
} 
results = [join.job1.result.toString(), join.job2.result.toString(), join.job2.result.toString()] 

if(join.job1.result.toString() == 'SUCCESS') { 
    buildList << join.job1.lastBuild.getDisplayName() 
} 

這裏的目標是並行運行多個現有的工作,然後對構建已完成訪問信息。這在Build Flow插件中一直沒有問題。

我一直無法找到使用新管道訪問這些數據的方法。

echo 'Checking streams for latest builds' 
join = [:] 

join['Job1'] = { build job: 'Job1', parameters: [[$class: 'StringParameterValue', name: 'TimeWindow', value: '1200']], propagate: false} 
join['Job2'] = { build job: 'Job2', parameters: [[$class: 'StringParameterValue', name: 'TimeWindow', value: '1200']], propagate: false} 
join['Job3'] = { build job: 'Job3', parameters: [[$class: 'StringParameterValue', name: 'TimeWindow', value: '1200']], propagate: false} 

parallel join 

連接['Job1']的轉儲不允許訪問AbstractBuild或類似的構建流插件。相反,它表明:

<[email protected] 
[email protected] 
de[email protected] 
[email protected] 
[email protected] 
resolveStrategy=0 
directive=0 
parameterTypes=null 
maximumNumberOfParameters=0 
bcw=null> 

使用新的管道,有沒有)來訪問像job1.result,job1.lastBuild,job1.lastBuild.getDisplayName數據(的方法嗎?

回答

3

您可以使用詹金斯API的parallel步驟之後訪問這些數據:

Jenkins.instance.getItemByFullName('Job1').lastBuild 
+0

儘管這將需要一些腳本的批准,如果你是在sandb運行氧氣環境。 – amuniz

+0

你也可以在「src」中寫一個groovy類來獲取org.jvnet.hudson.main並獲取當前實例的句柄。這樣做我不認爲我需要修改我的任何安全設置。 – red888

2

有點晚了,但你還可以定義runWrapper對象通過build命令將在關閉返回,並將其放置在地圖在parallel命令之外定義。

下面是一個例子。 說明:我使用的是propagate: false,因此不會拋出異常(JUnit測試失敗等)。你必須決定要如何處理異常,的try/catch /終於等

實例管道作業執行(需要用繩子PARAM commandStr進行參數):

env.PASSED_CMD="${params.commandStr}" 
stage('command-exec') { 
    node { 
     sh "${commandStr}" 
    } 
} 

執行工作(配置):

buildRuns = [:] 
buildResults = [:] 
def buildClosure(String jobKey, String paramAValue) { 
    return { 
     def runWrapper = build(
      job: 'command-test-job', 
      propagate: false, 
      parameters: [[$class: 'StringParameterValue', name: 'commandStr', value: paramAValue]] 
     ) 
     buildResults."$jobKey" = runWrapper 
    } 
} 
buildRuns."job1" = buildClosure("job1", "echo 'HI' && exit 0") 
buildRuns."job2" = buildClosure("job2", "echo 'HO' && exit 0") 
parallel buildRuns 
for(k in buildRuns.keySet()) { 
    def runResult = buildResults."$k" 
    echo "$k -> ${runResult.result}" 
    echo "$k -> ${runResult.buildVariables.PASSED_CMD}" 
} 

構建日誌顯示:

[Pipeline] parallel 
[Pipeline] [job1] { (Branch: job1) 
[Pipeline] [job2] { (Branch: job2) 
[Pipeline] [job1] build (Building command-test-job) 
[job1] Scheduling project: command-test-job 
[Pipeline] [job2] build (Building command-test-job) 
[job2] Scheduling project: command-test-job 
[job1] Starting building: command-test-job #7 
[job2] Starting building: command-test-job #8 
[Pipeline] [job2] } 
[Pipeline] [job1] } 
[Pipeline] // parallel 
[Pipeline] echo 
job1 -> SUCCESS 
[Pipeline] echo 
job1 -> echo 'HI' && exit 0 
[Pipeline] echo 
job2 -> SUCCESS 
[Pipeline] echo 
job2 -> echo 'HO' && exit 0 
[Pipeline] End of Pipeline 
Finished: SUCCESS 
0

釷與Steve-B's Answer非常相似,但您實際上並不需要明確定義運行包裝,或者事先將其放置在附加地圖中。

TL;博士您只需通過直接循環的並行構建存儲到HashMap和訪問地圖上它的keySet代替


拿這個答案與鹽糧,我使用的是舊版本的管道(Jenkins 2.7.2和Pipeline 2.2)。

您可以將並行構建結果存儲到一個哈希映射並遍歷該映射的keySet以獲取有關該構建的一些信息。

def create_build_job(job_name, pool_label="master", propagate=false) { 
    build job: job_name, parameters: [[$class: 'LabelParameterValue', name: "node_label", label: "${pool_label}"]], propagate: propagate, wait:true 
} 

def buildmap = [:] 
def build_results 

stage 'Perform Build' 
    //test1 is set to fail, test2 is set to succeed 
    buildmap['test1'] = {create_build_job('test1', "your_node_label")} 
    buildmap['test2'] = {create_build_job('test2', "your_node_label")} 

    build_results = parallel buildmap 

    for(k in build_results.keySet()){ 
     println build_results["${k}"].getProperties() 
    } 

對於這個管道,我只是傾銷全部存放在項目在地圖的RunWrapper的特性,但是您可以直接訪問每個屬性,所以如果你想你可以做構建的結果:

build_results["${k}"].result 

通過這條管道生產(編修任何潛在的身份信息控制檯輸出爲:

Started by user <user> 
[Pipeline] stage (Perform Build) 
Entering stage Perform Build 
Proceeding 
[Pipeline] parallel 
[Pipeline] [test1] { (Branch: test1) 
[Pipeline] [test2] { (Branch: test2) 
[Pipeline] [test1] build (Building test1) 
[test1] Scheduling project: test1 
[test1] Starting building: test1 #11 
[Pipeline] [test2] build (Building test2) 
[test2] Scheduling project: test2 
[test2] Starting building: test2 #11 
[Pipeline] } 
[Pipeline] } 
[Pipeline] // parallel 
[Pipeline] echo 
{rawBuild=test1 #11, class=class org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper, absoluteUrl=<jenkins_url>/job/test1/11/, buildVariables={}, previousBuil[email protected]1480013a, id=11, nextBuild=null, changeSets=[], result=FAILURE, description=null, startTimeInMillis=1509667550519, timeInMillis=1509667550510, duration=956, number=11, displayName=#11} 
[Pipeline] echo 
{rawBuild=test2 #11, class=class org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper, absoluteUrl=<jenkins_url>/job/test2/11/, buildVariables={}, previousBuil[email protected]2d9c7128, id=11, nextBuild=null, changeSets=[], result=SUCCESS, description=null, startTimeInMillis=1509667550546, timeInMillis=1509667550539, duration=992, number=11, displayName=#11} 
[Pipeline] End of Pipeline 
Finished: SUCCESS