使用分支策略和构建验证时,Azure DevOps Pipeline 双触发

问题描述

由于 Azure DevOps Pipelines 没有针对 Azure Repos 的 PR 触发器,您应该使用 Branch Policies

我已经设置好了,所以您必须打开 PR 才能合并到 production,而您不能直接推入 production

我还在 PR 中添加Build Validation。应该做的是,一旦 PR 被创建,它将(最终......小步)构建、运行单元测试、集成测试等。如果它们通过,那部分在 PR 清单中被选中,最终它可以是批准到 production 并部署到 Kubernetes。

无论如何,我遇到的问题是如何设置 azure-pipelines.yaml。当前发生的情况是,只要将短期分支推送到远程 (git push origin test-branch),它就会触发管道,然后在创建 PR 请求时再次触发。我只希望它在创建 PR 时运行,但我没有看到如何设置 azure-pipelines.yaml 来执行此操作。

这是我正在测试的内容

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

很容易理解为什么它会在 git push 上触发:它会查看除 production 之外的每个分支。我还尝试完全删除 trigger 部分,但它仍然执行相同的操作。

这是我的构建验证设置:

enter image description here

我应该如何设置它以完成此功能(仅在使用构建验证的 PR 上触发)?

解决方法

好的,看起来下面的工作完成了:

trigger: none

关于它的文档:Opting out of CI

当我将更改推送到 repo 时它没有触发,但在我打开 PR 时触发了。