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


    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:'
            }
        }
    }
    
    
  • 相关阅读:
    菜鸟看懂算法以后之一:头痛的64次左移
    C语言通过指针数组和二维数组读取文件
    C++中构造函数调用构造函数
    bnuoj53075 外挂使用拒绝
    [CodeForces]String Reconstruction
    BNU-2017.7.4排位赛2总结
    BNU-2017.7.5排位赛3总结
    BNU-2017.7.3排位赛1总结
    微软大楼设计方案(困难)
    最长公共子序列针对小字符集的算法
  • 原文地址:https://www.cnblogs.com/amize/p/15474153.html
Copyright © 2020-2023  润新知