I want to kick a build with Jenkins when a given PR is merged to master/develop branches.
Is this possible in BitBucket?
What I try is to create a build with the following trigger "Build when a change is pushed to BitBucket", but after I merge my PR nothing happens.
Yes, it is possible in BitBucket. But you will have to give PR parameters in Jenkins.
Advanced -> Refspec:
+refs/pull-requests/*:refs/remotes/origin/pr/*
Branch Specifier:
origin/pr/${pullRequestId}/from
And you would need Stash PullRequest Builder Plugin in Jenkins. There you have to pass your BitBucket credentials.
Related
These are the only options I have with the bitbucket plugin:
I want "exclude branches that are also filed as PRs", but when I select that jenkins actually removes the branch. I want to keep both the branch job and the PR job but only build the PR job. So I want the PR job to be built on commit, but also make it possible to manually run the branch job.
Is this possible without me having to add logic into my pipelines?
We have this, with "Discover branches" on "All branches" but "Automatic branch project triggering" > "Branch names to build automatically" on "PR-\d+".
I am pretty new to Jenkins, and when I make a pull request, the Github webhook triggers a rebuild on all branches in the repo. However, I only want the branch associated with the pull request to be built. I suspect that all branches are built due to nothing specified in the "Branch specifier."
Is there a way to specify to build only the branch the pull request was made to?
Thanks!
To resolve your issue try to use multibranch pipeline job or use filter in simple pipeline job.
To use multibranch pipeline job you need to install Pipeline: Multibranch plugin.
Pipeline job example
In simple pipeline job choose "Build when a change is pushed to GitLab" in "Build trigger" section -> Advanced -> Filter branches by name
I am not sure if I am asking the question in right way.
When I am triggering the multi branch pipeline job and I do "checkout scm" in Jenkinsfile, does it checkout the pull request or Master + pull request?
It depends of your settings.
As I remember, multibranch pipeline builds pull requests in separated view and it can build as PR source branch and result of PR. It cloned your repo, checkouts target branch, merges source branch into it and runs your stages.
If you look at the logs you will see that it checks out the hash of the commit
example: git checkout 1fb651dfdcbea276627de0247bbf5047840cae4c
As for workspace, if you log in to the Jenkins machine where this is running you will see that each branch has a separated workspace. Here you will find the source code of the checked out branch. The separated workspace ensures that there is no conflict when working with multiple concurrent branch builds.
I installed the bitbucket plugin in Jenkins, and made the following changes:
I added the webhook on the bitbucket repo in the following format:
http://JENKINS.SERVER:PORT/bitbucket-hook
Under build trigger enabled Build when a change is pushed to BitBucket
Under Source Code Management selected GIT; I defined which branch to build: foobar
So my question is, I want the build to be triggered only when there is a commit on that specific branch foobar!
How do I do that?
Do I need to specify anything else on the bitbucket side?
Tnx,
Tom
In Job Configuration -> Source Code Management -> Branches to build
Specify which branch you want to trigger for build
In Jenkins what this both will do if I mention Branch Specifier as
1 origin/pr/${pullRequestId}/from
2 origin/pr/${pullRequestId}/merge
Does this means that in first case build will be created from what ever code will be there in the pull request. and in second case, changes in PR will be applied as a patch to the target branch locally,inside Jenkins workspace and build will be trigged on the same.
Can you confirm you are using Atlassian Stash to manage your Git repo?
When you create the PR, Stash try a "lazy" merge (in the /merge refs ID). If the target branch is moving, Stash will attempt new merges.
Some explanation about this lazy merge here.
If you trigger a Jenkins build with a Stash hook, Stash will send all the PR information to Jenkins (pullRequestId, from SHA1, merge SHA1, ...).
So with your Jenkins job, you can try a merge (origin/pr/${pullRequestId}/from to origin/master, if master is your target branch) and you can build the merge result.
This what we are doing in my company with these settings and the Stash pullrequest builder plugin:
It's working well :)
Every time a developer updates the PR, a new PR build is triggered and Jenkins tries to do a new merge.
Once the PR is validated, if the developer click on the Merge button, it'll try to merge the code on the target branch.
You can set some merge options in this properties file.