Hide stages that are never run in declarative pipeline
Asked Answered
L

1

6

I have a scripted pipeline that I want to port to declarative form. I used to have

// 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

Then for the jobs that run for developers only the 1st stages of the pipeline where visible in Jenkins. In declarative, I have:

pipeline {
[...]

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

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

[...]
    }
}

But even for the development job, I see all the stages. Is there a way to get the same behavior as with scripted pipeline?

Leaven answered 20/5, 2019 at 15:30 Comment(0)
C
1

If you don't mind adding a dependency into your you jenkins, there's this small project.

It utilizes this hidden Jenkins tool

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

And provides it as library, so after some configuration library setup

The final usage would look like

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

The result would be (even the skipped stage column should disappear if no results would be present historically) pipeline with skipped stage

Caputto answered 21/5, 2019 at 13:31 Comment(8)
Thanks for your answer but my question is a little more specific. Currently from the same pipeline, in the Stage view or the Blue Ocen view of the job run: there is no trailing stages that were never ran. In your example, it;'s not a strailing stage that never run, it's a stage in the middle. It's fine but it's not what I'm looking for. When using declarative: all the stages are always visible even if they never have ran at all, even the trailing ones.Leaven
Have you added this dependency and tried out the imperative when?Caputto
It works for scripted pipelines but not for declarative ones that is what I'm looking for. I simply would like to have no display of stages that never run in some specific job while still using a single Jenkinsfile and this in declarative form.Leaven
That's very strange, the example with library and imperative when is for declarative pipelines.Caputto
This is still the top result on google, so I'll just respond to @hakamairi: No it isn't. The example in the repo of imperative-when is for scripted pipelines. You can tell because stage ("x") { when(xx) { [steps here] } } just doesn't work in a declarative pipeline.Tailorbird
@Elec0: I don't remember this one anymore, what if you would do conditionally the following for your stage? org.jenkinsci.plugins.pipeline.modeldefinition.Utils.markStageSkippedForConditional('x') assuming your stage name is 'x'.Caputto
@Caputto yeah you're probably right. You'd have to wrap it in a script { } block, but that might very well work.Tailorbird
The answer is not too bad then @Elec0, unless you are after some other outcome.Caputto

© 2022 - 2024 — McMap. All rights reserved.