How to automatically rebase all your Merge Requests on GitLab when pushing on master

Paul W.
Paul W.
Sep 24, 2020 · 2 min read

At OVRSEA we are used to merge our Merge Requests with a fast-forward policy. However, on GitLab, when you merge an MR on master, you must manually rebase all the other MRs afterwards. It’s time consuming and as developers we have other more important things to do.

To overcome this problem we made a very simple script to automate the rebase process of all opened MRs when a push is done on the main branch.

It runs on a GitLab CI pipeline alongside of our other pipelines.

auto-rebase.sh

We use curl for fetching all opened MRs via the GitLab API first, then we parse the returned JSON for iid variable with jq.

With each MR’s iid we can send a rebase request through GitLab API.

You have to define $GITLAB_PERSONAL_TOKEN and $PROJECT_ID environment variables, in our case we run this script on a GitLab CI pipeline so they are filled in the GitLab CI settings.

.gitlab-ci.yml

For the CI part we made a very simple pipeline :

  • We use node:14-alpine image wich includes a lightweight version of Linux.
  • A rule is added for running this job only on master branch.
  • jq and curl packages are necessary for our script, we install them with the apk command
  • And finally, our script is run.

OVRSEA