繁体   English   中英

在声明性管道中并行在多个代理上运行相同的 Jenkins 作业

[英]Running same Jenkins job on multiple agents in parallel in declarative pipeline

这是我所拥有的:

#!/usr/bin/env groovy

pipeline {
    agent none
    stages {
        stage('Checkout SCM') {
            agent { label 'win' && 'apple' && 'rhel' }
            steps {
                echo "Cloning Repository"
                checkout([$class: 'GitSCM',
                    branches: [[name: "*/develop"]],
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [[$class: 'WipeWorkspace']],
                    submoduleCfg: [],
                    userRemoteConfigs: [[credentialsId: 'UNAME', url: 'URL']],
                    browser: [$class: 'BitbucketWeb', repoUrl: 'URL'],
                ])}}

        stage('Building Win64, Linux764, MacOS') {
            agent { label 'win&&rhel&&apple' }
            steps {
                   script {
                        echo '************************'
                        echo '*****BUILDING JOBS******'
                        echo '************************'
                        sh 'python build.py'
                        sh 'cd ion-js && npm run prepublishOnly'
                      }}}
    }
} 

但是我得到了There are no nodes with the label 'win && rhel && apple'错误。 有没有人碰巧知道如何运行声明性 jenkins 管道,其中一个阶段在多个代理标签上并行运行?

我想同时将同一个 git repo 签出到 3 个不同的节点。 我试过agent { label 'win' && 'apple' && 'rhel' }agent { label 'win&&apple&&rhel' }但它只是说它找不到那个标签。

这里他们说你可以使用|| 并且使用&&应该可以工作,但我不确定我错过了什么。 我可以编写 3 个不同的结帐阶段,但我认为有更好的方法

我尝试过同样的事情但没有成功。 我知道的唯一解决方案是有一个parallel块并为每个代理/节点/标签多次定义阶段。

stage('Building Win64, Linux764, MacOS') {
  parallel {
    stage('Win64') {
      agent {
        label 'win-10-x64'
      }
      steps {
      ...
      }
    }
    stage('Linux64') {
      agent {
        label 'linux-x64'
      }
      steps {
      ...
      }
    }
    stage('MacOS') {
      agent {
        label 'macos'
      }
      steps {
      ...
      }
    }
  }
}

要从 Micah 添加更多答案,您可以定义一个函数,该函数可以为您创建一个阶段,并将在您选择的不同代理节点上执行生成阶段,而不是用不同的标签重复阶段。

def agents  = ['win64', 'linux64', 'macos']
 
def generateStage(nodeLabel) {
    return {
        stage("Runs on ${nodeLabel}") {
            node(nodeLabel) {
                steps {
                   script {
                        echo "Running on ${nodeLabel}"
                        echo '************************'
                        echo '*****BUILDING JOBS******'
                        echo '************************'
                        sh 'python build.py'
                        sh 'cd ion-js && npm run prepublishOnly'
                      }
                 }
            }
        }
    }
}
def parallelStagesMap = agents.collectEntries {
    ["${it}" : generateStage(it)]
}
pipeline {
    agent none
    stages {
        stage('non-parallel stage') {
            steps {
                echo 'This stage will be executed first.'
            }
        }
        stage('parallel stage') {
            steps {
                script {
                    parallel parallelStagesMap
                }
            }
        }       
    }
}

由于我们在调用 parallelStageMap 时有 parallel 关键字,因此同一阶段将在代理节点上并行执行。

专业提示:您可以在函数中定义更多步骤,这些步骤是在所有代理上执行的。 如果你想定义标签和阶段名称,你可以添加另一个名为stagename参数,并且可以解析到函数generateStage

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM