I have an Azure DevOps pipeline that should trigger after a merge to main only if changes occured under a certain path.
trigger:
branches:
include:
- main
paths:
include:
- 'Infrastructure/Some.Service.Api.Client/*'
My problem is that this will also trigger on pull request builds which do not originate in main. This pipeline generates a new version of an API client for a service. I only want to create a new version, if the code on main has changed.
How do I configure the trigger correctly?
If the PR trigger is not expected, you can disable it:
If you Git repository is on Azure DevOps, disable or remove the Build validation set on the branch policies of main
branch.
If the Git repository is not on Azure DevOps, such as on GitHub or Bitbucket, you need to do like as below.
pr: none
".
pr: none
trigger:
branches:
include:
- main
paths:
include:
- 'Infrastructure/Some.Service.Api.Client/*'
main
branch.If the PR trigger is expected, you can use the predefined variable "Build.SourceBranch" to identify whether the pipeline is run for main
branch or a PR.
main
branch, its value is "refs/heads/main
".refs/pull/<PR-Id>/merge
".So, you use this variable to set a condition for the step which to generate the new version number.
refs/heads/main
", execute the step.refs/heads/main
", skip the step.trigger:
branches:
include:
- main
paths:
include:
- 'Infrastructure/Some.Service.Api.Client/*'
steps:
- ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/main') }}:
# The step to generate the new version number.
- task: xxxx
displayName: 'Generate Version Number'
. . .
# Some other steps need to run always.
- task: xxxx
. . .