Search code examples
azureazure-devopsazure-pipelinesbuild-pipeline

Azure DevOps Pipeline double triggered when using branch policies and build validation


Since Azure DevOps Pipelines doesn't PR triggers for Azure Repos, you are supposed to use Branch Policies.

I have it setup so that you have to open a PR to merge to production and you can't push directly into production.

I also added a Build Validation to the PR. What that should do is once the PR is created, it will (eventually... baby steps) build, run units tests, integration tests, etc. If they pass, that part is checked off in the PR checklist and eventually it can be approved to production and deployed to Kubernetes.

Anyway, the issue I'm running into is with how to setup the azure-pipelines.yaml. What is currently happening is as soon as the short-lived branch is pushed to remote (git push origin test-branch) it triggers the pipeline and then it is triggered again when the PR request is created. I only want it to run when the PR is created and I'm not seeing how to setup the azure-pipelines.yaml to do this.

This is what I'm testing with:

trigger:
  branches:
    exclude:
    - production
  paths:
    include:
    - admin
    - admin-v2
    - api
    - client
    - k8s
    - templates
    - azure-pipelines.yaml

resources:
- repo: self

variables:

  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: '<GUID>'
  imageRepository: 'apptest'
  containerRegistry: 'apptestacr.azurecr.io'
  dockerfilePath: '$(Build.SourcesDirectory)'
  tag: '$(Build.BuildId)'
  imagePullSecret: 'apptestacr8a25-auth'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

stages:
- stage: Changes
  jobs:
  - job: Changes
    steps:
    - bash: |
        changedServices=$(git diff $(git branch --show-current) production --name-only | awk -F'/' 'NF!=1{print $1}' | sort -u)
        echo $changedServices

It is easy to see why it triggers on git push: it is looking at every branch except production. I also tried removing the trigger section altogether and it still does the same thing.

This is what I have for the Build Validation settings:

enter image description here

How should I set it up to accomplish this functionality (only triggering on PR with the Build Validation)?


Solution

  • Ok, it looks like the following did the job:

    trigger: none
    

    The documentation on it: Opting out of CI.

    It didn't trigger when I pushed the changes to the repo, but did when I opened a PR.