3

I have multiple projects under a solution,

ProjectA.csproj
projectB.csproj
projectC.csproj

I have created a YAML CI build pipeline for this solution with trigger from Master Branch

"trigger: - Master"

Whenever a check-in happens to Master for any of the project above, it triggers the CI pipeline and create artifacts for all the above individual projects.

Question - can I only build projects which have changes using the same single YAML file for the solution?

Anish
  • 103
  • 2
  • 5

2 Answers2

5

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.

LoLance
  • 25,666
  • 1
  • 39
  • 73
  • 1
    Is this really the best answer? It seems inelegant. Is there a way to trigger on a particular directory change in a git repo? – N-ate Mar 05 '21 at 03:29
1

Add the allowPackageConflicts input. It allows publish of just a single project in the solution. Only projects with an updated version number is published.

# publish to artifacts:
- task: NuGetCommand@2
  inputs:
    command: 'push'
    allowPackageConflicts: true
WhiteSpot
  • 31
  • 1
  • 1
  • 5