Jok*_*nch 6 git azure azure-devops azure-pipelines azure-pipelines-yaml
我正在尝试创建一个 azure 管道,该管道在特定分支中的更改时触发但排除其他分支。示例场景是,我有多个分支,例如dev
, test
,beta
分支。所以我在我的azure-pipelines.yml
trigger:
branches:
include:
- dev
exclude:
- test
- beta
Run Code Online (Sandbox Code Playgroud)
它工作正常,它dev
在我的 azure devops 中触发了 CI 构建。但问题是,在我切换到机器上的其他分支后,假设test
我更新了azure-pipelines.yml
这样的内容:
trigger:
branches:
include:
- test
exclude:
- dev
- beta
Run Code Online (Sandbox Code Playgroud)
并将其推送到原点,CI fortest
和dev
are trigger 并且它们的分支标记在test
分支中。这是错误的,因为dev
不应包含或触发CI 。
我还确保每个 CI 构建都使用azure-pipelines.yml
指定给特定分支的那个。这就是为什么我不知道为什么它不能正常工作。
我认为您不应该对不同的分支使用相同的文件。如果你想每个分支有多个构建定义,你应该为它们分开文件。为了避免重复代码,您应该使用模板。
向您展示它是如何工作的:
构建和测试.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)
Run Code Online (Sandbox Code Playgroud)
然后如果您的管道只想使用这些步骤,您应该使用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)
Run Code Online (Sandbox Code Playgroud)
或者如果您想执行其他步骤,请使用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'
Run Code Online (Sandbox Code Playgroud)
正如您所注意到的,每个构建都有自己的触发选项,因此您可以根据自己的需要进行自定义。我在我的博客上对此进行了描述,但以上所有内容都很好地解释了机制。
我知道它可能会引入更多文件,但我会尝试根据分支类型寻找一些模式。我可能会找个理由有一些不同的行为dev
,test
并beta
但我真的怀疑,每一个特性分支会比另一个特性分支不同。因此,您应该找到组并为这些组创建定义。添加不同的构建还可以帮助您识别构建的内容。否则,您必须详细了解并检查构建了哪个分支。
对于您的情况,如果您想遵循您的方法,我会尝试设置它pr: none
(您在上面的代码中有示例)。
归档时间: |
|
查看次数: |
2942 次 |
最近记录: |