2017-05-09 108 views
1

我想在具有相同標籤的所有代理中的Jenkins管道中運行特定任務。爲了這個目的我得到了下面的代碼。但它沒有按預期工作。 它只在標籤爲「my_label」的第一個從站中運行,然後退出。我需要用這個標籤在所有詹金斯奴隸上運行這個工作。Jenkinsfile:在所有具有相同標籤的代理中運行任務

任何幫助將不勝感激。

def labels=["my_label"] def builders=[:] for (x in labels) { 
    def label=x builders[label]= { 
    node(label) { 
     // build steps that should happen on all nodes go here 
     // Step 4 
     stage('Run deployment on all agents in the given environment') { 
     sh "echo Run deployment" sh "echo release_version = ${params.release_version}" sh "echo environment = ${params.environment}" 
     } 
    } 
    } 
} 

parallel builders 

感謝, 阿倫小號

回答

0

我發現這個代碼,做你問什麼。但是,您需要取消選中「沙箱」

// The script triggers PayloadJob on every node. 
// It uses Node and Label Parameter plugin to pass the job name to the payload job. 
// The code will require approval of several Jenkins classes in the Script Security mode 
def branches = [:] 
def names = nodeNames() 
for (int i=0; i<names.size(); ++i) { 
    def nodeName = names[i]; 
    // Into each branch we put the pipeline code we want to execute 
    branches["node_" + nodeName] = { 
    node(nodeName) { 
     echo "Triggering on " + nodeName 
    } 
    } 
} 

// Now we trigger all branches 
parallel branches 

// This method collects a list of Node names from the current Jenkins instance 
@NonCPS 
def nodeNames() { 
    return jenkins.model.Jenkins.instance.nodes.collect { node -> node.name } 
} 
相關問題