触发下一个管道阶段,分配的分支已完成拉动请求
我的YAML管道有3个部署阶段:
Alpha servers -> Beta servers -> Production servers
我有3个分支。一种用于Alpha代码,一个用于Beta,另一个用于生产。我要实现的是在YAML中自动化以下工作流程(我已经为Alpha分支设置了一个触发器,但这是关于以后阶段的问题):
Changes merged to the alpha branch after a pull request is completed -> alpha branch code build -> deployment to alpha servers -> *manual testing performed on alpha servers* -> only these specific changes merged from alpha to beta branch -> beta branch code build -> deployment to beta servers -> *manual testing performed on beta servers* -> only these specific changes merged from beta to main branch -> main branch code build -> deployment to production servers
我不知道如何在beta和生产阶段运行时如何运行更改已合并到Beta和主要分支。如果我声明所有3个分支的触发器(Alpha,beta,main),它将每次创建一个新的管道运行,因此不要进行特定更改的1次运行:
alpha(run after merge to alpha branch) -> beta(run after merge to beta branch) -> prod(run after merge to main branch)
我正在使用alpha,beta,prod部署的条件来检查哪个分支已经合并到,但我最终得到了3次跑步:
alpha(run after merge to alpha branch) -> beta(did not run) -> prod(did not run)
alpha(did not run) -> beta(run after merge to beta branch) -> prod(did not run)
alpha(did not run) -> beta(did not run) -> prod(run after merge to main branch)
我的管道:
variables:
- name: app_name
value: 'myapp'
trigger:
batch: true
branches:
include:
- alpha
- beta
stages:
- stage: buildAlpha
condition: and(eq(variables['Build.Reason'], 'IndividualCI'), eq(variables['Build.SourceBranch'], 'refs/heads/alpha'))
pool:
vmImage: windows-2022
jobs:
- job: compile
steps:
- checkout: git://MyTeam/MyProjectTest@alpha
- task: UseDotNet@2
displayName: 'Use .NET Core 3.1'
inputs:
packageType: 'sdk'
version: '3.1.x'
- task: NuGetToolInstaller@0
displayName: 'Use NuGet 6.0.0'
inputs:
versionSpec: 6.0.0
- task: NuGetCommand@2
displayName: 'NuGet restore'
inputs:
restoreSolution: 'MyProject\MyProject.sln'
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: false
projects: 'MyProject\MyProject.sln'
arguments: '-o $(build.artifactstagingdirectory)'
modifyOutputPath: false
zipAfterPublish: true
- publish: '$(build.artifactstagingdirectory)'
artifact: drop
- stage: deployDev
condition: and(eq(variables['Build.Reason'], 'IndividualCI'), eq(variables['Build.SourceBranch'], 'refs/heads/alpha'))
jobs:
- deployment: deployToAlpha
variables:
- group: DevVariables
environment:
name: dev
resourceType: VirtualMachine
tags: web
strategy:
runOnce:
deploy:
steps:
- task: IISWebAppDeploymentOnMachineGroup@0
displayName: 'IIS Web App Deploy'
inputs:
WebSiteName: 'myapp.og.com'
VirtualApplication: $(app_name)
RemoveAdditionalFilesFlag: true
TakeAppOfflineFlag: True
XmlVariableSubstitution: True
Package: '$(Pipeline.Workspace)/drop/**/*.zip'
- task: qetza.replacetokens.replacetokens-task.replacetokens@4
displayName: 'Replace tokens in **/appsettings.json'
inputs:
targetFiles: 'F:/Websites/myapp.og.com/$(app_name)/appsettings.json'
actionOnMissing: fail
keepToken: true
actionOnNoFiles: fail
enableTelemetry: false
- stage: buildBeta
condition: and(eq(variables['Build.Reason'], 'IndividualCI'), eq(variables['Build.SourceBranch'], 'refs/heads/beta'))
pool:
vmImage: windows-2022
jobs:
- job: compile
steps:
- checkout: git://MyTeam/MyProjectTest@beta
- task: UseDotNet@2
displayName: 'Use .NET Core 3.1'
inputs:
packageType: 'sdk'
version: '3.1.x'
- task: NuGetToolInstaller@0
displayName: 'Use NuGet 6.0.0'
inputs:
versionSpec: 6.0.0
- task: NuGetCommand@2
displayName: 'NuGet restore'
inputs:
restoreSolution: 'MyProject\MyProject.sln'
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: false
projects: 'MyProject\MyProject.sln'
arguments: '-o $(build.artifactstagingdirectory)'
modifyOutputPath: false
zipAfterPublish: true
- publish: '$(build.artifactstagingdirectory)'
artifact: drop
- stage: deployBeta
condition: and(eq(variables['Build.Reason'], 'IndividualCI'), eq(variables['Build.SourceBranch'], 'refs/heads/beta'))
jobs:
- deployment: deployToBeta
variables:
- group: betavariables
environment:
name: beta
resourceType: VirtualMachine
tags: web
strategy:
runOnce:
deploy:
steps:
- task: IISWebAppDeploymentOnMachineGroup@0
displayName: 'IIS Web App Deploy'
inputs:
WebSiteName: 'myapp.og.com'
VirtualApplication: $(app_name)
RemoveAdditionalFilesFlag: true
TakeAppOfflineFlag: True
XmlVariableSubstitution: True
Package: '$(Pipeline.Workspace)/drop/**/*.zip'
- task: qetza.replacetokens.replacetokens-task.replacetokens@4
displayName: 'Replace tokens in **/appsettings.json'
inputs:
targetFiles: 'F:/Websites/myapp.og.com/$(app_name)/appsettings.json'
actionOnMissing: fail
keepToken: true
actionOnNoFiles: fail
enableTelemetry: false
My YAML pipeline has 3 deployment stages:
Alpha servers -> Beta servers -> Production servers
I have 3 branches. One for alpha code, one for beta, and one for production. What I want to achieve is to automate the following workflow in YAML(I've already set a trigger for the alpha branch, but it's a question about later stages):
Changes merged to the alpha branch after a pull request is completed -> alpha branch code build -> deployment to alpha servers -> *manual testing performed on alpha servers* -> only these specific changes merged from alpha to beta branch -> beta branch code build -> deployment to beta servers -> *manual testing performed on beta servers* -> only these specific changes merged from beta to main branch -> main branch code build -> deployment to production servers
I can't figure out how to make beta and production stages run when changes are merged to the beta and main branches. If I declare triggers for all 3 branches (alpha, beta, main) it will create a new pipeline run every time so instead of having 1 run with specific changes:
alpha(run after merge to alpha branch) -> beta(run after merge to beta branch) -> prod(run after merge to main branch)
I'm using conditions for alpha, beta, prod deployments to check which branch has been merged to but I end up with 3 runs:
alpha(run after merge to alpha branch) -> beta(did not run) -> prod(did not run)
alpha(did not run) -> beta(run after merge to beta branch) -> prod(did not run)
alpha(did not run) -> beta(did not run) -> prod(run after merge to main branch)
My pipeline:
variables:
- name: app_name
value: 'myapp'
trigger:
batch: true
branches:
include:
- alpha
- beta
stages:
- stage: buildAlpha
condition: and(eq(variables['Build.Reason'], 'IndividualCI'), eq(variables['Build.SourceBranch'], 'refs/heads/alpha'))
pool:
vmImage: windows-2022
jobs:
- job: compile
steps:
- checkout: git://MyTeam/MyProjectTest@alpha
- task: UseDotNet@2
displayName: 'Use .NET Core 3.1'
inputs:
packageType: 'sdk'
version: '3.1.x'
- task: NuGetToolInstaller@0
displayName: 'Use NuGet 6.0.0'
inputs:
versionSpec: 6.0.0
- task: NuGetCommand@2
displayName: 'NuGet restore'
inputs:
restoreSolution: 'MyProject\MyProject.sln'
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: false
projects: 'MyProject\MyProject.sln'
arguments: '-o $(build.artifactstagingdirectory)'
modifyOutputPath: false
zipAfterPublish: true
- publish: '$(build.artifactstagingdirectory)'
artifact: drop
- stage: deployDev
condition: and(eq(variables['Build.Reason'], 'IndividualCI'), eq(variables['Build.SourceBranch'], 'refs/heads/alpha'))
jobs:
- deployment: deployToAlpha
variables:
- group: DevVariables
environment:
name: dev
resourceType: VirtualMachine
tags: web
strategy:
runOnce:
deploy:
steps:
- task: IISWebAppDeploymentOnMachineGroup@0
displayName: 'IIS Web App Deploy'
inputs:
WebSiteName: 'myapp.og.com'
VirtualApplication: $(app_name)
RemoveAdditionalFilesFlag: true
TakeAppOfflineFlag: True
XmlVariableSubstitution: True
Package: '$(Pipeline.Workspace)/drop/**/*.zip'
- task: qetza.replacetokens.replacetokens-task.replacetokens@4
displayName: 'Replace tokens in **/appsettings.json'
inputs:
targetFiles: 'F:/Websites/myapp.og.com/$(app_name)/appsettings.json'
actionOnMissing: fail
keepToken: true
actionOnNoFiles: fail
enableTelemetry: false
- stage: buildBeta
condition: and(eq(variables['Build.Reason'], 'IndividualCI'), eq(variables['Build.SourceBranch'], 'refs/heads/beta'))
pool:
vmImage: windows-2022
jobs:
- job: compile
steps:
- checkout: git://MyTeam/MyProjectTest@beta
- task: UseDotNet@2
displayName: 'Use .NET Core 3.1'
inputs:
packageType: 'sdk'
version: '3.1.x'
- task: NuGetToolInstaller@0
displayName: 'Use NuGet 6.0.0'
inputs:
versionSpec: 6.0.0
- task: NuGetCommand@2
displayName: 'NuGet restore'
inputs:
restoreSolution: 'MyProject\MyProject.sln'
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: false
projects: 'MyProject\MyProject.sln'
arguments: '-o $(build.artifactstagingdirectory)'
modifyOutputPath: false
zipAfterPublish: true
- publish: '$(build.artifactstagingdirectory)'
artifact: drop
- stage: deployBeta
condition: and(eq(variables['Build.Reason'], 'IndividualCI'), eq(variables['Build.SourceBranch'], 'refs/heads/beta'))
jobs:
- deployment: deployToBeta
variables:
- group: betavariables
environment:
name: beta
resourceType: VirtualMachine
tags: web
strategy:
runOnce:
deploy:
steps:
- task: IISWebAppDeploymentOnMachineGroup@0
displayName: 'IIS Web App Deploy'
inputs:
WebSiteName: 'myapp.og.com'
VirtualApplication: $(app_name)
RemoveAdditionalFilesFlag: true
TakeAppOfflineFlag: True
XmlVariableSubstitution: True
Package: '$(Pipeline.Workspace)/drop/**/*.zip'
- task: qetza.replacetokens.replacetokens-task.replacetokens@4
displayName: 'Replace tokens in **/appsettings.json'
inputs:
targetFiles: 'F:/Websites/myapp.og.com/$(app_name)/appsettings.json'
actionOnMissing: fail
keepToken: true
actionOnNoFiles: fail
enableTelemetry: false
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的情况下,拉动请求合并在三个分支中触发三个管道运行。这是一种预期的行为。
在YAML管道中,触发器只能设置为根级别。它可以在整个管道上工作。因此,三个PR合并将触发三个管道运行,并且它们是独立的。
当PR合并触发管道时,它将根据条件运行匹配阶段。
变量将以相同的值进行评估,因此有些阶段将运行,而其他阶段将跳过。
对于您的要求:三个公关合并触发同一管道运行的不同阶段,恐怕没有盒子外的方法可以满足您的要求。
对于解决方法,您可以根据阶段分为三个管道,并为每个管道设置相应的触发器。
我建议您可以创建建议反馈报告功能需求要求 。
In your scenario, Pull Request merges in three branches trigger three pipeline runs. This is an expected behavior.
In Yaml Pipeline, the trigger can only be set at the root level. It works on the entire pipeline. So three PR merges will trigger three pipeline runs, and they are independent。
When PR merge triggers the pipeline, it will run the matching stage based on the condition.
Variables will be evaluated with the same value, so some stages will run and others will skip.
For your requirement: three PR merges trigger different stages of the same pipeline run , I am afraid that there is no out-of-box method can meet your requirement.
For a workaround, you can split into three pipelines according to the stages and set the corresponding trigger for each pipeline.
I suggest that you can create a suggestion feedback to report the feature requirement.