jenkins【agent/node】【多node】【并发】汇总

  1. agent配置
agent{
    // node "xxx"
    node "master"
}
// agent any
  1. 多node并发【同步】
pipeline{
    agent none
    options{
        timestamps()
    }
    stages{

        stage('1'){
            agent{
                node "1"
            }
            steps{
                script{
                    deleteDir()
                    bat "dir"
                }
            }
        }


        stage('2'){
            agent{
                node "2"
            }
            steps{
                script{
                    deleteDir()
                    bat "dir"
                }
            }
        }


    }


}
  1. 多node并发【异步】
pipeline{
    agent none
    options{
        timestamps()
    }
    stages{

        stage('1') {
            parallel {
                stage('1.1'){
                    agent{
                        node "1.1"
                    }
                    steps{
                        script{
                            deleteDir()
                            bat "dir"
                        }
                    }
                }

                stage('1.2'){
                    agent{
                        node "1.2"
                    }
                    steps{
                        script{
                            deleteDir()
                            bat "dir"
                        }
                    }
                }

            }
        }

        stage('2'){
            agent{
                node "1.2"
            }
            steps{
                script{
                    deleteDir()
                    bat "ipconfig"
                }
            }
            post {
                always {
                    script{
                        bat "dir"
                    }
                }
            }
        }

        

    } //stages
}
  1. 静态的agent/node使用
pipeline{

    // agent any

    agent{
        // node "node1"
        // node "node2"
        label 'master'
    }
  1. 动态的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"
            }
        }
    }
}

  1. 如何获取分配了标签的所有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
}
  1. 【node】【withCredentials】
def remote1 = [:]
remote1.name = "192up"
remote1.host = "192.168.1.192"
remote1.allowAnyHosts = true

node {
    withCredentials([usernamePassword(credentialsId: '13f813a2-0843-4a9d-xxx', passwordVariable: 'password', usernameVariable: 'userName')]) {
        remote1.user = userName
        remote1.password = password

        stage("cp 192up to 195down ") {
            // writeFile file: '192up.sh', text: 'ls'
            // sshCommand remote: remote1, command: 'for i in {1..5}; do echo -n \"Loop \$i \"; date ; sleep 1; done'
            // sshScript remote: remote1, script: 'test.sh'
            // sshPut remote: remote1, from: '192up.sh', into: '.'
            // sshGet remote: remote1, from: 'test.sh', into: 'test_new.sh', override: true
            //sshRemove remote: remote, path: 'test.sh'
            sshCommand remote: remote1, command: 'rsync -avzu --progress Desktop mi@192.168.1.195:'
        }
    }
}



def remote2 = [:]
remote2.name = "195down"
remote2.host = "192.168.1.195"
remote2.allowAnyHosts = true

node {
    withCredentials([usernamePassword(credentialsId: '13f813a2-0843-4a9d-xxx', passwordVariable: 'password', usernameVariable: 'userName')]) {
        remote2.user = userName
        remote2.password = password

        stage("cp 195down to 192up") {
            // writeFile file: '195down.sh', text: 'ls'
            // sshCommand remote: remote2, command: 'for i in {1..5}; do echo -n \"Loop \$i \"; date ; sleep 1; done'
            // sshScript remote: remote2, script: 'test.sh'
            // sshPut remote: remote2, from: '195down.sh', into: '.'
            // sshGet remote: remote2, from: 'test.sh', into: 'test_new.sh', override: true
            //sshRemove remote: remote, path: 'test.sh'
            sshCommand remote: remote2, command: 'rsync -avzu --progress Desktop mi@192.168.1.192:'
        }
    }
}

posted @ 2021-10-28 09:48  该显示昵称已被使用了  阅读(660)  评论(0)    收藏  举报