Search code examples
gitlabgitlab-ci

Is it possible to check if a rebase is required in a GitLab merge request pipeline?


I need to perform some technical checks on other systems before I can allow branches to be rebased in GitLab. This is why I want to add a pipeline step to the merge request to perform these checks in case a rebase is required. Is it possible to check if a rebase is required in the pipeline? I didn't find any CI variable for this use case.

Thanks for your help!


Solution

  • Edit March 2023

    @ConjuringCoffee tested my solution and it didn't work, but they found a way to solve it... if you happen to end up on my solution first, just skip ahead to theirs.


    Original Post

    As far as I know there is no GitLab way to check whether or not a branch needs to be rebased.

    Basing this response on this previous StackOverflow solution, I would suggest trying to use Git on the command line to determine if a rebase is required:

    job:
      script:
        - export BRANCH_NAME=$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME || $CI_COMMIT_BRANCH
        - hash1=$(git show-ref --heads -s $CI_DEFAULT_BRANCH)
        - hash2=$(git merge-base $CI_DEFAULT_BRANCH $BRANCH_NAME)
        - |
          if [[ "${hash1}" = "${hash2}" ]]; then
            echo "No rebase is not required"
          else
            echo "A rebase is required"
          fi
    

    I have not tested this myself. Please notify me if this fails.