我认为你不应该对不同的分支使用相同的文件。如果你想每个分支有多个构建定义,你应该为它们分开文件。为避免重复代码,您应该使用模板。
向您展示它是如何工作的:
构建和测试.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'
正如您所注意到的,每个构建都有自己的触发选项,因此您可以根据自己的需要进行自定义。我在我的博客上对此进行了描述,但以上所有内容都很好地解释了机制。
我知道它可能会引入更多文件,但我会尝试根据分支类型寻找一些模式。我可能会找到对dev
. 因此,您应该找到组并为这些组创建定义。添加不同的构建还可以帮助您识别构建的内容。否则,您必须详细了解并检查构建了哪个分支。test
beta
对于您的情况,如果您想遵循您的方法,我会尝试设置它pr: none
(您在上面的代码中有示例)。