mirosval
mirosval

Reputation: 6822

Jenkins Pipeline: How do I fetch (not pull) a different branch?

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

Upvotes: 6

Views: 3274

Answers (1)

mirosval
mirosval

Reputation: 6822

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.

Upvotes: 4

Related Questions