I have set up Jenkins and I am using the Jenkins workflow multibranch plugin. I have configured it to listen for commits in a GitHub repo for any branch.
This is a sample of the Jenkinsfile
committed in one of the branches:
node {
// Mark the code checkout 'stage'....
stage 'Checkout'
// Get some code from a GitHub repository
git url: 'git@github.com:Me/my-repo.git', credentialsId: '###'
// Get the maven tool.
// ** NOTE: This 'M3' maven tool must be configured
// ** in the global configuration.
def mvnHome = tool 'M3'
stage 'Build'
sh "${mvnHome}/bin/mvn clean install"
}
When I commit something in this branch a build is triggered. The problem is that git checkouts the master branch and not the branch that is building at the moment.
How can I checkout the branch for the current build?
The solutions so far
- Use
checkout scm
instead ofgit url: 'git@github.com:Me/my-repo.git', credentialsId: '###'
This way you will checkout the current branch. - Use the branch variable - check this for more info: Jenkins Multibranch pipeline: What is the branch name variable?