Jenkins pipeline if else not working : <p>I am creating a sample jenkins pipeline, here is the code.</p>

<pre><code>pipeline {
    agent any 

    stages {    
        stage('test') { 
            steps { 
                sh 'echo hello'
            }            
        }
        stage('test1') { 
            steps { 
                sh 'echo $TEST'
            }            
        }
        stage('test3') {
            if (env.BRANCH_NAME == 'master') {
                echo 'I only execute on the master branch'
            } else {
                echo 'I execute elsewhere'
            }                        
        }        
    }
}
</code></pre>

<p>this pipeline fails with following error logs</p>

<pre><code>Started by user admin
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 15: Not a valid stage section definition: "if (env.BRANCH_NAME == 'master') {
                echo 'I only execute on the master branch'
            } else {
                echo 'I execute elsewhere'
            }". Some extra configuration is required. @ line 15, column 9.
           stage('test3') {
           ^

WorkflowScript: 15: Nothing to execute within stage "test3" @ line 15, column 9.
           stage('test3') {
           ^
</code></pre>

<p>But when i execute the following example <a href="https://jenkins.io/doc/book/pipeline/syntax/#declarative-steps" rel="noreferrer">from this url</a>, it executes successfully and print the else part.</p>

<pre><code>node {
    stage('Example') {
        if (env.BRANCH_NAME == 'master') {
            echo 'I only execute on the master branch'
        } else {
            echo 'I execute elsewhere'
        }
    }
}
</code></pre>

<p>The only difference i can see is that in the working example there is no <code>stages</code> but in my case it has.</p>

<p>What is wrong here, can anyone please suggest?</p>
