I use a custom trigger that scans for changes in repository and starts the builds when needed. It is a Jenkinsfile with some helper gradle scripts on the side.
The core of functionality that checks the changes is git diff
. It takes GIT_PREVIOUS_COMMIT
and GIT_COMMIT
and compares them. This works perfectly until there is a force push or amended commit.
What happens is that Jenkins checks out the latest commit without knowledge of the commit history.
And of course, git diff fails, because it cannot find the reference to the commit that was amended.
Locally, git diff
between current and amended commit works, because I have all the history. But I wonder if there is a way to do this in the pipeline.
My questions are:
Edit 1 and 2: typo and styling.
Edit 3:
I've found out about currentBuild.changeSets as well, but it doesn't
work for my use case. It returns an empty set if there was a force push.
There is an open bug for this:
https://issues.jenkins.io/browse/JENKINS-68010
In case someone else has the same question:
I've managed to fetch old (amended) commit from Bitbucket within Jenkins. So yes, Bitbucket stores commit history.
And it's possible to do it within Jenkins script if you have Git credentials with:
git fetch origin <full_commit_hash>
Note that it has to be full commit hash, not the short version of it.