<p>This is the way to go:</p>

<pre><code>stage('Deploy'){
  when {
    expression {env.GIT_BRANCH == 'origin/master'}
  }
  steps {
    ....
   }
}
</code></pre>

<p>Take care, this is only working with the declarative syntax.</p>

<p>The environment step is used to "set up the environment" meaning this is the place to declare environmental variables. </p>

<p>Also, in my case I did not declare the GIT_BRANCH var myself. But jenkins provides it as an environmental (therefore "env.GIT_BRANCH") variable if you did "checkout scm" to checkout your repository. If you want to check all the available env vars, do this as a step: <code>sh 'printenv'</code></p>

<p>check out this article for more info: <a href="https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/" rel="nofollow noreferrer">https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/</a></p>

CC BY-SA 4.0
2019-01-04T12:33:11.747Z
2019-01-04T12:41:04.920Z
2019-01-04T12:41:04.920Z
alex
<p>I want to use <code>when</code> on <code>branch</code> condition. The Jenkinsfile is like this</p>

<pre><code> stages{
        stage('Checkout project branch master'){
            steps{             
                withCredentials([sshUserPrivateKey(credentialsId: 'credential', keyFileVariable: 'CREDENTIAL')]) {}
                git branch: 'develop', credentialsId: 'credential', url: 'git@bitbucket.org:user/demopipelinejenkinsfile.git'
            }
        }   
        stage('develop'){
             steps {
                   echo 'on branch develop`
             }
        }
        stage('any_branch'){                  
             environment {
                   GIT_BRANCH = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
                   }
             steps{
                   echo "${env.GIT_BRANCH}"
                  }
        }
 }
</code></pre>

<p>The console output shows that the stage <code>develop</code> is skipped due to condition. However, the last stage still echoes the current branch is "develop"</p>

<p><strong>Question</strong>
How to use <code>when</code> with <code>branch</code> in <strong>Pipeline</strong> project (not <strong>Multibranch</strong> project)</p>

CC BY-SA 4.0
2018-10-31T16:27:09.300Z
Can you provide the exact output of your error when running the pipeline?
Preston Martin
CC BY-SA 4.0
2018-10-31T16:27:59.663Z
Also, I'm assuming you are using declarative pipelines, is that correct?
Preston Martin
CC BY-SA 4.0
2018-11-04T00:18:29.040Z
Your jenkinsfile stage `develop` doesn't have a when condition as seen above ? Are you able to provide the when condition you are using ? You can use something like `when { branch 'develop' }`
ben5556
<jenkins><jenkins-pipeline><jenkinsfile>
Jenkins Pipeline project can't when on branch
Tran Triet
# Jenkins Pipeline project can't when on branch

(Asked by: username_0 on 2018-10-31)

I want to use when on branch condition. The Jenkinsfile is like this
 stages{
        stage('Checkout project branch master'){
            steps{             
                withCredentials([sshUserPrivateKey(credentialsId: 'credential', keyFileVariable: 'CREDENTIAL')]) {}
                git branch: 'develop', credentialsId: 'credential', url: 'git@bitbucket.org:user/demopipelinejenkinsfile.git'
            }
        }   
        stage('develop'){
             steps {
                   echo 'on branch develop`
             }
        }
        stage('any_branch'){                  
             environment {
                   GIT_BRANCH = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
                   }
             steps{
                   echo "${env.GIT_BRANCH}"
                  }
        }
 }

The console output shows that the stage develop is skipped due to condition. However, the last stage still echoes the current branch is "develop"
Question
How to use when with branch in Pipeline project (not Multibranch project)

**Tags:** jenkins, jenkins-pipeline, jenkinsfile

> **Comments:**
> **username_1**: Can you provide the exact output of your error when running the pipeline?  
> **username_1**: Also, I'm assuming you are using declarative pipelines, is that correct?  
> **username_2**: Your jenkinsfile stage `develop` doesn't have a when condition as seen above ? Are you able to provide the when condition you are using ? You can use something like `when { branch 'develop' }`  

---

## 1 answers

### Answer by username_3 on 2019-01-04. Score: 4

This is the way to go:
stage('Deploy'){
  when {
    expression {env.GIT_BRANCH == 'origin/master'}
  }
  steps {
    ....
   }
}

Take care, this is only working with the declarative syntax.
The environment step is used to "set up the environment" meaning this is the place to declare environmental variables. 
Also, in my case I did not declare the GIT_BRANCH var myself. But jenkins provides it as an environmental (therefore "env.GIT_BRANCH") variable if you did "checkout scm" to checkout your repository. If you want to check all the available env vars, do this as a step: sh 'printenv'
check out this article for more info: https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/


---

