3
votes

We have a Git repository in TFS (2017.3) and our builds run on Jenkins.

We want to implement in the Pull Request a build validation so we created a build definition that queue a Jenkins job and in the "Job parameters" we enter BRANCH=$(System.PullRequest.SourceBranch):

enter image description here

The issue is that in the TFS Get Sources step TFS creates a temporarily merged branch to check the PR, but the System.PullRequest.SourceBranch returns the PR source branch (e.g. feature branch) and not the merged branch, so the Jenkins job runs on the source branch and not check the PR.

Can we send to Jenkins the merged branch or we must to do the merge in the Jenkins job?

UPDATE:

I see that the variable Build.SourceBranch returns the merged branch but it looks likes the Jenkins job not know this branch. I checked now in the agent and the Git is in Detached HEAD, so I assume there is no option to get from outside the agent (even if the agent was created a new branch - "merged branch" it's only local). any idea?

1
Just a blind shot, could it be there are spaces in the name of the branch? I'm not sure how the expansion BRANCH=$(...) turns out without quotes in that case. - David
Hi David, there is no spaces on the branch :( - Shayki Abramczyk

1 Answers

4
votes

I assume you use Jenkins TFS plugin to trigger a Jenkins job.

In that case according to plugin official documentation (section 'Git') you need to add +refs/heads/*:refs/remotes/origin/* +refs/pull/*:refs/remotes/origin-pull/* to Refspec setting in Git Checkout step.

TFS stores pull request merge commits in temporary branches named pull/<PR number>/head. By adding Refspec you'll allow Jenkins to fetch these merge commit results. Works for me.