我正在尝试有条件地在另一个从属服务器上运行我的管道阶段。但这不能正常工作。主管道以标签ecs-slave
运行(即使用容器从属)。
如果X在具有标签X的其他从属上运行此阶段,我希望能够保存,否则通常继续使用当前从属。
发生的是,詹金斯用标签ecs-slave设置了另一个新的从属,而不是使用当前的从属。
pipeline {
agent {
node {
label 'ecs-slaves'
}
}
stages {
stage('ssss') {
// Regarless of whether the label is different or the same (ecs-slaves) jenkins provisions a _new_ slave
agent {
node {
label "${(var == 'blahblah') ? 'ecs-slaves' : 'some-others-slave'}"
}
}
environment{...}
steps{
....
}
}
答案 0 :(得分:0)
if / else封装在变量块({}
)中。您只需要在这些括号内添加 var 。
node {
label "(${var} == 'blahblah') ? 'ecs-slaves' : 'some-others-slave'"
}
还有另一个问题。无论阶段ssss
处于什么状态,管道的布局方式都会生成一个新的从属,即使名称相同。您想使用when块来避免这种情况。请查看this example,我将在下面提供。
pipeline {
agent {
node {
label 'ecs-slaves'
}
}
parameters {
choice(
choices: ["${env.NODE_NAME}", 'other-slaves'],
description: '',
name: 'RUN_ON_SLAVE'
)
}
stages {
// Trigger this only when running on other slaves and set a new node
stage('ssss') {
when {
expression { params.RUN_ON_SLAVE == 'other-slaves' }
}
// Regardless of whether the label is different or the same (ecs-slaves) jenkins provisions a _new_ slave
agent {
node {
label 'other-slaves'
}
}
environment{...}
steps{
....
}
}
// Trigger this when 'ssss' is not triggered and don't run on a new node
stage('ssst') {
when {
expression { params.RUN_ON_SLAVE == "${env.NODE_NAME}" }
}
environment{...}
steps{
....
}
}
}
}
答案 1 :(得分:0)
单个ECS容器代理上的多个构建执行程序当前不受支持。由于您已经将ecs-slaves
定义为管道顶层的代理,因此,在那里生成的容器将保持锁定状态,直到管道结束。因此,阶段ssss
会(理论上)由于在同一容器上缺少另一个执行程序而无限期地排队,或者将生成与您的情况相同标签的另一个容器。