NashTech Insights

How to write an ADO pipeline for the terratest.

Mohd Muzakkir Saifi
Mohd Muzakkir Saifi
Table of Contents
code coding computer cyberspace


Hello Learners, I am back with another blog. In today’s blog, we will learn about the ADO pipeline for terratest. In this blog, we will explore How we can write and run the test cases with the ado pipeline. If you like my blogs, you can read more blogs here.

Note:- I am working on the terratest blog series. So please follow my blog and learn about the terratest. You can check out this link for more blogs on the terratest.

Azure Pipeline:-

Azure DevOps (ADO) Pipelines is a cloud-based continuous integration and continuous delivery (CI/CD) platform provided by Microsoft Azure cloud. It is a powerful tool that allows developers to automate the build, test, and deployment processes for their applications.

Feature of Azure DevOps Pipelines:

  1. CI/CD Automation: Azure DevOps Pipelines enable developers to set up automated workflows to build, test, and deploy their applications with ease. This automation ensures that code changes are continuously integrated, tested, and delivered to production or staging environments.
  2. Multi-Platform Support: ADO Pipelines supports building and deploying applications on various platforms, including Windows, Linux, and macOS. It supports different programming languages also.
  3. YAML-Based Pipeline Definition: Pipelines in ADO are defined using YAML, a human-readable format.
  4. Integration with Azure Services: Azure DevOps Pipelines seamlessly integrate with other Azure services, such as Azure Repos, Azure Artifacts, Azure Functions, Azure Kubernetes Service (AKS), and more.
  5. Triggers and Scheduling: Pipelines can be triggered automatically on code commits, pull requests, or other events, ensuring continuous integration. Additionally, developers can schedule pipelines to run at specific times.


In this section, We will create an Azure DevOps pipeline to trigger the terratest to test the Private ACR. I have divided this pipeline into parts so that we can understand it in steps.


In this section, we will set up the trigger point for the pipeline. For now, we have two trigger points we can trigger the pipeline manually or we can set up the pipeline with respect to the cron job.

This is the manual trigger:-

name: ACR Test using terratest
trigger: none

This is the cron job trigger:

  - cron: "*/20 * * * *"
    always: true
        - terratest-acr-module

In the trigger, we need to define the cron time. As per the about cron, this pipeline will trigger every 20 mins. In the last, I have defined the branch which is in the bold font.

Variable Group:-

As we need a few credentials or secret IDs or passwords in the pipeline. So we can not add them to the pipeline directly. So we will use the variable group for that. We will define them in the variable group and call them into the pipeline. You can create your variable group from here.

  - group: Azure Credential

My variable group name is Azure Credential. So I am calling it.


All the steps will be defined in this section. This Jobs section contains more than one job.

- job: Run Terratest


In this section, you can create any task and step and define your flow.

    - script: |
        echo "This is second path", $(Build.SourcesDirectory)
        mkdir "$(Build.SourcesDirectory)/terratest"
        ls -R "$(Build.SourcesDirectory)"
        ls -R "$(Build.SourcesDirectory)/terratest"
        cd "$(Build.SourcesDirectory)/terratest"
        ls -la
      displayName: 'Create folder'

First of all, I am creating a folder in the ADO agent. So that I can perform my operation in that folder.

- checkout: self
      displayName: 'Checkout terratest branch repo'
      path: terratest
      clean: true

Check out the code from the git repo in the Ado-Agent.

    - script: |
        curl -sL | sudo bash
      displayName: 'Install Azure CLI'

    - script: |
        sudo apt-get update
        sudo apt-get install -y unzip
        curl -LO
        rm -rf terraform_1.*
        sudo mv terraform /usr/local/bin/
        terraform --version
      displayName: 'Install Terraform'

    - script: |
        curl -LO
        sudo tar -C /usr/local -xzf go1.17.2.linux-amd64.tar.gz
        rm -rf go1.*
        export GOROOT=/usr/local/go
        export GOPATH=$(HOME)/go
        export PATH=$GOROOT/bin:$GOPATH/bin:$PATH
        echo "export GOROOT=/usr/local/go" >> $(HOME)/.bashrc
        echo "export GOPATH=$(HOME)/go" >> $(HOME)/.bashrc
        echo "export PATH=\$GOROOT/bin:\$GOPATH/bin:\$PATH" >> $(HOME)/.bashrc
        source $(HOME)/.bashrc
        go version
      displayName: 'Install Go'

Now I am adding the script to install the required package for the terratest like Azure CLI, Terraform, and Golang.

    - script: |
        az --version
        az login --service-principal -u $appid) -p $(appsecret) --tenant $(TenantId)
        echo "This is subscription id:=" $(ARM_SUBSCRIPTION_ID)
        az account set --subscription $(ARM_SUBSCRIPTION_ID)
        az account show
        # az resource list --subscription $(ARM_SUBSCRIPTION_ID)
      displayName: 'Check Azure CLI Version and Set up the Account'

I am authenticating with the azure account with the help of az cli.

Now, We have define all the all the step for the pipeline and now you can run the terratest code. So We will add the new script which can run the terratest:

    - script: |
          export PATH=$PATH:/usr/local/go/bin
          cd test
          go install
          go mod init
          go mod tidy
          # az account show
          go test -v -timeout 50m
      displayName: 'Run Terratest'
        TF_VAR_azure_client_id: $(appid)
        TF_VAR_azure_client_secret: $(appsecret)
        TF_VAR_azure_tenant_id: $(TenantId)
        TF_VAR_azure_subscription_id: $(ARM_SUBSCRIPTION_ID)       

This script can run the terratest and I am passing the environment variable because the terratest code will require these values while running the terraform.

It is a complete Azure pipeline to trigger the terratest. So you can also get the complete pipeline from here.

You can trigger the pipeline and will see the output below:

As you can see our pipeline has been run successfully.


In this blog, We have seen how we can create the ado pipeline for the terratest. I have divided all the steps and tasks into steps so that we can understand and I also attached the git URL. you can clone from there. If you like my blog then you can like this or if want to read more blogs then follow this link. and also check out the terratest official doc

Mohd Muzakkir Saifi

Mohd Muzakkir Saifi

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: