Search code examples
gitgitlabgitlab-ci

How can i automate the merging from staging to production?


I have a project on gitlab with a staging and a production branches, i want to have my feature branches being automatically merged into staging with merge requests and the staging being merged into production with a manually created merge request.

The problem of this approach is that after every manual merge request from staging to production, the second one ends up being 2 commits ahead of the first one, making any subsequent merge request blocked or in the need to be locally merged and then uploaded.

How can i automate the merging from staging to production?


Solution

  • I think you should rethink your branching strategy. Do you really want to merge all of staging into production, or rather just those features that are ready?

    I'd recommend merging the feature branches into main, and leave the integration/staging branch as it is. Regularily merge main into staging, and from time to time you recreate staging from scratch as taking main plus merging all current features branches into it.

    With a bit of scripting, you can fully automate that using the Gitlab API to find MRs worth merging.

    To control what feature branch gets merged into what target, we use custom labels to mark an MR as "merge into staging::automatically" after review. If the script fails to actually do the merge, it applies the label "merge into staging::failed" to the MR so everyone involed will get notified.