【问题标题】:Trigger CI specific branch and exclude other branches in Azure-Pipelines.YML Problem触发 CI 特定分支并排除 Azure-Pipelines.YML 中的其他分支问题
【发布时间】:2024-05-15 21:50:01
【问题描述】:

我正在尝试创建一个 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。这就是为什么我不知道为什么它不能正常工作。

【问题讨论】:

    标签: git azure azure-devops azure-pipelines azure-pipelines-yaml


    【解决方案1】:

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

    向您展示它是如何工作的:

    构建和测试.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'
    

    您注意到每个构建都有自己的触发选项,因此您可以根据需要进行自定义。我在blog 上对此进行了描述,但以上所有内容都很好地解释了机制。

    我知道它可能会引入更多文件,但我会尝试根据分支类型寻找一些模式。我可能会为devtestbeta 找到一些不同行为的理由,但我真的怀疑每个单独的功能分支都会与另一个功能分支不同。因此,您应该找到组并为这些组创建定义。添加不同的构建还可以帮助您识别构建的内容。否则,您必须深入了解并检查构建的分支。

    对于您的情况,如果您想遵循您的方法,我会尝试设置此pr: none(您在上面的代码中有示例)。

    【讨论】:

    • 感谢您的建议。但是当我将更改推送到原点时,我仍然无法弄清楚如何仅触发特定分支。我是使用azure-pipelines.yml 的新手,我更新了pr: none,但它仍然会触发其他分支。
    • 您需要很少的构建定义。构建-dev.yaml 和构建-test.yaml。将它们全部放在您的主分支中。您可以简单地复制粘贴已有的内容。然后在 Azure DevOps 上创建一个管道,选择这个新文件。