- 静态的agent/node使用
pipeline{
// agent any
agent{
// node "node1"
// node "node2"
label 'master'
}
- 动态的agent/node使用
def AGENT_LABEL = null
node('master') {
stage('Checkout and set agent'){
checkout scm
### Or just use any other approach to figure out agent label: read file, etc
if (env.BRANCH_NAME == 'master') {
AGENT_LABEL = "prod"
} else {
AGENT_LABEL = "dev"
}
}
}
pipeline {
agent {
label "${AGENT_LABEL}"
}
stages {
stage('Normal build') {
steps {
echo "Running in ${AGENT_LABEL}"
sh "hostname"
}
}
stage ("Docker build") {
agent{
dockerfile {
dir 'Dockerfiles'
label "${AGENT_LABEL}"
}
}
steps{
sh "hostname"
}
}
}
}
- 如何获取分配了标签的所有Jenkins节点的列表,包括主节点?
@NonCPS
def hostNames(label) {
def nodes = []
jenkins.model.Jenkins.instance.computers.each { c ->
if (c.node.labelString.contains(label)) {
nodes.add(c.node.selfLabel.name)
}
}
return nodes
}
@NonCPS
def hostNames(label) {
def nodes = []
jenkins.model.Jenkins.get.computers.each { c ->
c.node.labelString.split(/\s+/).each { l ->
if (l != null && l.equals(label)) {
nodes.add(c.node.selfLabel.name)
}
}
}
return nodes
}