B.T Anand
B.T Anand

Reputation: 617

Conditional stage execution on PR raise even Azure devops

My current project structure has two folders, each having its own package.xml and individual deployment scripts.

src
src1 (added Newley)

Branch name: develop

Current policy: Runs SRC validation on a PR raise event

Problem statement: Now that I have added SRC1, I want to have a PR policy such that:

Is there a way to implement this use case in Azure DevOps/VSTS tool?

Upvotes: 0

Views: 303

Answers (2)

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30353

You can create a single build validation pipeline with multiple jobs. In this pipeline, you can run git diff --name-only HEAD HEAD~1 in a script task to get the changed folders. And then use dependencies and conditions for the multiple jobs.

See below example: There are three jobs in below yaml pipeline.

The first job GetFolder will run the git commands to get the changed folders. Then set the variable SCR1Folder to true if SCR1 folder is changed, and set the variable SCR2Folder to true if SCR2 folder is changed. See here for more information about set output variables in scripts.

The second job SRC1 is dependent on job GetFolder. And It will be executed after job GetFolder, and on the condition if variable SCR1Folder is true eq(dependencies.GetFolder.outputs['Flag.SCR1Folder'], 'true')

The third job SRC2 is dependent on job GetFolder and job "SRC1". It will be executed on the condition if variable SCR2Folder is true eq(dependencies.GetFolder.outputs['Flag.SCR2Folder'], 'true'), and it will run after job GetFolder and job "SRC1"

trigger: none

pool:
 vmImage: windows-latest

jobs:
- job: GetFolder
  steps:
  - powershell: |
      $folders= git diff --name-only HEAD HEAD~1
      $folders
      if($folders -cmatch ".*/*SCR1/.*"){
        echo "##vso[task.setvariable variable=SCR1Folder;isOutput=true]true" 
        }
      if($folders -cmatch ".*/*SCR2/.*"){
        echo "##vso[task.setvariable variable=SCR2Folder;isOutput=true]true" 
         }
    name: Flag

- job: SRC1
  dependsOn: GetFolder
  condition: and(succeeded(), eq(dependencies.GetFolder.outputs['Flag.SCR1Folder'], 'true'))
  steps:
  - powershell: echo "run src1 validation"
   
- job: SRC2
  dependsOn: 
  - GetFolder
  - SRC1
  condition: and(succeeded('GetFolder'),eq(dependencies.GetFolder.outputs['Flag.SCR2Folder'], 'true'))
  steps:
  - powershell: echo "run src2 validation"

I tested above yaml pipeline. If only src1 folder is changed. Then job GetFolder and job SRC1 will run and job SRC2 will be skiped.

If If only src2 folder is changed. Then job GetFolder and job SRC2 will run and job SRC1 will be skiped.

If both folders are changed job SRC2 will be run after job SRC1.

Upvotes: 0

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41755

The easiest way is to create 2 builds:

  1. Run src validation
  2. Run src1 validation

Create a build validation policy in the PR and filter the builds with the path filter:

enter image description here

The above its fir src, add one more with corresponding filter.

See here more info.

Upvotes: 1

Related Questions