Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-43429

stage when result is not respected when used from a library

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Major Major
    • None
    • Jenkins ver. 2.46.1 (LTS)
      Pipeline: API / Model 1.1.2
      All other plugins are currently up to date too.
      OS: Windows 7

      I am extracting the contents of my Jenkinsfile to a common library to reuse it in a number of projects. I have a build step that may be deactivated related on some user input and this is done via when

      This works fine when using the Jenkinsfile but does not as soon as i put it in a library

      Jenkinsfile
      @Library('mybuild') _
      
      buildPipeline {
          name = 'shared build pipeline'
      }
      

      this is the content of the sharedLibrary:

      var/buildPipeline.groovy
      def call(Closure body) {
          // evaluate the body block, and collect configuration into the object
          def config = [:]
          body.resolveStrategy = Closure.DELEGATE_FIRST
          body.delegate = config
      
          body()
      
          // here starts the "real" pipeline
          pipeline {
              agent any
              stages {
                  stage("Two") {
                      when {
                          expression {
                              echo "Should I run?"
                              return false
                          }
                      }
                      steps {
                          echo "World"
                          echo "Heal it"
                      }
                  }
              } // stages
          } // pipeline
      }
      

      expected is:

      [Pipeline] stage
      [Pipeline] { (Two)
      Stage 'Two' skipped due to when conditional
      [Pipeline] echo
      15:36:18 Should I run?
      [Pipeline] }
      [Pipeline] // stage
      

      but i get

      [Pipeline] stage
      [Pipeline] { (Two)
      [Pipeline] echo
      World
      [Pipeline] echo
      Heal it
      [Pipeline] }
      [Pipeline] // stage
      

      anything i am missing?

          [JENKINS-43429] stage when result is not respected when used from a library

          Andrew Bayer added a comment -

          Yeah, Declarative Pipelines defined in libraries aren't guaranteed to work due to parsing requirements.

          Andrew Bayer added a comment - Yeah, Declarative Pipelines defined in libraries aren't guaranteed to work due to parsing requirements.

          abayer is this limitation documented, did i miss something and are actions taken to change it or should i go without declarative pipeline syntax if i want to use shared libraries?

          I have to manage a whole bunch of jobs that are nearly equal and the shared library configuration is one of the key benefits in the Jenkins Pipeline ecosystem offers. If that is not explicitly supported with declarative pipelines (the current test cases don't seem to have any shared library tests, right?) and anything may break at any time, then declarative pipelines are quiet useless to me and most likely many others too.

          Daniel Geißler added a comment - abayer is this limitation documented, did i miss something and are actions taken to change it or should i go without declarative pipeline syntax if i want to use shared libraries? I have to manage a whole bunch of jobs that are nearly equal and the shared library configuration is one of the key benefits in the Jenkins Pipeline ecosystem offers. If that is not explicitly supported with declarative pipelines (the current test cases don't seem to have any shared library tests, right?) and anything may break at any time, then declarative pipelines are quiet useless to me and most likely many others too.

          Liam Newman added a comment -

          Bulk closing resolved issues.

          Liam Newman added a comment - Bulk closing resolved issues.

            abayer Andrew Bayer
            dageissl Daniel Geißler
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: