Search code examples
gitjenkinsjenkins-pipelinegit-amend

How to access amended commit in Jenkins?


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:

  1. Does the bitbucket store commit history and is it possible to fetch it somehow in Jenkins?
  2. Does the Jenkins store commit(s) from the build somewhere? Would it be possible to get commits from the previous Jenkins build?

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


Solution

  • 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.