Creating a build against a pull request

We're trying out Stash in a POC mode. One of the ideal behaviors for us would be if Stash could trigger a build when a pull request comes in. This way we know the build is good.

So what I've done is setup stash notifier in Jenkins, and told Jenkins to look at the branches refs/pull-requests/*/merge (which should have the merge result, if I understand correctly). Yes I understand this is internal use only, and I understand that it cannot be modfied (I have no inteion of modifying it). I've also setup the Jenkins webhook. When I send the pull request, i don't think the webhook is triggering. Maybe it's not intended to, I'm not sure.

So, is there an alternate solution to this?

8 answers

2 votes

Unfortunately, building from the pull request refs is not as straightforward as it may seem.

When you view a pull request in Stash, Stash shows the diff between the 'from' branch of the pull request and the merge of the 'from' branch and the 'to' branch. This merge needs to be recalculated every time someone pushes to either the 'from' or 'to' branch.

For performance reasons, these calculations are done asynchronously on background threads, which means that the refs under /refs/pull-requests are updated some time after push has completed. Therefore, when the webhook sends a notification to Jenkins, the refs under /refs/pull-requests will not have been updated yet.

If you do want to build the pull requests and you're happy building the 'from' branch of a pull request, you could:

Write a post-receive hook that:

  • loops through the RefChanges
  • for each RefChange look up the open pull requests (using PullRequestService.findInDirection(OUTGOING, repoId, refChange.getRefId(), OPEN, NEWEST, PageUtils.newRequest(0, 1))
  • if there are any results for the call above (which means that there is an open pull request with that branch as the 'from' branch), send the build trigger with the refChange.getToHash() as the custom TAG.

Note that such a hook only builds a pull request if the hook is enabled in the 'from' repository for that pull request. So say you have a pull request from a personal fork to the main repository, that pull request would only be built if you enable the hook in your personal fork as well.

So can we get all that build into stash as a feature. Seems like a HUGE deal breaker for most people who do continuous integration. My engineers are shocked that we can't get this working. It's out of the box with github.

Is this still the case where Pull Requests don't work from forked repos? Is there any way besides polling to get this to work?

It literally makes 0 sense to create a Jenkins job for a fork. The public repo's job should have all the builds in it, not only does this come out of the box with GitHub like @Chris Ferry said, it comes seamlessly out of the box with every other product besides BitBucket. Very frustrating.

I agree as well.  I've just started digging into this and am shocked that its not a feature.  

I solved it with 2 jobs in Jenkins. One that polls Stash REST API and one parameterized build that does the actual verification. The polling job will trigger the build job with any new pull request. The build job uses Stash Notifier Plugin to report back to Stash.

I wrote a post about it here: http://bjurr.se/building-atlassian-stash-pull-requests-in-jenkins/

Great write-up. Any chance you could go into more detail by providing code examples? For example the groovy code. Thanks Chris

how would this work w/ Jenkins pipelines?

There is a plugin in Atlassian Marketplace now that you may want to have a look at:
https://marketplace.atlassian.com/plugins/se.bjurr.prnfs.pull-request-notifier-for-stash

You can have it trigger on a pull request being opened, re-opened or updated. And then invoke Jenkins like:

http://server/job/pullRequestBuilderJob/buildWithParameters?token=T&FROM_REPO=${PULL_REQUEST_FROM_REPO_NAME}&TO_REPO=${PULL_REQUEST_TO_REPO_NAME}&FROM_HASH=${PULL_REQUEST_FROM_HASH}&TO_HASH=${PULL_REQUEST_TO_HASH}

To trigger a build.

Then you can use Stash Notifier plugin to report back to Stash. Use the variable $PULL_REQUEST_FROM_HASH in configuration for Stash Notifier plugin to get it nicely reported in the pull request view =)

Hi John,

Do you mind me asking specifically what your configuration for the Jenkins job is? Do you have a screenshot of the configuration page? I'm interested in the refspec and branches field in particular.

Charles

Alternatively, what you can do is have a job/plan that builds on every branch that matches a given pattern. Every time a developer pushes a branch to, say, feature/abc or bugfix/xyz the build will trigger. Quite often these branches will correspond to a pull request anyway.

Hi Charles,

I didn't ask the question, but I am trying to do the same thing. My refspec is

+refs/pull-requests/*:refs/remotes/origin/pr/*

and my branch is the commit's hash (grabbed from a build parameter that I named TAG, so the exact entry I have in branch is $TAG). I'm successfully able to build a pull request by making an HTTP call to http://myjenkinsserver/job/my_job/buildWithParameters?token=TOKEN_NAME&TAG=a4745b5.

I just don't know how to get Stash to trigger Jenkins on a pull request, as John asked. I'm about to write my own hook that hits the URL above.

You may have a look at this blog post where I describe the detailed steps how to setup Jenkins and Stash to trigger automatically the correct Jenkins build(s) when a pull request is created or updated.

It uses the Jenkins Git Plugin and the Pull Request Notifier for Stash add-on and selects the correct build using the notification feature of the Jenkins Git Plugin.

how would this work w/ Jenkins pipelines?

Hey Ken using information from Christian and Thomas, I have done a write up of how to achieve this.

https://dengelonsoftware.blogspot.co.uk/2017/01/jenkins-pull-request-and-pipeline.html

Thanks...that looks like a helpful post :-)

I have a very specific issues with Pull Request builds. I am using TeamCity and Bitbucket server and am able to get builds working fine. But problem is if I am building the 'from' branch (refs/pull-requests/*/from) the status is showing up on the PR page on Bitbucket. if I build the 'merge' branch (refs/pull-requests/*/merge), nothing shows up on the PR page.

Any advice?

Suggest an answer

Log in or Join to answer
Community showcase
Piotr Plewa
Published Dec 27, 2017 in Bitbucket

Recipe: Deploying AWS Lambda functions with Bitbucket Pipelines

Bitbucket Pipelines helps me manage and automate a number of serverless deployments to AWS Lambda and this is how I do it. I'm building Node.js Lambda functions using node-lambda&nbsp...

696 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot