如何有条件地为管道创建代理"object"?



我有一个管道,我想有条件地将其用于不同的类型的代理。

我不想在管道级别设置默认值,因为即使不使用,每次都会提供代理类型

我有k8s从机和ECS从机,我基本上想这样做,但这不起作用:

pipeline {
agent {
if (true) {
kubernetes {
defaultContainer "mycontainer"
yaml 'my podspec'
}
} else {
node {
label 'my-ecs-cluster'
customWorkspace 'myworkspace'
}
}
}

希望下面的示例管道脚本可以帮助您解决问题。

pipeline
{
agent none
parameters
{
choice(name: 'agent', choices: ['master','slave'], description: 'Agent Selection!')
}
stages
{
stage('Say Hello - Master')
{
when
{
expression
{
agent == 'master' 
}
}
agent
{
node
{
label 'master'
}
}
steps
{
echo 'hello master'
}
}
stage('Say Hello - Slave')
{
when
{
expression
{
agent == 'slave' 
}
}
agent
{
node
{
label 'slave'
}
}
steps
{
echo 'hello slave'
}
}
}
}

最新更新