【问题标题】:How can I skip a stage if the agent is offline?如果代理离线,我如何跳过一个阶段?
【发布时间】:2019-10-16 05:22:39
【问题描述】:

在我的管道中,我有一个阶段来检查特定计算机(节点)是否离线。如果是,我想跳过下一阶段。但是,下阶段设置为使用离线代理,所以似乎无法检查When子句。

这是我的管道的简化版本:

pipeline {
    agent none

    environment {
        CONTINUERUN = true
    }

    stages {
        stage('Check Should Run') {
            agent any
            steps {
                script {
                    CONTINUERUN = false
                }
            }
        }

        stage('Skip this stage') {
            agent {
                label 'offlineAgent'
            }
            when {
                expression {
                    CONTINUERUN
                }
            }
            steps {
                //Do stuff here
            }
        }
    }
}

当我运行它时,构建只是挂在“跳过这个阶段”阶段。我假设,因为代理处于离线状态。当已知代理离线时,如何跳过此阶段?

【问题讨论】:

  • @RvdK 如果您通读 cmets 关于该问题的答案,您会发现提供的解决方案不再有效。

标签: jenkins jenkins-pipeline jenkins-groovy


【解决方案1】:

为了在分配代理之前评估表达式,您需要在 when 块中添加 beforeAgent 指令。

documentation的相关部分:

stage 中输入agent 之前评估when

默认情况下,stagewhen 条件将在为stage 输入agent 后评估,如果已定义。但是,这可以通过在 when 块中指定 beforeAgent 选项来更改。如果 beforeAgent 设置为 true,则将首先评估 when 条件,并且仅当 when 条件评估为 true 时才会输入 agent

【讨论】:

  • 谢谢@raspy!我自己只是在阅读文档,并来到这里提供这个确切的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多