【问题标题】:Hide stages that are never run in declarative pipeline隐藏从未在声明性管道中运行的阶段
【发布时间】:2019-05-20 15:30:03
【问题描述】:

我有一个脚本化的管道,我想将它移植到声明性形式。我曾经有过

// Stages used in developement after each single commit
stage('Build') {
}

stage('Unit Tests') {}
// Other stages only for developer
[...]

// Stages used in test only once per day for instance
stage('Deploy') {
    if ( testJob() ) {
    } else {
        Utils.markStageSkippedForConditional(STAGE_NAME)
    }
}
[...]
// Other stages for more testing

然后,对于为开发人员运行的作业,只有在 Jenkins 中可见的管道的第一阶段。 在声明中,我有:

pipeline {
[...]

    stages {
        stage ('Build') {
            [...]
        }

        stage ('Unit Tests') {
            [...]
        }
[...]        
        stage ('Deploy') {
            when { expression { testJob() }
            [...]
        }

[...]
    }
}

但即使是开发工作,我也看到了所有阶段。 有没有办法获得与脚本化管道相同的行为?

【问题讨论】:

    标签: jenkins-pipeline visualization declarative


    【解决方案1】:

    如果你不介意在你的 jenkins 中添加依赖项,there's this small project

    它利用了这个隐藏的 Jenkins 工具

    import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
    // .....
    stage('I am skipped') {
        Utils.markStageSkippedForConditional(STAGE_NAME)
    }
    

    并将其作为库提供,因此经过一些配置

    最终的用法是这样的

    stage('My Conditional Stage') {
        when (BRANCH_NAME != 'master') {
            echo 'Only on master branch.'
        }
    }
    

    结果将是(如果历史上没有结果,即使跳过的阶段列也应该消失)

    【讨论】:

    • 感谢您的回答,但我的问题更具体一些。当前来自同一管道,在作业运行的 Stage 视图或 Blue Ocen 视图中:没有从未运行过的尾随阶段。在您的示例中,它不是一个永远不会运行的落后阶段,它是一个中间阶段。很好,但这不是我要找的。使用声明性时:所有阶段始终可见,即使它们根本没有运行过,即使是尾随阶段。
    • 你是否添加了这个依赖并尝试了当务之急
    • 它适用于脚本化管道,但不适用于我正在寻找的声明性管道。我只是不想显示在某些特定作业中从未运行过的阶段,同时仍然使用单个 Jenkinsfile 并且以声明形式。
    • 这很奇怪,带有库和命令式when 的示例用于声明性管道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    相关资源
    最近更新 更多