how to set up azure-pipeline.yaml file for resource deployments in different subfolders which contain tfvar files

59 views Asked by At

I have a project folder "Azure_Architecture" which contains 2 sub folders "module" and "subscription". Each subscription folders contain 2 another sub folders contain eu-north and eu-west.

Under eu-north there is a folder called platform and it contain a resource_group folder inside that platform folder ( actually there are different resource folders like vnets, subnet etc. For reference mentioned only resource folder).

In the resource folder i have a provider.tf, main.tf, variable.tf and terraform.tfvars files.

The pipeline should trigger automatically when i made some changes in the terraform.tfvars files from each resouce subfolders.

I have tried the below code snippet and it is not working

trigger:
  branches:
    include:
      - main
 
pool:
  vmImage: 'ubuntu-latest'
 
steps:
- checkout: self  # Disable automatic checkout
 
- script: |
    #repo_path=$(System.DefaultWorkingDirectory)
    latest_file=$(git log -1 --name-only --pretty=format: -- "Azure_Architecture")
    echo "Latest file modified: $latest_file"
    folder_path="$(dirname "$latest_file")"
    echo "Moving to folder: $folder_path"
    cd $(System.DefaultWorkingDirectory)
    pwd
    # cd $(System.DefaultWorkingDirectory)'/'$folder_path
    # pwd
    # cd ..
    #pwd
  displayName: 'Check latest modified file and move to folder'  
 
- script: |
   
  displayName: 'Initialize and Validate Terraform'

- script: |
    terraform init 
  displayName: 'Terraform init'

- script: |
    terraform plan 
  displayName: 'Terraform Plan'

- script: |
   
  displayName: 'Terraform Apply'
 #testing
1

There are 1 answers

0
Miao Tian-MSFT On

According to your description, you want to trigger your pipeline when you modify the contents of the terraform.tfvars file.

Currently in your yaml, the trigger of the pipeline is that modifying the main branch file will trigger the pipeline. If the pipeline is not triggered, please check if you modified the terraform.tfvars files of the main branch.

You can also specify file paths trigger. For example, only trigger the pipeline when you modify the terraform.tfvars files in Azure_Architecture folder on any branch.

trigger:
  branches:
    include:
    - '*'
  paths:
    include:
    - Azure_Architecture/**/*terraform.tfvars

For more information, please refer CI triggers.