我认为您不应该在不同的分支中使用相同的文件。如果您想要每个分支有多个构建定义,您应该为它们分别使用不同的文件。为了避免重复代码,您应该使用模板。
以下是示范如何工作:
build-and-test.yaml:
parameters:
- name: buildConfiguration
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 }}'
- 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
(您可以在上面的代码示例中找到)。
azure-pipelines.yml
的新手,我已更新了pr: none
,但它仍会触发其他分支。 - Joker Bench