3

I wanted to set a condition based on the committed paths in Job level? Within my pipeline, I only want to run a particular job if a certain folder has been committed.

I have defined a path filter in my pipeline trigger

trigger:
  branches:
    include:
    - DevopsTest
  paths:
    include:
    - Test/*
    - Prod/*

Based on the committed path I wanted to set a condition to invoke respective job to pass variables into my script inorder to deploy. If committed path is Test run the Test job and skip the other and vice versa.

Based on the This Approach i have made the changes to my Yaml.

variables:
- group: Test
- group: Prod
stages:
- stage: CICDPolicyDeployment
  jobs: 
  - job: CICDFlow
    steps:
      - task: PowerShell@2
        name: folderupdate
        inputs:
          targetType: 'inline'
          script: |
            $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 "Test/*")
                {
                  Write-Host "##vso[task.setvariable variable=TestUpdate]True"
                }
            }
    

      - job: Test
        condition: and(succeeded(), eq(variables['TestUpdate'], 'True'))
        steps:
          - task: AzurePowerShell@5
            inputs:
             azureSubscription: 'test'
             ScriptType: 'FilePath'
             ScriptPath: '.\scripts\pipelinescript.ps1'
             ScriptArguments: -NAME $(testtname) -API_KEY $(testapikey) -DEPLOY_KEY $(testdeploykey)
             azurePowerShellVersion: 'LatestVersion'

      - job: Prod
        condition: and(succeeded(), eq(variables['ProdUpdate'], 'True'))
        steps:
          - task: AzurePowerShell@5
            inputs:
             azureSubscription: 'prod'
             ScriptType: 'FilePath'
             ScriptPath: '.\scripts\pipelinescript.ps1'
             ScriptArguments: -NAME $(Prodtname) -API_KEY $(Prodapikey) -DEPLOY_KEY $(proddeploykey)
             azurePowerShellVersion: 'LatestVersion'

However the If condition in Inline script is not getting matched with committed path and not setting the value to true

is there something i'm not doing right? or am i missing something here

Nish
  • 69
  • 6
  • Does this answer your question? [Triggering Azure DevOps builds based on changes to sub folders](https://stackoverflow.com/questions/53227343/triggering-azure-devops-builds-based-on-changes-to-sub-folders) – Krzysztof Madej Sep 29 '21 at 08:35
  • @KrzysztofMadej I have changed the Yaml code towards the Approach, However still something is not right – Nish Sep 29 '21 at 16:51

1 Answers1

0

There is not out of the box solutions for this approach. You need to scan changes first and then set variables and based on them create condition. You can check it here.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • Thanks, However I have reviewed the Approach and even tried to scan the changes first and then setting variables and based on them created condition. However it still failing to meet the condition. Is there any other way or any references – Nish Sep 29 '21 at 15:31
  • By tweaking on above solution it worked form me now. – Nish Sep 29 '21 at 19:17