Your GitLab CI pipeline is failing because the user or bot running the pipeline doesn’t have the necessary permissions to merge code into the target branch. This is a security measure to prevent unauthorized changes to your codebase.

Here are the common causes and how to fix them:

Cause 1: Pipeline User Lacks Merge Rights

The most common reason is that the user associated with the CI job (often a project access token or a system user) doesn’t have the "Developer" role or higher on the project. Merge capabilities are tied to these roles.

Diagnosis: Navigate to your project’s Settings > Members. Look for the user or bot that is running your CI jobs. Check its assigned role. If it’s "Guest" or "Reporter," that’s the problem. You can also check the CI/CD variables for CI_JOB_USER or CI_PIPELINE_USER if they are explicitly set.

Fix: Grant the user or bot the "Developer" role.

  1. Go to Settings > Members.
  2. Click Invite members.
  3. Enter the username or email of the user/bot.
  4. Select the Developer role.
  5. Click Invite.

For bots or service accounts, ensure they are properly configured with this role.

Why it works: The "Developer" role in GitLab includes permissions to create branches, push commits, and merge branches, which is exactly what your CI pipeline needs to automate merges.

Cause 2: Protected Branch Rules Blocking Merge

Your target branch (e.g., main, master, develop) might be protected, and the CI user is not allowed to merge into it according to the branch protection rules.

Diagnosis: Go to Settings > Repository. Expand the Protected branches section. Check the rules for the branch your CI is trying to merge into. Specifically, look at the "Allowed to merge" setting. If the user or role associated with your CI job isn’t listed or allowed, this is the issue.

Fix: Modify the protected branch rules to allow the CI user or role to merge.

  1. Go to Settings > Repository.
  2. Expand Protected branches.
  3. Find your target branch and click Edit.
  4. Under "Allowed to merge," select the role that your CI user has (e.g., "Developer," "Maintainer," or a custom role). If you are using a specific user or service account, you might need to ensure a role that includes them is selected.
  5. Click Save changes.

Why it works: Branch protection rules enforce who can make changes to critical branches. By updating these rules, you explicitly permit the CI process to perform the merge operation.

Cause 3: Insufficient Scope for Project Access Tokens

If you’re using a Project Access Token to authenticate your CI jobs, it might not have been created with the necessary scopes. The api scope is typically required for merge operations.

Diagnosis: Navigate to your project’s Settings > Access Tokens. Find the token used by your CI/CD configuration. Check its "Scopes." If api is not listed, this is the problem.

Fix: Create a new Project Access Token with the api scope or update the existing one (though creating a new one is often simpler and safer as tokens cannot be edited after creation).

  1. Go to Settings > Access Tokens.
  2. Click Create new token.
  3. Give it a descriptive name (e.g., ci-merge-token).
  4. Set an expiry date.
  5. Select the api scope. You may also need read_repository and write_repository.
  6. Click Create project access token.
  7. Crucially: Update your CI/CD variables in Settings > CI/CD > Variables to use this new token (e.g., as CI_JOB_TOKEN or a custom variable, and ensure your .gitlab-ci.yml uses it correctly for authentication, often by setting the CI_JOB_TOKEN variable in your CI job or by using the token in an Authorization header).

Why it works: The api scope grants the token broad access to interact with the GitLab API, which includes functionalities like creating merge requests and merging branches.

Cause 4: Project is Part of a Group with Restrictive CI/CD Settings

If your project is nested within multiple groups, group-level CI/CD settings or security policies might be overriding project-specific settings and restricting merge capabilities for CI jobs.

Diagnosis: Check the parent group(s) of your project. Navigate to the group’s Settings > CI/CD. Look for any security policies, protected branches, or access restrictions that might be inherited by your project.

Fix: Adjust the group-level CI/CD settings or security policies to allow merges for your CI user/role. Alternatively, if group-level settings are too restrictive, consider moving the project to a different group with more permissive settings, or ensuring the CI user is explicitly allowed at the group level.

Why it works: Group settings often act as a higher-level enforcement mechanism. Aligning these with project needs ensures that the CI job has the necessary permissions across all levels of your GitLab hierarchy.

Cause 5: Merge Request Approvals Blocking Automatic Merge

If your CI pipeline is configured to create a merge request and then automatically merge it upon certain conditions, but an approval rule is in place that requires manual approval, the pipeline will stall.

Diagnosis: Go to Settings > Repository > Merge requests. Look at the "Approval rules" section. Check if there are any rules that require manual approvals before a merge can occur. See if the CI user or role is excluded from these rules or if the rules are being met by other means.

Fix: Modify the approval rules to either:

  1. Require fewer approvals.
  2. Allow the CI user/bot to approve.
  3. Exclude the CI user/bot from needing to approve.
  4. Ensure that any required approvals are met by human users before the CI pipeline attempts to merge.

Why it works: Merge request approvals are a safeguard to ensure code quality. By configuring these rules appropriately, you can either automate the approval process for your CI or ensure that manual approvals are completed, allowing the pipeline to proceed.

Cause 6: GitLab Runner Configuration Issues (Less Common for Permissions)

While less directly related to permissions for merging, an improperly configured runner could sometimes manifest as permission errors if it’s not correctly authenticated or if its environment variables are not set up to allow API access.

Diagnosis: Check your GitLab runner’s configuration (config.toml) and its registration. Ensure it’s correctly linked to your GitLab instance and has the necessary capabilities. Also, inspect the job logs for any signs of runner-specific authentication failures or network issues preventing API calls.

Fix: Re-register the runner if necessary, ensuring it has the correct URL and token. Verify that the runner has access to the GitLab API endpoint. If using tags, ensure the job is correctly tagged to use the appropriate runner.

Why it works: A correctly configured runner ensures that the CI job executes in an environment that can reliably communicate with the GitLab API for all its operations, including merging.

The next error you’ll likely encounter after fixing these permissions is related to merge conflicts, as the system will now be able to attempt the merge but find that the target branch has diverged from the source branch.

Want structured learning?

Take the full Gitlab-ci course →