Question - can I only build projects which have changes using the same
single YAML file for the solution?
Yes, you can. Assuming you have multiple jobs/steps for different projects, you can use Conditions to determine when it should run/skip some specific steps. You can check this example:
trigger:
- master
pool:
vmImage: 'windows-latest'
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$files=$(git diff HEAD HEAD~ --name-only)
$temp=$files -split ' '
$count=$temp.Length
For ($i=0; $i -lt $temp.Length; $i++)
{
$name=$temp[$i]
echo "this is $name file"
if ($name -like "ProjectA/*")
{
Write-Host "##vso[task.setvariable variable=RunProjectA]True"
}
if ($name -like "ProjectB/*")
{
Write-Host "##vso[task.setvariable variable=RunProjectB]True"
}
if ($name -like "ProjectC/*")
{
Write-Host "##vso[task.setvariable variable=RunProjectC]True"
}
}
- script: echo "Run this step when ProjectA folder is changed."
displayName: 'Run A'
condition: eq(variables['RunProjectA'], 'True')
- script: echo "Run this step when ProjectB folder is changed."
displayName: 'Run B'
condition: eq(variables['RunProjectB'], 'True')
- script: echo "Run this step when ProjectC folder is changed."
displayName: 'Run C'
condition: eq(variables['RunProjectC'], 'True')
Then if we only have changes in ProjectA folder, only those steps/tasks with condition: eq(variables['RunProjectA'], 'True') will run. You should have three separate build tasks in your pipeline for ProjectA, ProjectB and ProjectC, and give them your custom condition, then you can only build projects which have changes... (Hint from Jayendran in this issue!)
Hope it works.