隐藏从未在声明式管道中运行的阶段

时间:2019-05-20 15:30:04

标签: jenkins-pipeline visualization declarative

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

// 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() }
            [...]
        }

[...]
    }
}

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

1 个答案:

答案 0 :(得分:0)

如果您不介意在您的詹金斯中添加依赖项,请there's this small project

它利用了这个隐藏的詹金斯工具

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

并将其作为库提供,因此在进行一些配置后 library setup

最终用法如下

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

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