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?
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 =)
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:
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I agree as well. I've just started digging into this and am shocked that its not a feature.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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/
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great write-up. Any chance you could go into more detail by providing code examples? For example the groovy code. Thanks Chris
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.