Vlad Gheorghe
Vlad Gheorghe

Reputation: 480

Automatically merge Master to Develop on Gitlab

Experiencing issues on Gitlab because the develop branch gradually deviates from master. For example, if somebody applies a hotfix on master it won't automatically appear in develop and that might create conflicts.

Is there a way for automatically syncing develop with master within the CI-CD process?

Related: Automatically syncing develop with master on merge

Upvotes: 3

Views: 3542

Answers (1)

Adam Marshall
Adam Marshall

Reputation: 7649

To build off @matt's comment, there's no builtin way to do this in Gitlab or any other SCM/CI/CD system that I'm aware of, but there are a few options if you really need this:

  1. You could register a webhook with Gitlab to send a hook when there's a push to main, then wherever you receive the hook can clone the repo, merge to develop, and push back to the remote.
  2. Another option is to have a small pipeline job that is started by an API call to checkout develop, merge, and push to the remote. This would still require the webhook from the first option, but doesn't require cloning.

However, doing this at all can be troublesome if you have multiple people working on a project. Say you have someone doing work on Develop, whether that's dev work, testing, whatever, and there's a push to main, which is automatically merged into develop. This now messes up that first person's work. This can be fine if you're the only person on the project though.

As an alternative to auto-merging, one thing I do in my projects that multiple people touch is to add a simple Pipeline job that sends a slack notification when there's a push to main. It looks like this:

Merge Notification:
  stage: "Send Notifications"
  image: centos:7
  only:
    - main
  needs: []
  variables:
    GIT_STRATEGY: none
  script:
    - 'curl -X POST --data-urlencode "payload={\"text\": \"There has been an update to the _*$CI_COMMIT_REF_NAME*_ branch. Please be sure to rebase your non-merged code as soon as possible!\", \"icon_emoji\": \":gitlab:\"}" https://hooks.slack.com/services/redacted'

I use the centos:7 image here, but normally for these types of things I use the alpine image since it's <50mb in size. I made this job so long ago I don't remember why I used centos. Other images should work fine here too, so you'll just have to experiment.

  • The only: main clause says that this job will only ever be added to the pipeline if the branch is main,

  • The needs: [] clause is relatively new to Gitlab. It lets you define a-cyclic pipelines, which just means that you can run jobs out of order. This example "needs" no other jobs from earlier in the pipeline, so it can safely run as soon as the pipeline starts, regardless of the stage. If you have enough runners available (or time, if using Gitlab.com), all eligible jobs can run at once, across all stages, depending on their needs.

  • Next, we tell the job that we don't need to clone/fetch the project at all. the GIT_STRATEGY: none variable means don't clone the project from Gitlab.

  • Lastly, the only script we're running is the standard curl command to send a message to a specific Slack Channel. This can be swapped for any other service if you don't use slack.

Upvotes: 2

Related Questions