azuredevops_git_repository

Manages a git repository within Azure DevOps.

Example Usage

Create Git repository

resource "azuredevops_project" "example" {
  name               = "Example Project"
  visibility         = "private"
  version_control    = "Git"
  work_item_template = "Agile"
}

resource "azuredevops_git_repository" "example" {
  project_id = azuredevops_project.example.id
  name       = "Example Empty Git Repository"
  initialization {
    init_type = "Clean"
  }
}

Configure existing Git repository imported into Terraform state

resource "azuredevops_project" "example" {
  name               = "Example Project"
  visibility         = "private"
  version_control    = "Git"
  work_item_template = "Agile"
}

resource "azuredevops_git_repository" "example" {
  project_id     = azuredevops_project.example.id
  name           = "Example Git Repository"
  default_branch = "refs/heads/main"
  initialization {
    init_type = "Clean"
  }
  lifecycle {
    ignore_changes = [
      # Ignore changes to initialization to support importing existing repositories
      # Given that a repo now exists, either imported into terraform state or created by terraform,
      # we don't care for the configuration of initialization against the existing resource
      initialization,
    ]
  }
}

Create Fork of another Azure DevOps Git repository

resource "azuredevops_project" "example" {
  name               = "Example Project"
  visibility         = "private"
  version_control    = "Git"
  work_item_template = "Agile"
}

resource "azuredevops_git_repository" "example" {
  project_id     = azuredevops_project.example.id
  name           = "Example Git Repository"
  default_branch = "refs/heads/main"
  initialization {
    init_type = "Clean"
  }
}

resource "azuredevops_git_repository" "example-fork" {
  project_id           = azuredevops_project.example.id
  name                 = "Example Fork Repository"
  parent_repository_id = azuredevops_git_repository.example.id
  initialization {
    init_type = "Clean"
  }
}

Create Import from another Git repository

resource "azuredevops_project" "example" {
  name               = "Example Project"
  visibility         = "private"
  version_control    = "Git"
  work_item_template = "Agile"
}

resource "azuredevops_git_repository" "example" {
  project_id     = azuredevops_project.example.id
  name           = "Example Git Repository"
  default_branch = "refs/heads/main"
  initialization {
    init_type = "Clean"
  }
}

resource "azuredevops_git_repository" "example-import" {
  project_id = azuredevops_project.example.id
  name       = "Example Import Repository"
  initialization {
    init_type   = "Import"
    source_type = "Git"
    source_url  = "https://github.com/microsoft/terraform-provider-azuredevops.git"
  }
}

Import from a Private Repository

resource "azuredevops_project" "example" {
  name               = "Example Project"
  visibility         = "private"
  version_control    = "Git"
  work_item_template = "Agile"
}

resource "azuredevops_git_repository" "example" {
  project_id     = azuredevops_project.example.id
  name           = "Example Git Repository"
  default_branch = "refs/heads/main"
  initialization {
    init_type = "Clean"
  }
}

resource "azuredevops_serviceendpoint_generic_git" "example-serviceendpoint" {
  project_id            = azuredevops_project.example.id
  repository_url        = "https://dev.azure.com/org/project/_git/repository"
  username              = "username"
  password              = "<password>/<PAT>"
  service_endpoint_name = "Example Generic Git"
  description           = "Managed by Terraform"
}

resource "azuredevops_git_repository" "example-import" {
  project_id = azuredevops_project.example.id
  name       = "Example Import Existing Repository"
  initialization {
    init_type             = "Import"
    source_type           = "Git"
    source_url            = "https://dev.azure.com/example-org/private-repository.git"
    service_connection_id = azuredevops_serviceendpoint_generic_git.example-serviceendpoint.id
  }
}

Argument Reference

The following arguments are supported:

initialization - (Required) block supports the following:

Attributes Reference

In addition to all arguments above, except initialization, the following attributes are exported:

Import

Azure DevOps Repositories can be imported using the repo name or by the repo Guid e.g.

terraform import azuredevops_git_repository.example projectName/repoName

or

terraform import azuredevops_git_repository.example projectName/00000000-0000-0000-0000-000000000000

NOTE: Importing an existing repository and running terraform plan will detect a difference on the initialization block. The plan and apply will then attempt to delete the repository and recreate it so that the configuration matches. The initialization block must be ignored from the plan in order to support configuring existing repositories imported into Terraform state.

resource "azuredevops_project" "example" {
  name               = "Example Project"
  visibility         = "private"
  version_control    = "Git"
  work_item_template = "Agile"
}

resource "azuredevops_git_repository" "example" {
  project_id     = azuredevops_project.example.id
  name           = "Example Git Repository"
  default_branch = "refs/heads/main"
  initialization {
    init_type = "Clean"
  }
  lifecycle {
    ignore_changes = [
      # Ignore changes to initialization to support importing existing repositories
      # Given that a repo now exists, either imported into terraform state or created by terraform,
      # we don't care for the configuration of initialization against the existing resource
      initialization,
    ]
  }
}