zoukankan      html  css  js  c++  java
  • 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:'
            }
        }
    }
    
    
  • 相关阅读:
    osharp3引入事务后操作结果类别的调整
    Code First 迁移
    表达式拼接Expression<Func<IEntityMapper, bool>> predicate
    ASP.NET中Session的sessionState 4种mode模式
    EF Code First Migrations数据库迁移
    啊里大鱼短信发送API
    asp.net服务器控件的生命周期
    osharp3使用经验:整合DbContextScope 文章 1
    关于MarshalByRefObject的解释
    数据库操作事务IsolationLevel 枚举
  • 原文地址:https://www.cnblogs.com/amize/p/15474153.html
Copyright © 2011-2022 走看看