Conditional build solution in Azure DevOps

2020-07-28 00:10发布

问题:

I am working on a large scale application which uses multiple small-small project/solutions. Each solution is built in their respective agents. Similar to below screenshot

Now the problem is that all the projects/solutions are compiled even if a single project file is changed.

To reduce CI build time, I want to add conditional expression in the build which would build the project only if their respective source is changed.

I know this can be achieved in Azure DevOps via Custom condition using variable expressions. But I am not sure how can I check if the respective source code is changed

Does anyone know what variable expression I need to write here?

回答1:

Well as I see your are using the Git Repo, you can do this in the below way

As a very first step you need to find whether which project/solution is modified.

You can find the solution from my answer here,where you can see I've used a simple powershell script to pull out the modified files, and enables the corresponding variables.

Sample Powershell script to pull-out the modified files

$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"
    }
}


回答2:

you cant achieve that with condition. condition are for executing tasks inside build. you are looking for build triggers in yaml files:

trigger:
  branches:
    include:
      - master
  paths:
    include:
      - path\to\app\*

this build will only trigger when anything under `path\to\app\ folder was changed. that way you create a build per app and can isolate those builds to specific files changed.

https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=vsts&tabs=schema#pipeline