在Azure-Pipelines.YML中触发特定分支的CI,并排除其他分支问题。

7
我正在尝试创建一个Azure管道,该管道会在特定分支的更改时触发,但不包括其他分支。示例场景是,我有多个分支,例如devtestbeta等分支。因此,我在我的azure-pipelines.yml中有这个示例配置。
trigger:
  branches: 
    include: 
      - dev
    exclude:
      - test
      - beta

它可以正常工作,并在我的 Azure DevOps 中触发“dev” 的 CI 构建。但问题是,当我在我的计算机上切换到其他分支,比如“test”,并像这样更新了“azure-pipelines.yml”:

trigger:
  branches: 
    include: 
      - test
    exclude:
      - dev
      - beta

并将其推送到源,触发了testdev的CI,并且它们的分支标签位于test分支中。这是错误的,因为不应该包括或触发dev的CI。
我还确保每个CI构建都使用指定给特定分支的azure-pipelines.yml。这就是为什么我不知道为什么它不能正常工作的原因。
1个回答

8

我认为您不应该在不同的分支中使用相同的文件。如果您想要每个分支有多个构建定义,您应该为它们分别使用不同的文件。为了避免重复代码,您应该使用模板。

以下是示范如何工作:

build-and-test.yaml:

parameters:
- name: buildConfiguration # name of the parameter; required
  default: 'Release'

steps:

    - task: DotNetCoreCLI@2
      displayName: Restore nuget packages
      inputs:
        command: restore
        projects: '**/*.csproj'
        workingDirectory: $(Build.SourcesDirectory)
    
    - task: DotNetCoreCLI@2
      displayName: Build
      inputs:
        command: build
        projects: '$(Build.SourcesDirectory)/gated-checkin/GatedCheckin.sln'
        arguments: '--configuration ${{ parameters.buildConfiguration }}'
    
    # You just added coverlet.collector to use 'XPlat Code Coverage'
    - task: DotNetCoreCLI@2
      displayName: Test
      inputs:
        command: test
        projects: '**/*Tests/*.csproj'
        arguments: '--configuration ${{ parameters.buildConfiguration }} --collect:"XPlat Code Coverage" -- RunConfiguration.DisableAppDomain=true'
        workingDirectory: $(Build.SourcesDirectory)

如果您的流程仅需要使用这些步骤,则应使用extends关键字:

trigger:
  branches:
    include:
    - '*'
    exclude:
    - master

pr:
  branches:
    include:
    - master
  paths:
    include:
    - gated-checkin-with-template/*
    exclude:
    - gated-checkin-with-template/azure-pipelines.yml

variables:
  buildConfiguration: 'Release'

extends:
  template: build-and-test.yaml
  parameters:
      buildConfiguration: $(buildConfiguration)

或者如果您需要进一步的步骤,请使用template关键字:

trigger:
  branches:
    include:
    - master
  paths:
    include:
    - gated-checkin-with-template/*
    exclude:
    - gated-checkin-with-template/azure-pipelines-gc.yml

pr: none

pool:
  vmImage: 'ubuntu-latest'

variables:
  buildConfiguration: 'Release'

steps:

- template: build-and-test.yaml
  parameters:
      buildConfiguration: $(buildConfiguration)

- script: echo Some steps to create artifacts!
  displayName: 'Run a one-line script'

正如您所注意到的,每个构建都有其自己的触发选项,因此您可以根据自己的需要进行定制。我在我的博客中描述了这一点,但以上所有内容都很好地解释了机制。
我知道这可能会引入更多文件,但我会尝试寻找一些不同于其他分支类型的模式。我可能会找到一些原因,为devtestbeta设置一些不同的行为,但我真的怀疑每个单独的特性分支都会与另一个特性分支不同。因此,您应该找到组并为这些组创建定义。添加不同的构建也有助于您识别何时进行了构建。否则,您必须进入细节并检查构建了哪个分支。
对于您的情况,如果您要遵循自己的方法,我建议尝试设置pr: none(您可以在上面的代码示例中找到)。

谢谢您的建议。但我仍然无法弄清楚如何在将更改推送到源时仅触发特定分支。我是使用 azure-pipelines.yml 的新手,我已更新了 pr: none,但它仍会触发其他分支。 - Joker Bench
您需要几个构建定义。其中包括build-dev.yaml和build-test.yaml。将它们全部放在主分支中。您可以直接复制粘贴已有的内容。然后,在Azure DevOps上创建一个流水线,选择这些新文件。 - Krzysztof Madej

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接