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



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

我认为不应该将同一个文件用于不同的分支。如果您希望每个分支有多个生成定义,则应该为它们创建单独的文件。为了避免重复代码,您应该使用模板。

向您展示它的工作原理:

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(您在上面的代码中有一个示例(。

最新更新