Triggering Azure DevOps builds based on changes to sub folders Triggering Azure DevOps builds based on changes to sub folders git git

Triggering Azure DevOps builds based on changes to sub folders


You can do like below

  1. Create variables based on your microservices with the values "False"

E.g,MicroserviceAUpdated= "False",MicroserviceBUpdated= "False" etc.,

  1. Add a Powershell script task at the begin of your build definition.The powershell script will do the following:

Get the changeset/commit in the build to check which files are changed.

  • Update the MicroserviceAUpdated variable to "true" if only anyfiles are changed under SubFolderA.
  • Update the MicroserviceBUpdated variable to "true" if only any
    files are changed under SubFolderA.

So on....

  1. Create separate build task for each microservice, configure the build tasks to run with custom conditions like below

For MicroserviceA build Task

"Custom conditions": and(succeeded(), eq(variables['MicroserviceAUpdated'], 'True'))

For MicroserviceB build Task

"Custom conditions": and(succeeded(), eq(variables['MicroserviceBUpdated'], 'True'))

So on...

This way MicoserviceTask will be skipped if the value of the variable is False

For Step 2

$files=$(git diff HEAD HEAD~ --name-only)$temp=$files -split ' '$count=$temp.Lengthecho "Total changed $count files"For ($i=0; $i -lt $temp.Length; $i++){  $name=$temp[$i]  echo "this is $name file"  if ($name -like "SubFolderA/*")    {      Write-Host "##vso[task.setvariable variable=MicroserviceAUpdated]True"    }}


On the Triggers tab, there is an option to specify the path to the project you want to build. When that path is specified, only commits which contain modifications that match the include/exclude rules will trigger a build.

In my case this is a much better solution than the PowerShell script which still triggered builds and releases on all the projects spamming our Slack and filling with rubbish our project's history.

enter image description here


Jayendran answer is very excellent! Here's a more PowerShell-y way to do step 2:

$editedFiles = git diff HEAD HEAD~ --name-only$editedFiles | ForEach-Object {    Switch -Wildcard ($_ ) {        'SubFolderA/*' { Write-Output "##vso[task.setvariable variable=MicroserviceA]True" }        # The rest of your path filters    }}