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

John Ament Aug 10, 2013

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
Michael Heemskerk Atlassian Team Sep 13, 2013

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.

Chris Ferry Dec 01, 2014

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
Charlie Johnston Oct 03, 2017

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
James Scharett Oct 05, 2017

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

2 votes
Tomas Bjerre Apr 03, 2015

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

1 vote
Tomas Bjerre Feb 19, 2015

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/

Chris Ferry Feb 23, 2015

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

Ken Yee Aug 18, 2016

how would this work w/ Jenkins pipelines?

0 votes
Charles O'Farrell Aug 12, 2013

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

0 votes
Charles O'Farrell Aug 12, 2013

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.

0 votes
Sean Simonsen Sep 12, 2013

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.

0 votes
Christian Galsterer May 04, 2015

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.

Ken Yee Aug 18, 2016

how would this work w/ Jenkins pipelines?

James Dengel Nov 06, 2017

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

Ken Yee Nov 06, 2017

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

0 votes
Jai Langoju May 06, 2017

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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Bitbucket Pipelines

Building a Bitbucket Pipe as a casual coder

...ipe.sh :  #!/bin/bash source "$(dirname "$0")/common.sh" enable_debug extra_args="" if [[ "${DEBUG}" == "true" ]]; then extra_args="--verbose" fi # mandatory variables R...

1,735 views 1 18
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