Photo by 2H Media

Add default files to Azure DevOps repositories using Terraform

Add mandatory default files

Posted by Damien Aicheh on 10/26/2022 · 5 mins

In this tutorial we will add some default files to the repositories we previously created associated with our Azure DevOps project using Terraform!

This tutorial is part of a full series of tutorials on configuring Azure DevOps using Terraform. You can download the project from the previous part and follow along.

Add the template files

For the purpose of this article we will add only two files in an assets folder, so the structure of the project will be:

| - set_up_azure_devops
   | - assets <---- Here is the folder you needs to add
      | - .gitignore 
      | - azure-pipelines.yml
   | - .env
   | - .gitignore
   | - env.tfvars
   | - project.tf
   | - provider.tf
   | - repos.tf
   | - variables.tf

So you can add a .gitignore with default values (this is just for the sample):

.env
.terraform
*.tfstate
**/*.lock.*
**/*.tfplan
terraform.tfplan
terraform.tfstate.backup
plan.out

And the Azure DevOps pipeline that we will automatically configure in the next tutorial:

trigger:
  - main

pool:
  vmImage: ubuntu-latest

steps:
  - script: echo Hello, world!
    displayName: 'Run a one-line script'

  - script: |
      echo Add other tasks to build, test, and deploy your project.
      echo See https://aka.ms/yaml
    displayName: 'Run a multi-line script'

As you can see it’s just a basic “Hello World”, of course if you know which type of project you create, feel free to add one more specific.

Add the files

Now let’s add the two files, first you need to create a new file called files.tf. As you can see below we iterate on the repositories list azuredevops_git_repository.this and we add the files from the assets folder. If your files just contain one or two lines you can define their content directly in the content property.

resource "azuredevops_git_repository_file" "default_pipeline" {
  count               = length(azuredevops_git_repository.this)
  repository_id       = azuredevops_git_repository.this[count.index].id
  file                = "azure-pipelines.yml"
  content             = file("${path.module}/assets/azure-pipelines.yml")
  branch              = "refs/heads/develop"
  commit_message      = "Add azure-pipelines.yml"
  overwrite_on_create = false

  lifecycle {
    ignore_changes = [
      file,
      content,
      commit_message
    ]
  }
}

resource "azuredevops_git_repository_file" "default_gitignore" {
  count               = length(azuredevops_git_repository.this)
  repository_id       = azuredevops_git_repository.this[count.index].id
  file                = ".gitignore"
  content             = file("${path.module}/assets/.gitignore")
  branch              = "refs/heads/develop"
  commit_message      = "Add .gitignore"
  overwrite_on_create = false

  lifecycle {
    ignore_changes = [
      file,
      content,
      commit_message
    ]
  }
}

You can find all the properties available in the Terraform documentation.

Run Terraform

Now you can run a Terraform plan command and apply it:

terraform plan -var-file=env.tfvars --out=plan.out

Then:

terraform apply plan.out

As result you should see one commit per each files added to your repositories inside Azure DevOps!

Final touch

Your repositories are now initialized correctly inside Azure DevOps using Terraform. You will find full source code in this Github repository.

What’s next?

In the next tutorial of this series we will focus on automatically creating a default pipeline for each repositories!

Do not hesitate to follow me on to not miss my next tutorial!