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

when {branch} stage conditional is busted (mostly)

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Minor
    • Resolution: Fixed
    • Labels:
      None
    • Environment:
      Jenkins: 2.40 (docker)
      Pipeline: Model Definition: 0.8.1
      SCM API Plugin: 2.0.1-beta-2
      pipeline: Multibranch: 2.10-beta-1
    • Similar Issues:

      Description

      Just installed latest pipeline-model-definition plugin (0.8.1) and was trying to set up a when branch stage condition.

      In short, only way to make branch matching work is via expression, evaluating env.BRANCH_NAME directly:

      when {
        expression {
          return env.BRANCH_NAME == "feature/docker"
        }
      }
      

      This is in a branch feature/docker job of a multi-branch pipeline

      I tried other permutation of the conditional, and it did not work, the stage would get skipped (steps were not executed):

      when {
        branch "feature/docker"
      }
      
      when {
        environment name: "BRANCH_NAME", value: "feature/docker"
      }
      

      My test pipeline is basically:

      pipeline {
          agent {
              label 'docker'
          }
          
          stages {
              stage('test condition: expression') {
                  when {
                      expression {
                          return env.BRANCH_NAME == "feature/docker"
                      }
                  }
                  steps {
                      sh 'echo "expression"'
                  }
              }
              
              stage('test when: env') {
                  when {
                      environment name: "BRANCH_NAME", value: "feature/docker"
                  }
                  steps {
                      sh 'echo "env"'
                  }
              }
              
              stage('test when: branch') {
                  when {
                      branch "feature/docker"
                  }
                  steps {
                      sh 'echo "branch"'
                  }
              }
          }
      }
      

      Console output:

      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] stage
      [Pipeline] { (test condition: expression)
      [Pipeline] sh
      [myproject_feature_docker-4Z7CPEYEM3PKEYK35E3XFJRVUNLHOV4KEKFEQICOUFRC35RQM2OA] Running shell script
      + echo expression
      expression
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] stage
      [Pipeline] { (test when: env)
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] stage
      [Pipeline] { (test when: branch)
      [Pipeline] }
      [Pipeline] // stage
      

        Attachments

          Issue Links

            Activity

            Hide
            abayer Andrew Bayer added a comment -

            Bobby, could you take a look?

            Show
            abayer Andrew Bayer added a comment - Bobby, could you take a look?
            Hide
            abayer Andrew Bayer added a comment -

            Check that, I got it. getContextVariables() basically did nothing useful - it was always getting a null step and so not giving us a context to work with. I've got a branch that'll be up for review momentarily that changes the branch and env conditions to get passed the value to check against rather than trying to find it for themselves. Also we only had negative tests for branch and env, which is...embarrassing.

            Show
            abayer Andrew Bayer added a comment - Check that, I got it. getContextVariables() basically did nothing useful - it was always getting a null step and so not giving us a context to work with. I've got a branch that'll be up for review momentarily that changes the branch and env conditions to get passed the value to check against rather than trying to find it for themselves. Also we only had negative tests for branch and env, which is...embarrassing.
            Hide
            abayer Andrew Bayer added a comment -

            PR up at https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/91 - should do the trick. Sorry about this!

            Show
            abayer Andrew Bayer added a comment - PR up at https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/91 - should do the trick. Sorry about this!
            Hide
            scm_issue_link SCM/JIRA link daemon added a comment -

            Code changed in jenkins
            User: Andrew Bayer
            Path:
            pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/BranchConditional.java
            pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/EnvironmentConditional.java
            pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/BranchConditionalScript.groovy
            pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/EnvironmentConditionalScript.groovy
            pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/AbstractModelDefTest.java
            pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/BasicModelDefTest.java
            pipeline-model-definition/src/test/resources/json/whenBranch.json
            pipeline-model-definition/src/test/resources/json/whenBranchFalse.json
            pipeline-model-definition/src/test/resources/json/whenEnv.json
            pipeline-model-definition/src/test/resources/json/whenEnvFalse.json
            pipeline-model-definition/src/test/resources/whenBranch.groovy
            pipeline-model-definition/src/test/resources/whenBranchFalse.groovy
            pipeline-model-definition/src/test/resources/whenBranchTrue.groovy
            pipeline-model-definition/src/test/resources/whenEnv.groovy
            pipeline-model-definition/src/test/resources/whenEnvFalse.groovy
            pipeline-model-definition/src/test/resources/whenEnvTrue.groovy
            http://jenkins-ci.org/commit/pipeline-model-definition-plugin/4af77f00e32e2e6c459c15564ca84b4c53a6332c
            Log:
            [FIXED JENKINS-41012] Actually get when branch/env to work

            Stop relying on the step context, since that wasn't actually getting
            provided, and instead pass in the needed arguments from the *Script
            class. Also make sure we actually have tests for all the conditions
            passing, not just failing. Oy, but I feel silly there.

            Show
            scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Andrew Bayer Path: pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/BranchConditional.java pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/EnvironmentConditional.java pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/BranchConditionalScript.groovy pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/EnvironmentConditionalScript.groovy pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/AbstractModelDefTest.java pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/BasicModelDefTest.java pipeline-model-definition/src/test/resources/json/whenBranch.json pipeline-model-definition/src/test/resources/json/whenBranchFalse.json pipeline-model-definition/src/test/resources/json/whenEnv.json pipeline-model-definition/src/test/resources/json/whenEnvFalse.json pipeline-model-definition/src/test/resources/whenBranch.groovy pipeline-model-definition/src/test/resources/whenBranchFalse.groovy pipeline-model-definition/src/test/resources/whenBranchTrue.groovy pipeline-model-definition/src/test/resources/whenEnv.groovy pipeline-model-definition/src/test/resources/whenEnvFalse.groovy pipeline-model-definition/src/test/resources/whenEnvTrue.groovy http://jenkins-ci.org/commit/pipeline-model-definition-plugin/4af77f00e32e2e6c459c15564ca84b4c53a6332c Log: [FIXED JENKINS-41012] Actually get when branch/env to work Stop relying on the step context, since that wasn't actually getting provided, and instead pass in the needed arguments from the *Script class. Also make sure we actually have tests for all the conditions passing , not just failing. Oy, but I feel silly there.
            Hide
            abayer Andrew Bayer added a comment -

            Pushing out an emergency 0.8.2 release momentarily with this fix. Again, sorry for letting this slip through!

            Show
            abayer Andrew Bayer added a comment - Pushing out an emergency 0.8.2 release momentarily with this fix. Again, sorry for letting this slip through!
            Hide
            smazurov Stepan Mazurov added a comment -

            No worries, this is beta software after all, thanks for taking care of it!

            Show
            smazurov Stepan Mazurov added a comment - No worries, this is beta software after all, thanks for taking care of it!
            Hide
            abayer Andrew Bayer added a comment -

            Stepan Mazurov Nonetheless, I feel sheepish. Shoulda tested better! But the release has been cut and should show up in the update center in a couple hours. Thanks for catching this and supplying such an excellent bug report - the expression comparison was really helpful, since in our test environment, we don't actually get BRANCH_NAME set by the SCM, so if I hadn't had that validation that it does get set in the real world, I'd've gone down some rathole to figure out what could be done. =)

            Show
            abayer Andrew Bayer added a comment - Stepan Mazurov Nonetheless, I feel sheepish. Shoulda tested better! But the release has been cut and should show up in the update center in a couple hours. Thanks for catching this and supplying such an excellent bug report - the expression comparison was really helpful, since in our test environment, we don't actually get BRANCH_NAME set by the SCM, so if I hadn't had that validation that it does get set in the real world, I'd've gone down some rathole to figure out what could be done. =)
            Hide
            smazurov Stepan Mazurov added a comment -

            Glad I could help! Thank you for those words, I appreciate it.

            Show
            smazurov Stepan Mazurov added a comment - Glad I could help! Thank you for those words, I appreciate it.

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                smazurov Stepan Mazurov
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: