<p>It sounds like you want a <a href="https://www.jenkins.io/doc/book/pipeline/syntax/#when" rel="nofollow noreferrer"><code>when</code></a> statement.</p>
<p>As I understand it, you want to execute stage B <strong>when</strong> you are on branch <code>update/jenkinsfile</code>. In order to do this, you need to add a condition to stage B:</p>
<pre class="lang-java prettyprint-override"><code>pipeline {
    agent any
    stages {
        stage('A') {
            steps {
                sh 'echo &quot;Step A&quot;'
            }
        }
        stage('B') {
            when {
              branch 'update/jenkinsfile'
            }
            steps {
                sh 'echo &quot;Step B&quot;'
            }
        }
    }
}

</code></pre>
<p>Note that you also need your jenkins to discover this new branch - this will depend on how you have configured the job. The above assumes that you have a multibranch pipeline job which is configured to include that branch.</p>

CC BY-SA 4.0
2021-03-26T08:27:20.277Z
Yeah this definitely works but I want a solution for incrementally updating the Jenkinsfile. For ex - I want to add a new step on the pipeline but before merging the new Jenkinsfile to the master I want to test it if the new stage{} is working fine. Currently, Jenkinsfile is being picked up from the master branch and I'm not able to test my change because that stage{} is not available in the master.
Oli
CC BY-SA 4.0
2021-03-26T08:56:48.097Z
You need to define the job to discover pipelines on other branches,a s I mentioned in the answer. A common approach is to discover pull requests and use a `{ when {branch 'PR-*}}` condition.
Bruce Becker
CC BY-SA 4.0
2021-03-26T09:30:35.623Z
Got it. Thank you :)
Oli
CC BY-SA 4.0
2021-03-26T08:00:29.373Z
2021-03-26T08:00:29.373Z
Bruce Becker
<p>I've a Jenkinsfile in the <code>master</code> branch of the repository with following code</p>
<pre><code>pipeline {
    agent any
    stages {
        stage('A') {
            steps {
                sh 'echo &quot;Step A&quot;'
            }
        }
    }
}
</code></pre>
<p>Now I want to add a new stage B for which I created a git branch <code>update/jenkinsfile</code> and in that branch the Jenkinsfile is now</p>
<pre><code>pipeline {
    agent any
    stages {
        stage('A') {
            steps {
                sh 'echo &quot;Step A&quot;'
            }
        }
        stage('B') {
            steps {
                sh 'echo &quot;Step B&quot;'
            }
        }
    }
}
</code></pre>
<p>How do I make Jenkins to use Jenkinsfile from branch B not from master in order to testing the change?</p>

<jenkins><jenkins-pipeline><jenkinsfile>
Jenkinsfile update using version control
Oli
# Jenkinsfile update using version control

(Asked by: username_0 on 2021-03-26)

I've a Jenkinsfile in the master branch of the repository with following code
pipeline {
    agent any
    stages {
        stage('A') {
            steps {
                sh 'echo "Step A"'
            }
        }
    }
}

Now I want to add a new stage B for which I created a git branch update/jenkinsfile and in that branch the Jenkinsfile is now
pipeline {
    agent any
    stages {
        stage('A') {
            steps {
                sh 'echo "Step A"'
            }
        }
        stage('B') {
            steps {
                sh 'echo "Step B"'
            }
        }
    }
}

How do I make Jenkins to use Jenkinsfile from branch B not from master in order to testing the change?

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


---

## 1 answers

### Answer by username_1 on 2021-03-26. Score: 1

It sounds like you want a when statement.
As I understand it, you want to execute stage B when you are on branch update/jenkinsfile. In order to do this, you need to add a condition to stage B:
pipeline {
    agent any
    stages {
        stage('A') {
            steps {
                sh 'echo "Step A"'
            }
        }
        stage('B') {
            when {
              branch 'update/jenkinsfile'
            }
            steps {
                sh 'echo "Step B"'
            }
        }
    }
}


Note that you also need your jenkins to discover this new branch - this will depend on how you have configured the job. The above assumes that you have a multibranch pipeline job which is configured to include that branch.

> **Comments:**
> **username_0**: Yeah this definitely works but I want a solution for incrementally updating the Jenkinsfile. For ex - I want to add a new step on the pipeline but before merging the new Jenkinsfile to the master I want to test it if the new stage{} is working fine. Currently, Jenkinsfile is being picked up from the master branch and I'm not able to test my change because that stage{} is not available in the master.  
> **username_1**: You need to define the job to discover pipelines on other branches,a s I mentioned in the answer. A common approach is to discover pull requests and use a `{ when {branch 'PR-*}}` condition.  
> **username_0**: Got it. Thank you :)  

---

