Triggering Azure DevOps builds based on changes to sub folders
You can do like below
- Create variables based on your microservices with the values "False"
E.g,MicroserviceAUpdated
= "False",MicroserviceBUpdated
= "False" etc.,
- 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 any files are changed underSubFolderA
. - Update the
MicroserviceBUpdated
variable to "true" if only any
files are changed underSubFolderA
.
So on....
- 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.Length
echo "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.