Using Terraform to Create and Initialize Azure DevOps Repos

This is the 3rd part in the series of blog posts on managing the Azure DevOps using Terraform. You can find the series index here. In this blog post, we’ll create an Azure Repository to store source code for the developers using Terraform. We can create an empty repository as well as create a fork from another repository. The master repository needs to be an already hosted Azure DevOps repository. Do note that for fork to work, you need to use a PAT which has required access to the parent project. The code we have written has a little deviation from the official documentation, and we’ll discuss the same as well. Being the first provider version, such a small issue is quite likely and expected.

Create a Blank Azure DevOps Repository

To create a Azure DevOps Repository, we can use the azuredevops_git_repository resource. To create a blank repository, we need to provide some basic information such as the project name (which contains the repository), name for the repository and a flag or value specifying that we are creating an empty repository. The repository type will automatically be taken from Project Settings. All of this can be provided using the below block of code:

resource "azuredevops_git_repository" "tf-example-repo" {
project_id = azuredevops_project.tf-example.id
name = "terraform-example-repo"
initialization {
init_type = "Clean"
}
}
view raw azure-repo.tf hosted with ❤ by GitHub

If we run terraform plan now, we can see the change that it is going to make:

We can run terraform apply -auto-approve now to make the changes.

Create a Fork of another Azure DevOps Repository

We can also choose to create and initialized repository based on fork of another Azure DevOps repository. For this, we have to supply the parent repository id. Again we can use data source azuredevops_project to fetch the id associated the parent project. After this, we can use data source azuredevops_git_repositories to fetch the id for the repository. Below is our code for same:

data "azuredevops_project" "parent-project" {
project_name = "Space Game – web – Pipeline"
}
data "azuredevops_git_repositories" "parent-repo" {
project_id = data.azuredevops_project.parent-project.id
name = "Space Game – web"
}
resource "azuredevops_git_repository" "tf-forked-repo" {
parent_repository_id = data.azuredevops_git_repositories.parent-repo.repositories[0].id
project_id = azuredevops_project.tf-example.id
name = "forked-repo"
}

Do note that above code is a deviation from official documentation, which does not seem to be proper, as of writing of this code. Contradictory to official documentation, parent_id is not accepted as a valid property inside azuredevops_git_repository resource. Only parent_repository_id is a valid property which makes more sense. Again, official documentation says that project id of the project containing parent source code repository needs to be supplied which does not make sense because a parent project can contain multiple repositories. So how will Terraform know which repo to refer from parent project. Also the type supplied must be a UUID formatted string. So I had to re-write it in the above manner to make it work.

Note: Above issue is now covered under GitHub Issue.

We can now run the familiar terraform plan and terraform apply commands to apply our changes:

We can now hop over to Azure DevOps and verify that forked repo has been created fine:

Note: Although it creates a fork, it crashes terraform-apply. So you’ll have to bear with that.

One thought on “Using Terraform to Create and Initialize Azure DevOps Repos

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s