1

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?

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.

3
  • 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
    Mar 26 at 8:27
  • 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. Mar 26 at 8:56
  • Got it. Thank you :)
    – Oli
    Mar 26 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.