有条件地在不同的从属/工作区上运行 Jenkins 阶段,否则使用现有的从属/工作区

问题描述 投票:0回答:2

我正在尝试有条件地在另一个从站上运行我的管道的一个阶段。但这不能正常工作。主管道以标签

ecs-slave
运行(我正在使用容器奴隶)。

如果 X 在带有标签 X 的不同从站上运行此阶段,我希望能够保存,否则像往常一样继续使用当前从站。

取而代之的是 Jenkins 提供了另一个带有标签 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{
              ....
            }
        }
jenkins jenkins-pipeline jenkins-plugins
2个回答
0
投票

if/else 封装在变量块中(

{}
)。你只需要那些括号内的var

node {
  label "(${var} == 'blahblah') ? 'ecs-slaves' : 'some-others-slave'"
}

还有一个问题。无论阶段

ssss
中有什么,管道的布局方式都会生成一个新的从属,即使名称相同。您想使用 when 块来避免这种情况。查看这个例子,我将在下面提供。

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{
                ....
            }
        }
    }
}

0
投票

目前不支持单个 ECS 容器代理上的多个构建执行器。由于您已经将

ecs-slaves
定义为管道顶层的代理,因此在那里生成的容器将保持锁定状态,直到管道结束。因此,阶段
ssss
将由于同一容器上缺少另一个执行者而无限期地(理论上)保持排队状态,或者它会产生另一个与您的情况具有相同标签的容器。

© www.soinside.com 2019 - 2024. All rights reserved.