I'm trying to use Jenkins Pipelines and I would like to diff the current branch of the job to master
. For that I need to have master
checked out. Jenkins only checks out the branch I'm building.
I tried:
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'GITHUB', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
sh 'git fetch origin master:master'
}
but that only binds the credentials to the ENV variables and git doesn't pick that up.
All the other approaches I've seen use checkout, but that would change my current branch. I only want to fetch the master so I can do a git diff
later
@swoop81 answer is working but if you just want to checkout one branch, you could fetch only this one.
Head over to your Jenkins instance and create a new item. Enter a name for the job, and select the “Multibranch Pipeline” option at the end of the screen. Then, click on the OK button. In the next screen, go to the “Branch sources” tab, click on the “Add source” button, and choose “Git” from the dropdown menu.
Git already only pulls the current branch. If you have branch set up as a tracking branch, you do not need to specify the remote branch. git branch --set-upstream localbranch reponame/remotebranch will set up the tracking relationship. You then issue git pull [--rebase] and only that branch will be updated.
I found a workaround:
git branch: 'master', credentialsId: 'GITHUB', url: env.GIT_URL
git branch: env.BRANCH_NAME, credentialsId: 'GITHUB', url: env.GIT_URL
First I check out the master branch and then check out my branch again. This is not ideal, since its changing the working tree instead of just fetching the master branch, but it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With