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

when {branch} stage conditional is busted (mostly)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Minor Minor
    • None
    • Jenkins: 2.40 (docker)
      Pipeline: Model Definition: 0.8.1
      SCM API Plugin: 2.0.1-beta-2
      pipeline: Multibranch: 2.10-beta-1

      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
      

            abayer Andrew Bayer
            smazurov Stepan Mazurov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: