Issue
This seems to be a quite popular question, but all have different variances and I can't get to the bottom of it.
My git repository has 2 projects each in a separate folder. I'd like to trigger Jenkins CI by pull-request and run build for a particular project only if files in the subfolder for that folder changed since the last commit.
I've learned how to trigger Jenkins CI by pull-request (use MultiBranchPipeline Jenkins project type).
I can execute scripts in the CI (JenkinsFile inside of my repo).
Also, I know how to detect if there are any files changed in a particular subfolder in the last commit (git diff HEAD^ HEAD Frontend/).
But I can't figure out how to identify commits in the pull-request so I can check changes in the subfolder for those 2 commits.
Question: How can I know what are commits in the pull-request?
Or the end question:
Question: How to check if files in a folder of the PullRequest changed since the last build?
P.S. I know Jenkins knows this as in logs I see the revision:
Checking out Revision 88f657f0d3c63c43ba3b07686c93a640ca681e00 (PR-4)
How can I access it on the scripts level?
Thanks!
Solution
After googling around which did not lead to anything fruitful I've come up with a workaround: instead of trying to pull information from Jenkins I decided to pull it from Git, luckily it has a nice command, which shows the delta between either branches or commits:
git merge-base HEAD origin/master
The only problem was that for some reason, the repo which Jenkins pulled from Github as part of PR, did not see 'origin/master': it only saw 'origin/PR-xyz'. And even if I did 'get fetch origin master' it did not help.
So I had to add a remote manually. And as the repo is preserved by Jenkins while PR is open, I've added 'if' to ensure that sequential PR runs won't fail because remote already exists:
String remotes = powershell script:'git remote', returnStdout:true
echo 'Remotes: '+remotes
if( !remotes.contains('github') )
{
echo 'Adding github remote'
powershell script:'git remote add github https://github.com/SeredaOM/JenkinsLearning.git'
powershell script:'git fetch github master'
}
P.S. If someone has any better solution please let me know, it would be a good learning material.
Answered By - Budda
Answer Checked By - Senaida (JavaFixing Volunteer)