It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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.

Like # people like this

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.

Like Brad likes this

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

There is a plugin in Atlassian Marketplace now that you may want to have a look at:

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


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 =)

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:

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?

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.


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


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.

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 Sign up to answer
Community showcase
Published in Bitbucket

Atlassian supported Jenkins integration for Bitbucket Server

We’ve been building a plugin to integrate Bitbucket Server and Jenkins CI, and I’m excited to announce that our alpha is ready to download and install. It lets you seamlessly configure a Jenkins job ...

986 views 1 17
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you