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

Sharing of declarative pipeline between similar projects

      There are multiple projects with similar pipelines.

      I can reuse declarative pipeline right now by importing library with pipeline defined:
      Jenkinsfile:

      @Library('github.com/my/libs@master') _
      
      common_jenkinsfile {
        // project parameters
      }
      

      github.com/my/libs/common_jenkinsfile.groovy:

      def call(body) {
          def config = [:]
          body.resolveStrategy = Closure.DELEGATE_FIRST
          body.delegate = config
          body()
      
          pipeline {
              agent any
              stages {
                  stage('This one should be skipped') {
                      when {
                          expression { false }
                      }
                      steps {
                          echo "this should be skipped, but it does not ("
                      }
                  }
              }
          }
      }
      

      The problems are:

      • This is not officially supported, so can break with any next Jenkins release
      • 'when' does not work in this case, so stages are never skipped

          [JENKINS-45306] Sharing of declarative pipeline between similar projects

          Alexander Vorobiev created issue -
          Alexander Vorobiev made changes -
          Description Original: Stage with dedicated agent in this pipeline is not skipped:

          \{\{
           pipeline \{
           agent none
           stages \{
           stage('This one is not skipped :(') \{
           when \{
           expression

          \{ false }
           }
           agent any
           steps \{ echo "this should be skipped, but it won't" }
           }
           }
           }
           }}
           
           Log:
           [Pipeline] stage
           [Pipeline] \{ (This one is not skipped :()
           [Pipeline] node
           Running on iOS-xc in /Users/jenkins/slave/workspace/Pipeline_when
           [Pipeline] \{
           Stage 'This one is not skipped :(' skipped due to when conditional
           [Pipeline] }
           [Pipeline] // node
           [Pipeline] }
           [Pipeline] // stage
           [Pipeline] End of Pipeline
           
           Simple pipeline with single agent works fine:
           \{\{
           pipeline \{
           agent none
           stages \{
           stage('This one is not skipped :(') \{
           when \{
           expression \{ false }

          }
           agent any
           steps

          \{ echo "this should be skipped, but it won't" }

          }
           }
           }
           }}

          Log:
          [Pipeline] node
          Running on iOS-xc in /Users/jenkins/slave/workspace/Pipeline_when
          [Pipeline] \{
          [Pipeline] stage
          [Pipeline] \{ (This on is skipped)
          Stage 'This on is skipped' skipped due to when conditional
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          New: Stage with dedicated agent in this pipeline is not skipped ('when' does not work):
          {code:java}
           pipeline {
              agent none
              stages {
                  stage('This one is not skipped :(') {
                      when {
                          expression { false }
                      }
                      agent any
                      steps {
                          echo "this should be skipped, but it won't"
                      }
                  }
              }
          }
          {code}
          Log:
          {panel}
          [Pipeline] stage
           [Pipeline] \{ (This one is not skipped :()
           [Pipeline] node
           Running on iOS-xc in /Users/jenkins/slave/workspace/Pipeline_when
           [Pipeline] \{
           Stage 'This one is not skipped :(' skipped due to when conditional
           [Pipeline] }
           [Pipeline] // node
           [Pipeline] }
           [Pipeline] // stage
           [Pipeline] End of Pipeline
          {panel}
          Simple pipeline with single agent works fine:
          {code:java}
           pipeline {
              agent any
              stages {
                  stage('This on is skipped') {
                      when {
                          expression { false }
                      }
                      steps {
                          echo "this should be skipped and it will"
                      }
                  }
              }
          }
          {code}
          Log:
          {panel}
          [Pipeline] node
           Running on iOS-xc in /Users/jenkins/slave/workspace/Pipeline_when
           [Pipeline] \{
           [Pipeline] stage
           [Pipeline] \{ (This on is skipped)
           Stage 'This on is skipped' skipped due to when conditional
           [Pipeline] }
           [Pipeline] // stage
           [Pipeline] }
           [Pipeline] // node
           [Pipeline] End of Pipeline
          {panel}
          Alexander Vorobiev made changes -
          Resolution New: Not A Defect [ 7 ]
          Status Original: Open [ 1 ] New: Closed [ 6 ]
          Alexander Vorobiev made changes -
          Summary Original: 'when' does not skip stage with dedicated agent New: Sharing of declarative pipeline between similar projects
          Alexander Vorobiev made changes -
          Description Original: Stage with dedicated agent in this pipeline is not skipped ('when' does not work):
          {code:java}
           pipeline {
              agent none
              stages {
                  stage('This one is not skipped :(') {
                      when {
                          expression { false }
                      }
                      agent any
                      steps {
                          echo "this should be skipped, but it won't"
                      }
                  }
              }
          }
          {code}
          Log:
          {panel}
          [Pipeline] stage
           [Pipeline] \{ (This one is not skipped :()
           [Pipeline] node
           Running on iOS-xc in /Users/jenkins/slave/workspace/Pipeline_when
           [Pipeline] \{
           Stage 'This one is not skipped :(' skipped due to when conditional
           [Pipeline] }
           [Pipeline] // node
           [Pipeline] }
           [Pipeline] // stage
           [Pipeline] End of Pipeline
          {panel}
          Simple pipeline with single agent works fine:
          {code:java}
           pipeline {
              agent any
              stages {
                  stage('This on is skipped') {
                      when {
                          expression { false }
                      }
                      steps {
                          echo "this should be skipped and it will"
                      }
                  }
              }
          }
          {code}
          Log:
          {panel}
          [Pipeline] node
           Running on iOS-xc in /Users/jenkins/slave/workspace/Pipeline_when
           [Pipeline] \{
           [Pipeline] stage
           [Pipeline] \{ (This on is skipped)
           Stage 'This on is skipped' skipped due to when conditional
           [Pipeline] }
           [Pipeline] // stage
           [Pipeline] }
           [Pipeline] // node
           [Pipeline] End of Pipeline
          {panel}
          New: There are multiple projects with similar pipelines.

          I can reuse declarative pipeline right now by importing library with pipeline defined:
          Jenkinsfile:
          {code:java}
          @Library('github.com/my/libs@master') _

          common_jenkinsfile {
            // project parameters
          }
          {code}

          github.com/my/libs/common_jenkinsfile.groovy:
          {code:java}
          def call(body) {
              def config = [:]
              body.resolveStrategy = Closure.DELEGATE_FIRST
              body.delegate = config
              body()

              pipeline {
                  agent any
                  stages {
                      stage('This one should be skipped') {
                          when {
                              expression { false }
                          }
                          steps {
                              echo "this should be skipped, but it does not ("
                          }
                      }
                  }
              }
          }
          {code}

          The problems are:
          - This is not officially supported, so can break with any next Jenkins release
          - 'when' does not work in this case, so stages are never skipped
          Alexander Vorobiev made changes -
          Resolution Original: Not A Defect [ 7 ]
          Status Original: Closed [ 6 ] New: Reopened [ 4 ]
          Alexander Vorobiev made changes -
          Component/s New: pipeline-model-definition-plugin [ 21706 ]
          Issue Type Original: Bug [ 1 ] New: Story [ 10002 ]
          Priority Original: Minor [ 4 ] New: Major [ 3 ]
          Alexander Vorobiev made changes -
          Link New: This issue is related to JENKINS-42224 [ JENKINS-42224 ]
          Andrew Bayer made changes -
          Link New: This issue duplicates JENKINS-42224 [ JENKINS-42224 ]
          Andrew Bayer made changes -
          Resolution New: Duplicate [ 3 ]
          Status Original: Reopened [ 4 ] New: Resolved [ 5 ]

            Unassigned Unassigned
            vorobievalex Alexander Vorobiev
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: