gitlab_branch_protection (Resource)

The gitlab_branch_protection resource allows to manage the lifecycle of a protected branch of a repository.

Upstream API: GitLab REST API docs

Example Usage

resource "gitlab_branch_protection" "BranchProtect" {
  project                      = "12345"
  branch                       = "BranchProtected"
  push_access_level            = "developer"
  merge_access_level           = "developer"
  unprotect_access_level       = "developer"
  allow_force_push             = true
  code_owner_approval_required = true
  allowed_to_push {
    user_id = 5
  }
  allowed_to_push {
    user_id = 521
  }
  allowed_to_merge {
    user_id = 15
  }
  allowed_to_merge {
    user_id = 37
  }
  allowed_to_unprotect {
    user_id = 15
  }
  allowed_to_unprotect {
    group_id = 42
  }
}

# Example using dynamic block
resource "gitlab_branch_protection" "main" {
  project                = "12345"
  branch                 = "main"
  push_access_level      = "maintainer"
  merge_access_level     = "maintainer"
  unprotect_access_level = "maintainer"

  dynamic "allowed_to_push" {
    for_each = [50, 55, 60]
    content {
      user_id = allowed_to_push.value
    }
  }
}

Schema

Required

Optional

Read-Only

Nested Schema for allowed_to_merge

Optional:

Read-Only:

Nested Schema for allowed_to_push

Optional:

Read-Only:

Nested Schema for allowed_to_unprotect

Optional:

Read-Only:

Import

Import is supported using the following syntax:

# Gitlab protected branches can be imported with a key composed of `<project_id>:<branch>`, e.g.
terraform import gitlab_branch_protection.BranchProtect "12345:main"