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

Multibranch Pipeline broken on when expressions on tag

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open (View Workflow)
    • Priority: Major
    • Resolution: Unresolved
    • Component/s: pipeline
    • Labels:
      None
    • Similar Issues:

      Description

      When using 'when' expression, multibranch pipeline does not skip the condition but executes the stage.

      I've 3 stages, Build, Deploy to QA and Prod (which is not show in the snippet. Build has nested stages with when condition. I want to build from any branch or tag starts with release/*. hence I have a when condition in Build Stage (nested).

      Then when it come to deployment, I want Deploy to QA to be executed when 'when' expression matched to the release.* "REGEX". But instead of skipping on any branch, it executes the stage.

      Pipeline: 

      pipeline {
          agent none
          stages {
              stage('Build') {
                  agent {
                      node ('master')
                  }
      
                  stages {
                      stage ('Setup') {
                          agent {
                              node {
                                  label ''
                              }
                          }
                          when {
                              branch '**'
                              tag pattern: "release.*", comparator: "REGEXP"
                          }
                          steps {
                                   //some stuff here
                                  }
                              }
                          }
                      }
                  }
              }
              stage('Deploy to QA') {
                  agent {
                      node {
                          label ''
                      }
                  }
                  when {
                      tag pattern: 'release.*', comparator: "REGEXP"
                  }
                  steps {
                      //some stuff here
                          }
                      }
                  }
              }
          }
      }
      

        Attachments

          Activity

          Hide
          markewaite Mark Waite added a comment -

          You'll encourage more help from others if you follow the instructions at "How to report an issue". When you report an issue to the Jenkins project, you're trying to persuade those who read the issue that they should spend their time to understand your issue and respond to your issue. Posting a large block of unformatted pipeline code and saying

          When using when expression, multibranch pipeline does not skip the condition but executes the stage

          is much less likely to persuade people to help you than if you submit a minimal sample that illustrates your specific concern.

          In this case,I see that you're using two when clauses in the pipeline. One of them uses a single expression. The other uses two expressions, but I believe one of the two expressions (branch: "**"" will always be true. If one of the two expressions is always true, then the result of the when will be the value of the other expression. The conditions for when must all be true for the step to be executed, per the declarative pipeline documentation.

          You'll need to provide more details and a more precise example of the problem before others are likely to assist.

          Show
          markewaite Mark Waite added a comment - You'll encourage more help from others if you follow the instructions at "How to report an issue" . When you report an issue to the Jenkins project, you're trying to persuade those who read the issue that they should spend their time to understand your issue and respond to your issue. Posting a large block of unformatted pipeline code and saying When using when expression, multibranch pipeline does not skip the condition but executes the stage is much less likely to persuade people to help you than if you submit a minimal sample that illustrates your specific concern. In this case,I see that you're using two when clauses in the pipeline. One of them uses a single expression. The other uses two expressions, but I believe one of the two expressions ( branch: "**"" will always be true. If one of the two expressions is always true, then the result of the when will be the value of the other expression. The conditions for when must all be true for the step to be executed, per the declarative pipeline documentation . You'll need to provide more details and a more precise example of the problem before others are likely to assist.
          Hide
          puneet0803 Puneet added a comment -

          Hi Mark Waite,

          Thanks for your prompt response, I apologies on not following the guidelines for issue reporting. I've added few more context w.r.t it. Thanks for the explanation, I'm in an assumption that 'when' expression is only valid till stage executes. If you noticed, I've two stages as shown above "Build & Deploy to QA"( Build has nested stages) was thinking build will have its separate when expression and deploy will have its own.

          All I want to do is build on any branch & tags start with release/ and deploy when only tags start with release/

          Would really appreicate your help on this.

          Show
          puneet0803 Puneet added a comment - Hi Mark Waite , Thanks for your prompt response, I apologies on not following the guidelines for issue reporting. I've added few more context w.r.t it. Thanks for the explanation, I'm in an assumption that 'when' expression is only valid till stage executes. If you noticed, I've two stages as shown above "Build & Deploy to QA"( Build has nested stages) was thinking build will have its separate when expression and deploy will have its own. All I want to do is build on any branch & tags start with release/ and deploy when only tags start with release/ Would really appreicate your help on this.
          Hide
          marc_samendinger Marc Samendinger added a comment -

          This might be related to https://issues.jenkins.io/browse/JENKINS-61099

          In our project I can see the same. Our stage that we want to skip looks like

          stage('Docker build') {
            when {
              tag pattern: '^\\d\\.\\d\\.\\d$', comparator: "REGEXP"
            }
            steps {
              script {
                // build docker image
              }
            }
          }
          

          But the docker image gets build for all tags and branches.

          Interesting is we have two Jenkins instances and on one this works (stage gets skipped for certain tags and branches) and on the other it doesn't. Adding the allOf condition to the when block throws the error mentioned in #61099, but only on the instance where it doesn't work.

          Not sure if this is the reason, but we have two different versions 2.222.1 (works) and 2.263.4 (doesn't) both run on linux amd64.
          the attached files contain our installed plugins

          Show
          marc_samendinger Marc Samendinger added a comment - This might be related to https://issues.jenkins.io/browse/JENKINS-61099 In our project I can see the same. Our stage that we want to skip looks like stage( 'Docker build' ) { when { tag pattern: '^\\d\\.\\d\\.\\d$' , comparator: "REGEXP" } steps { script { // build docker image } } } But the docker image gets build for all tags and branches. Interesting is we have two Jenkins instances and on one this works (stage gets skipped for certain tags and branches) and on the other it doesn't. Adding the allOf condition to the when block throws the error mentioned in #61099, but only on the instance where it doesn't work. Not sure if this is the reason, but we have two different versions 2.222.1 (works) and 2.263.4 (doesn't) both run on linux amd64. the attached files contain our installed plugins

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            puneet0803 Puneet
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated: