Skip to content

willianpaixao/terraform-gitlab-project

Repository files navigation

GitLab Logo

Gitlab project Terraform module

This module is built upon the gitlabhq/gitlab provider, and is used to create a new Gitlab projects (aka Git repository) under an existing group. It reduces the boilerplate and has better security flags raised by default.

-> The group can be created or imported using gitlab_group

Default resources and features

Feature Default
CI/CD Pipelines true
Container Registry true
Git Large File Storage (LFS) false
Gitlab Pages false
Issues false
Package Registry false
Snippets false
Wiki false

Examples

Create one repository

Pretty simple, add the following block and replace the values accordingly.

module "project" {
  source  = "willianpaixao/project/gitlab"
  version = "~> 1.0.1"

  name         = "My New Pet project"
  description  = "Yet another project I will never finish"
  namespace_id = gitlab_group.some-group.id
  tags         = ["pet", "unfinished"]
}

Create many repositories under the same subgroup

The usage consists of two simple steps, first define your variable in variables.tf then add the module block to your code, referencing the source as following:

variable "project" {
  description = "list of attributes of a project"
  type        = map(any)
  default = {
    your-shiny-project = {
      name        = "your-shiny-project"
      description = "A descriptive description"
      tags        = ["tagA", "tagB"]
  }
}

module "subgroup_projects" {
  source  = "willianpaixao/project/gitlab"
  version = "~> 1.0.1"

  for_each = var.project

  name         = each.value.name
  description  = each.value.description
  namespace_id = gitlab_group.some-group.id
  tags         = setunion(["subgroup"], each.value.tags)
}

Setting a list of project owners

data "gitlab_user" "owners" {
  for_each = toset(["user1", "user2", "user3"])
  username = each.value
}

module "api" {
  source  = "willianpaixao/project/gitlab"
  version = "~> 1.0.1"

  name               = "api"
  description        = "REST API"
  namespace_id       = gitlab_group.some-group.id
  approvals_required = 3
  owners             = [for user in data.owners.users : user.id]
  tags               = "api"
}

resource "gitlab_project_membership" "owners" {
  for_each     = [for user in data.owners.users : user.id]

  project_id   = module.api.id
  user_id      = each.key
  access_level = "owners"
}

That's it! Run terraform plan and check the output to see if matches your recent changes.

Providers

Name Version
gitlab >= 3.7.0

Resources

Name Type
gitlab_branch_protection.default resource
gitlab_pipeline_trigger.default resource
gitlab_project.default resource
gitlab_project_approval_rule.default resource
gitlab_project_level_mr_approvals.default resource
gitlab_tag_protection.default resource

Inputs

Name Description Type Default Required
description A short description of the project string n/a yes
name Name of the project string n/a yes
namespace_id The group where the project belongs to string n/a yes
approvals_required The minimum number of approvals required for MRs number 1 no
author_email_regex All commit author emails must match this regex. As part of Push Rules, it's only available for GitLab Premium string null no
container_registry_enabled Enable container registry for the project bool true no
default_branch The default branch for the project string "main" no
owners A list of specific User IDs allowed to approve Merge Requests. Please refer to Gitlab documentation for further information list(string) [] no
pipelines_enabled Enable pipelines for the project bool true no
shared_runners_enabled Enable shared runners for the project bool true no
tags The list of tags to be attached to the project list(string) [] no
template_project_id Project ID of a custom project template. Please refer to Gitlab documentation for further information number null no
use_custom_template Use either custom instance or group-level project template bool false no

Outputs

Name Description
id Integer that uniquely identifies the project within the gitlab install
path_with_namespace The path of the repository with namespace
web_url URL that can be used to find the project in a browser

Changelog

Please refer to our changelog for further information on versioning and upgrades.

Contributing

Checkout our contributing guide.

References