GitLab Merge Request Automation with API Calls - n8n Workflow

Use this powerful n8n workflow to automate checking, creating, closing, commenting on, and merging GitLab Merge Requests (MRs) directly using the HTTP Request n8n node.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

DevOps Engineers looking to streamline deployment pipelines.
Technical leads who need reliable, scheduled code deployment automation.
Users seeking advanced examples of how to integrate third-party APIs using the n8n node.
Anyone wanting to convert manual GitLab steps into a robust, scheduled n8n trigger.

Overview

Managing Merge Requests (MRs) manually can be tedious and prone to human error, especially in continuous integration environments. This n8n workflow solves this by providing a complete, automated MR lifecycle management system directly integrated with the GitLab API.

Starting with a simple n8n trigger, the automation checks if a specific MR already exists. If it finds one or more, it automatically closes them via an iterative loop, ensuring a clean slate. It then creates a new MR, adds necessary custom notes for context, waits for the pipeline to initiate, and finally executes the merge action, optionally removing the source branch. This powerful approach demonstrates how specialized n8n templates can manage complex DevOps operations reliably.

How it Works

The entire process is controlled by a scheduled n8n trigger and executes the following steps:


  1. Schedule Activation: The n8n workflow starts based on the configured schedule using the Schedule Trigger n8n node.

  2. Check Existing MRs: An HTTP Request n8n node queries the GitLab API to check for any currently 'opened' Merge Requests originating from the specified sourceBranchName.

  3. Conditional Logic: The 'Is Exists' n8n node checks if the previous API call returned an empty dataset.

  4. If MR Exists (Cleanup Path): If existing MRs are found, the Split In Batches n8n node iterates over them. A subsequent HTTP Request n8n node uses a PUT command to explicitly set the stateevent to close for each existing MR.

  5. If MR Does Not Exist (Creation Path): If no existing MRs are found, a new HTTP Request n8n node uses a POST command to create a new Merge Request, specifying the source branch, target branch, and title.

  6. Add Comments: Following creation, another HTTP Request n8n node posts custom notes () to the newly created Merge Request.

  7. Wait Period: A Wait n8n node pauses the execution for 30 seconds, allowing time for potential pipeline initialization or external manual reviews.

  8. Set Merge Variables: A Set n8n node defines the final parameters for merging, such as shouldremovesourcebranch: true.

  9. Final Merge Action: The final HTTP Request n8n node sends a PUT request to the merge endpoint, using the variables set previously to finalize the Merge Request, thereby completing this automated n8n workflow.

Installation Guide

To deploy this n8n workflow, follow these steps:


  1. Import: Copy the provided JSON data and paste it directly into your n8n canvas.

  2. Credentials: This n8n workflow requires a connection to GitLab via API token. Ensure you have a valid GitLab Private Token with api scope.

  3. Configuration: Update all placeholder values (, sourceBranchName, targetBranchName, mergeTitle, gitlabToken, mergeComments, and ) within the HTTP Request n8n node expressions. These values must be replaced with static strings or references to incoming data/Set n8n nodes if you modify the trigger.

  4. Testing: Run the n8n workflow manually to verify connection and logic before activating the Schedule Trigger n8n node.

Node Details

Schedule Trigger (n8n trigger): Initiates the entire automation on a set schedule.
API to Check existing merge request (HTTP Request n8n node): Sends a GET request to the GitLab Merge Request API, filtering by state: opened and a specific sourcebranch. Crucial for deciding the flow path.
Is Exists (If n8n node): Implements the core flow control, directing the n8n workflow based on whether existing open MRs were found. It uses the isEmpty() expression.
Loop Over Items (Split In Batches n8n node): Manages iteration when multiple existing Merge Requests must be closed.
API to CLOSE existing Merge Request (HTTP Request n8n node): Uses a PUT request to update the MR state to 'close'. Essential for cleanup in this n8n template.
Create New Merge Request (HTTP Request n8n node): Uses a POST request with URL-encoded body parameters (source
branch, targetbranch, title) to initiate the new MR.
Add Custom Notes To Merge Request (HTTP Request n8n node): Posts a comment/note to the MR using the internal merge request ID (iid>).
30 secs wait... (Wait n8n node): Pauses the n8n workflow execution flow for a specific duration (30 seconds) before attempting the final merge.
setValueForMerge (Set n8n node): Standardizes the input data for the final merge step, defining parameters like shouldremovesource_branch.
Merge When Pipeline Succeeds (HTTP Request n8n node): The final action. Sends a PUT request with a JSON body to execute the merge operation on the GitLab server, finalizing the goal of this n8n template.

Related n8n Workflows

Free

Nodes: 6 Nodes
Updated: December 26 2025
View all
Created by
Aditya Gaur
Aditya Gaur

Featured*