What does PR merge means? - jenkins

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.

Related

BitBucket merge check to allow merge PR when ONLY LAST build is successful

We have a repo in BitBucket Server with "Minimum successful builds" merge check enabled and set to 1. When PR is opened the build starts and result of that build returns back to BitBucket.
Sometimes build fails (out of memory, etc) we run build again manually (without new commits). In that case PR contains two builds: first is "Failed" and the second is "Passed", but "Minimum successful builds" merge check still can't allow us to merge because it require to all builds be successful.
It is possible to check only last build be successful to allow PR merge?
So, the only solution I've found so far is changing the build name which Jenkins sends to Bitbucket. The main idea is that the build name must always be the same for that particular Pull Request. In that case, PR build name on Bitbucket is overridden each time when a new build of the PR starts by Jenkins.
In our case, I just removed Jenkins's BUILD_NUMBER from the build name, which it sent to Bitbucket.
The only drawback of that solution I've found is that you can only see last build status of the PR without any build history.

Is there a way to have Jenkins only build the PR and not the corresponding branch but also not delete the branch job?

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+".

How can I get the name of the branch that was merged into master in Multibranch pipeline

I have a multibranch pipeline job.
When the pipeline runs for the master branch I want to:
Check if this scm push was a merge
Get the name of the branch that was merged
This way when the master build runs I can see if and which branch branch was merged into it and then do stuff with that info.
It would be nice if this is a built in jenkins feature or if this info can just be read from the GitSCM class
There is no straight way I guess. One workaround would be to use commands directly to fetch the details.
To find if the push is a merge commit, execute the following command:
git log --pretty=%P -n 1 "{commit id}"
It will give the parent commits for the given commit id. If there are two parents, then it is a merge commit.
To find the branch name of the merged commit:
git branch --contains "{commit id}"
This will return the branch names.

how do I create Master + PR branch and run some tests on it?

I am new to jenkins.
I want to build pipeline/jenkins job where once developer has pull request approved by other developers with 1 LGTM, I would build a new Master + the PR as new Master and run smoke tests.
If it passes, I will merge to the Master.
Thanks in advance!
You would need to setup a multibranch pipeline on a dedicate repo.
That dedicated repo would be one where (master+PR) branches are pushed to: their new branch would trigger the multibranch pipeline.
In order to push such branches (master+PR), you need another job which will:
periodically fetch from the origina repo
check for any PR branch with a +1 LGTM
create a new branch from master and merge that PR branch
push the resulting new branch to the dedicated remote repo mentioned before.

Triggering a Jenkins build on merge from BitBucket

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.

Resources