[英]Running same Jenkins job on multiple agents in parallel in declarative pipeline
here's what I have:这是我所拥有的:
#!/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'
}}}
}
}
However I get the There are no nodes with the label 'win && rhel && apple'
error.但是我得到了There are no nodes with the label 'win && rhel && apple'
错误。 Does anyone happen to know how to run a declarative jenkins pipeline where one of the stages is ran on multiple agent labels in parallel?有没有人碰巧知道如何运行声明性 jenkins 管道,其中一个阶段在多个代理标签上并行运行?
I want to checkout the same git repo to 3 different nodes at the same time.我想同时将同一个 git repo 签出到 3 个不同的节点。 I've tried agent { label 'win' && 'apple' && 'rhel' }
and agent { label 'win&&apple&&rhel' }
but it just says it can't find that label.我试过agent { label 'win' && 'apple' && 'rhel' }
和agent { label 'win&&apple&&rhel' }
但它只是说它找不到那个标签。
Here they say you can use ||
这里他们说你可以使用||
and using &&
should work but I'm not sure what I'm missing.并且使用&&
应该可以工作,但我不确定我错过了什么。 I could write 3 different checkout stages but I figured there was a better way我可以编写 3 个不同的结帐阶段,但我认为有更好的方法
I have tried the same things with no success.我尝试过同样的事情但没有成功。 The only solution I'm aware of is to have a parallel
block and define the stage multiple times, for each agent/node/label.我知道的唯一解决方案是有一个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 {
...
}
}
}
}
To add more to answer from Micah, Instead of repeating stage with different labels, you can define a function which can create a stage for you and will be executing the generate stage on different agent nodes of your choice.要从 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
}
}
}
}
}
Since we have parallel keyword while calling parallelStageMap, the same stage will be executed in parallel on the agent nodes.由于我们在调用 parallelStageMap 时有 parallel 关键字,因此同一阶段将在代理节点上并行执行。
ProTip: You can define more steps inside the function which are common to be executed on all agents.专业提示:您可以在函数中定义更多步骤,这些步骤是在所有代理上执行的。 If you wanted to defined label and stage name, you can added another argument named stagename
and can parse into the function generateStage
.如果你想定义标签和阶段名称,你可以添加另一个名为stagename
参数,并且可以解析到函数generateStage
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.