Home > Enterprise >  azure pipeline - set a variable based on the trigger path
azure pipeline - set a variable based on the trigger path

Time:09-07

I am using Azure Pipelines and have the following situation:

I have one repository with the folders A and B (and many more in the future). If I run the pipeline manually, I do chose A or B and the pipeline does stuff with the files in that folder. This is fine for my branches, but I also want an automatic trigger for my main branch.

How do I get a variable that depends on the path of the code change?

My trigger looks something like this:

trigger:
  branches:
    include:
      - main
  paths:
    include:
      - abc/xyz/A/*
      - abc/xyz/B/*

And all I need is a variable that is A or B now, depending on the path where the change occured. (Ideally if there are changes in both folders the pipeline should trigger twice, but this is the next problem I have to take on)

CodePudding user response:

trigger:
  branches:
    include:
      - main
      - dev
  paths:
    exclude:
      - azure-pipelines.yml
      - TfCodeandPlanValidation.yml
      - 'README.md'
  
variables:
  - name: tf_version
    value: latest
  - name: variablegroupname
    ${{ if eq(variables['Build.SourceBranchName'], 'main') }}:
      value: kv-Prod
    ${{ if eq(variables['Build.SourceBranchName'], 'dev') }}:
      value: kv-Dev
  - name: env
    ${{ if eq(variables['Build.SourceBranchName'], 'main') }}:
      value: prod
    ${{ if eq(variables['Build.SourceBranchName'], 'dev') }}:
      value: dev

Here I have mentioned it in the variable section.

CodePudding user response:

There is no direct way to achieve this.

As a work around, you could use git command to get the commit message from the code change in A and B, and set the commit message as variable.

git diff-tree --no-commit-id --name-only -r $(Build.SourceVersion)

If the changes is from Folder A. Set "FolderAUpdated" to ture

Write-Host "##vso[task.setvariable variable=FolderAUpdated]true"

And set conditions to check the variable value

"Custom conditions": and(succeeded(), eq(variables['FolderAUpdated'], 'true'))

"(Ideally if there are changes in both folders the pipeline should trigger twice, but this is the next problem I have to take on)"

A good option for this would create two individual pipelines, if both folders have changes, two pipelines will be triggered.

Pipeline A

trigger:
  branches:
    include:
      - main
  paths:
    include:
      - abc/xyz/A/*

Pipeline B

trigger:
  branches:
    include:
      - main
  paths:
    include:
      - abc/xyz/B/*
  • Related