[英]How to Trigger a build pipeline depends on create PR request source branch in Azure DevOps
[英]Azure DevOps - Pipeline should not trigger build for PR
我有一个 Git 存储库的 Azure DevOps 管道。 我目前有一个脚本来验证 Azure 管道中的 PR 评论。
当代码合并到主分支时,我想触发构建。 我不确定如何使用 Azure DevOps 管道来实现这一点。
#Trigger for Development
trigger:
branches:
include:
- development
- master
#Trigger checks for PR
pr:
branches:
include:
- development
- master
- feature
- main
paths:
exclude:
- README/*
当代码合并到主分支时,我想触发构建
如果要在代码合并到主分支后验证注释,我们需要在 PR 完成后而不是在创建 PR 时触发构建。
因此,在这种情况下, PR 触发器无法满足我们的要求。
为了解决这个问题,我们可以使用 ** condition** eq(variables['Commitcomment'], 'Merge pull request')
为主分支启用 CI 触发器,以执行脚本任务以验证 PR 评论。
在这种情况下,只有当Commitcomment
为Merge pull request
时,管道才会执行作业,这样可以过滤掉 PR 没有做的修改。
要获取变量Commitcomment
的值,我们可以通过变量Build.SourceVersionMessage
检查 github 上的提交消息:
如果提交来自 PR,它会给出一个默认注释,以: Merge pull request xxx
开头,我们可以添加一个 bash\powershell 脚本来获取前几个字段。
如果前几个字段是Merge pull request
,则使用Logging Command将变量Commitcomment
设置为 true :
- task: CmdLine@2
displayName: get the first few fields
inputs:
script: >-
echo $(Build.SourceVersionMessage)
set TempVar=$(Build.SourceVersionMessage)
set Commitcomment=%TempVar:~0,18%
echo %Commitcomment%
echo ##vso[task.setvariable variable=Commitcomment]%Commitcomment%
参考链接: Azure Devops 中是否有 $(SourceVersion) 的短 7 位版本?
然后将此变量添加为条件condition: and(succeeded(), eq(variables['Commitcomment'], 'Merge pull request'))
为您的任务验证 PR 评论:
- task: CmdLine@2
displayName: script to validate the PR comments
condition: and(succeeded(), eq(variables['Commitcomment'], 'Merge pull request'))
inputs:
script: >
echo To validate the PR comments
在这种情况下,如果提交不是来自 PR,它将跳过 PR 评论验证任务:
如果您只想在特定分支中完成合并(拉取请求验证)后启动构建,那么您的代码很好。
如果您当前要运行验证版本,它未集成到 Yaml 管道配置中( https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema %2C 参数模式#pr-trigger )
为此,必须通过图形界面完成:项目设置 -> 存储库 -> Select 你的仓库 -> 策略 -> 分支策略 -> Select 你的分支 -> 构建验证 -> + -> 添加构建信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.