[英]running the same task on multiple agents in my Jenkins declarative pipeline
[英]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.