2017-07-25 49 views
3

情景:我想基於當前作業接收的輸入參數動態觸發少量下游作業(作業A和作業B ....) 。如何根據某些輸入參數動態地在jenkins中觸發多個下游作業

+0

這是什麼輸入參數呢? – Suresh

+0

輸入將是應該觸發的作業名稱(逗號分隔)。 –

+0

您有沒有給出一個路徑將此參數傳遞給[參數化觸發器插件](https://wiki.jenkins.io/display/JENKINS/Parameterized+Trigger+Plugin) – Suresh

回答

1
import hudson.model.* 

def values = ${configname}.split(',') 
def currentBuild = Thread.currentThread().executable 

println ${configname} 
println ${sourceBranch} 

values.eachWithIndex { item, index -> 
    println item 
    println index 

def job = hudson.model.Hudson.instance.getJob(item) 
def params = new StringParameterValue('upstream_job', ${sourceBranch}) 
def paramsAction = new ParametersAction(params) 
def cause = new hudson.model.Cause.UpstreamCause(currentBuild) 
def causeAction = new hudson.model.CauseAction(cause) 
hudson.model.Hudson.instance.queue.schedule(job, 0, causeAction, paramsAction) 
} 

怎麼這樣呢?我從上游系統得到一個以逗號分隔的列表,我將它們分解爲個人內部工作的字符串。通過傳遞每個單獨的字符串來打電話。

1

這Jenkinsfile會這麼做:

#!/usr/bin/env groovy 

pipeline { 
    agent { label 'docker' } 
    parameters { 
    string(name: 'myHotParam', defaultValue: '', description: 'What is your param, sir?') 
    } 
    stages { 
    stage('build') { 
     steps { 
     script { 
      if (params.myHotParam == 'buildEverything') { 
      build 'mydir/jobA' 
      build 'mydir/jobB' 
      } 
     } 
     } 
    } 
    } 
} 
+0

感謝您的答案,將嘗試這一點。 –

相關問題