n8n

How to Automate GitLab Merge Request Management?

Keep code changes moving without manual clicks. The workflow checks for merge requests on a schedule, opens one when needed, adds notes, and merges only after checks pass. It fits teams that want clean branches and faster releases.

It runs on a timer. First it calls GitLab to find open merge requests for the source branch. If none exist, it creates a new one, posts a custom comment, waits 30 seconds for reviews and the pipeline, then merges when the pipeline succeeds. If older or duplicate requests are found, it loops through them in batches and closes each one. All API calls use a private token in the request header.

Set your project ID, source branch, target branch, title, and note text. Map the merge request id from the create step into the note, close, and merge steps. Expect fewer delays and fewer mistakes, with less context switching. Teams often save 20 to 30 minutes per release cycle. Adjust the wait time to match your CI runtime. Great for nightly promotions and cleanup after feature branches. See the Tools Required section above to set up accounts and keys.

What are the key features?

  • Scheduled checks for open merge requests by source branch using GitLab API
  • Conditional path to create a new merge request when none exists
  • Batch loop to find and close older or duplicate merge requests
  • Automatic notes posted to the merge request with custom content
  • Timed wait to allow reviews and CI jobs to run before merge
  • Merge action that runs only when the pipeline succeeds
  • Private token header for secure API calls to GitLab
  • Set node prepares merge parameters for the final action

What are the benefits?

  • Reduce manual merge request work from 30 minutes to 2 minutes per release
  • Streamline branch promotion steps by about 70 percent
  • Eliminate duplicate merge requests and cut related errors by 90 percent
  • Merge only when the pipeline passes for 100 percent gated releases
  • Handle up to 5 times more branch updates without extra effort
  • Keep all GitLab actions in one place for easier audits

How do you set it up?

  1. Import the template into n8n: Create a new workflow in n8n > Click the three dots menu > Select 'Import from File' > Choose the downloaded JSON file.
  2. You'll need accounts with GitLab. See the Tools Required section above for links to create accounts with this service.
  3. In GitLab, create a Personal Access Token with the api scope. Copy the token and keep it secure.
  4. Open each HTTP Request node in n8n. In the Credential to connect with dropdown, click Create new credential and follow the on screen steps. Add your GitLab token as a header named PRIVATE-TOKEN.
  5. Replace the project placeholder in each API URL with your actual GitLab project ID or path.
  6. Set values for sourceBranchName, targetBranchName, mergeTitle, and mergeComments. Use expressions or a Set node so these values are easy to change later.
  7. Ensure the create step output provides the merge request iid. Map that iid into the note, close, and merge nodes where the URL includes the merge request id.
  8. Run a test with a branch that has no open merge request. Confirm a new merge request is created in GitLab and the custom note appears.
  9. Create a duplicate or extra merge request for the same branch and run the workflow again. Check that older requests are closed by the loop.
  10. Trigger a passing pipeline and watch the execution. If the merge happens before jobs finish, increase the wait time from 30 seconds to match your CI runtime.
  11. If you see 401 errors, verify the token and header name. If you see 404 on merge request paths, confirm the iid mapping and project ID.
  12. When everything looks good, set the Schedule Trigger to your desired frequency and activate the workflow.

Tools Required

$24 / mo or $20 / mo billed annually to use n8n in the cloud. However, the local or self-hosted n8n Community Edition is free.

GitLab

Sign up

Free: $0/user / mo (GitLab.com). API access via Personal Access Tokens on Free tier

Similar Templates

Join Futurise to access 1,200+ automation templates

Get instant access to ready-made automation workflows for n8n, Make.com, AI agents, and more. Download, customise, and deploy in minutes.