CD Release pipeline automation in devOps

I need to automate CI/CD pipeline in azure devOps using terraform infrastructure tool, for CI i have resources from terraform to automate but I have no resources for automating CD release pipeline using terraform, Is there any way to automate release pipeline in azure devOps using terraform?

Can you clarify, do you want to:

a) Use Terraform to provision/configure Azure DevOps pipelines
or
b) Use Azure Devops pipelines to provision resources via Terraform

I need to use Terraform to provision/configure Azure DevOps pipelines

Thanks for the clarification @boopathi-007.

So, you are likely already familiar with the microsoft/azuredevops provider.

As you will know, this provider does not provide any resources for managing pipelines.

They can, however, be managed via the Azure Devops API and it is possible to use the fmontezuma/restapi provider to make the appropriate calls to create and destroy pipelines.

The below example creates a pipeline based upon YAML pipeline definition which is in the specified REPO within the project

Set the noted elements in the provider block (or comment out where indicated and set the relevant environment variables). And also set the appropriate values in the locals block. Once you have done that, this example should run as a stand-alone module if you drop it into an empty folder and run terraform init and terraform plan / terraform apply

terraform {
  required_providers {
    restapi = {
      source  = "fmontezuma/restapi"
      version = "1.14.1"
    }
  }
}

provider "restapi" {
  uri      = "https://dev.azure.com/{org}/{project}/_apis" # The URI of the REST API - replace {org} and {project} with your values - This can also be set with the environment variable REST_API_URI (do not include uri attribute in provider block)
  username = "user"                                        # Must me set to something - even though PAT authentication ignores it
  password = "PAT"                                         # Personal Access Token. This can also be set with the environment variable REST_API_PASSWORD (do not include password attribute in provider block)

  write_returns_object = true
}

locals {
  repository_name = "devops-api"
  pipline_folder  = "\\BILL\\"            # This is where the pipeline will be created. Escape the backslashes.
  pipeline_path   = "azure-pipelines.yml" # This is the path to the pipeline configuration file in the repository
  pipeline_name   = "elephant"
}


# Data block to get the repository ID from the repository name
data "restapi_object" "repository" {
  path         = "/git/repositories" #?api-version=7.2-preview.1"
  search_key   = "name"
  search_value = local.repository_name
  results_key  = "value"
}

# Resource block to create a new pipeline
resource "restapi_object" "devops_pipeline" {
  path         = "/pipelines?api-version=7.2-preview.1"
  read_path    = "/pipelines/{id}?api-version=7.2-preview.1"
  destroy_path = "/build/definitions/{id}?api-version=7.2-preview.7" # This is the path to delete the pipeline - which has to be done via the build API
  data = jsonencode({
    configuration = {
      path = local.pipeline_path
      repository = {
        id   = data.restapi_object.repository.api_data.id,
        type = "azureReposGit"
      }
      type = "yaml"
    }
    folder = local.pipline_folder,
    name   = local.pipeline_name
  })
  force_new = ["configuration"] # This is required to force the resource to be recreated when the configuration changes as the pipelines API does not support updating the configuration of an existing pipeline (PUT)
}

# Uncomment the following outputs to see the results of the data and resource blocks

# output "repository" {
#   value = data.restapi_object.repository.api_data
# }

# output "devops_pipeline" {
#   value = jsondecode(restapi_object.devops_pipeline.create_response)
# }

While it may not be as robust as a first-class resource in a provider, it does extend the capabilities of terraform to manage pipeline resources (and others) that are supported by the API but not by the first-class provider at this time.

Hope that helps.

If this post your question, please mark it as the answer to help others find the solution.

Happy Terraforming!

The above information was helpful but I also need the implementation of creating “Release Pipeline” , create the stage and adding tasks for that stage using terraform code…Like I can already achieve this via REST API using python integrating with terraform …I need any proper method to implement this process in Terraform because I need to automate CD pipeline in DevOps.

OK - this sounds like you are using the ‘classic’ pipelines, as opposed to the modern YAML-based pipelines.

As you know, the microsoft.azuredevops provider does not have the relevant resources for this. If you are already creating Release pipelines via API using Python you will need to approach it a similar way by leveraging Terraform.

However, I would consider pipelines (as in their runnable steps/stages/etc.) are not platform/infrastructure and are actually code.
In fact the modern Azure DevOps YAML-based pipelines further reinforce this by needing to be checked in as part of the repo. So creating a pipeline ‘object’ as above, as part of the project/repo configuration which references the YAML file (code) is (arguably) valid.
Using Terraform to write/create and deploy ‘code’ (as in the pipeline steps stages etc - which under the covers are YAML now anyway) seems like a potentially ill-advised approach.

You may also want to look at the modern YAML-based pipeline’s deployment jobs approach, along with environments. Which should be able to take the place of the classic deployment pipelines in almost all cases. This may enable you to adopt a different approach, splitting the pipeline into code (in the repo) and config/deployment (In terraform).

Sorry I don’t have any further suggestions for your specific requirement.