How do I detect that gerrit change needs rebasing on Jenkins?

359 Views Asked by At

I want to fail-fast on Gerrit triggered Jenkins jobs when the changes are outdated and they need rebasing by adding a piece of code at the start of the job that triggers and error: "Rebase your change first."

You can find the list of Jenkins variables defined by Gerrit plugin at What environment variables are passed go Jenkins when using the Gerrit Trigger Plugin?

1

There are 1 best solutions below

0
sorin On

This broken attempt:

#!/bin/bash
set -exo pipefail
# fast-fail if gerrit change is not rebased
if [ -z ${GERRIT_PROJECT+x} ]; then
  pushd $GERRIT_PROJECT
    MERGE_BASE=$(git merge-base HEAD origin/$GERRIT_BRANCH)
    git rebase
    [ "$MERGE_BASE" == "$(git merge-base HEAD origin/$GERRIT_BRANCH)" ] || {
      echo "FATAL:  Please rebase $GERRIT_CHANGE_URL change request before testing it."
      exit 101
    }
  popd
fi

Somehow this worked on some jobs using pipelines but failed on others, I suspect due to a different checkout method. So, still looking for a reliable way of doing it.