[英]Jenkins declarative pipeline configuring the agent using shared library
我希望能夠根據我的共享庫 groovy 文件中的參數指定代理。 我知道下面的例子會起作用,但它需要我復制管道兩次:
// vars/selectAgent.groovy
def call(String agent) {
if (agent === "any") {
pipeline {
agent any
stages {
stage('Stage1') {
steps {
echo "ran on any agent"
}
}
}
}
} else {
pipeline {
agent {
label "$agent"
}
stages {
stage(‘Stage1') {
steps {
echo "The build ran on agent label ${agent}"
}
}
}
}
}
}
是否可以創建一個代理語句,該語句將呈現為
agent any
或者
agent {
label 'myLabel'
}
無需重復整個管道? 請注意,我的管道比這個簡單的例子要長得多。 我查看了 GroovyASTTransform,但我不知道這是如何工作的,如果可能的話以及如何去做。
顯然我希望我的 Jenkinsfile 包含
selectAgent('any')
或者
selectAgent('myLabel')
階段級代理選擇,並添加表達式作為條件。 通過這種方式,您將只為外部參數執行 1 個階段。
def call(String agent) {
pipeline {
agent none
stages {
stage('Even Stage') {
when {
expression { ${params.agent} == 'any' }
}
agent {label 'agent1'}
steps {
echo "The build number is even"
}
}
stage('Odd Stage') {
when {
expression { !${params.agent} == 'any' }
}
agent {label 'agent2'}
steps {
echo "The build number is odd"
}
}
}
}
}
注意: Pipeline 沒有經過測試,但它應該可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.