Git Azure管道:CI构建在合并请求源branchname后运行?

Git Azure管道:CI构建在合并请求源branchname后运行?,git,azure,continuous-integration,branch,azure-pipelines,Git,Azure,Continuous Integration,Branch,Azure Pipelines,这有点难以解释,但在Azure管道中,如果您有用于拉取请求的生成验证策略,则生成管道将运行以下变量: System.PullRequest.SourceBranch The branch that is being reviewed in a pull request. For example: refs/heads/feature/branch. System.PullRequest.TargetBranch The branch that is the target of a p

这有点难以解释,但在Azure管道中,如果您有用于拉取请求的生成验证策略,则生成管道将运行以下变量:

System.PullRequest.SourceBranch 

The branch that is being reviewed in a pull request. For example: refs/heads/feature/branch. 


System.PullRequest.TargetBranch

The branch that is the target of a pull request. For example: refs/heads/master. 
但在拉取请求完成且CI触发目标分支(refs/head/master)上的管道构建后,就无法再查看这些变量

我有一个npm包,在成功的PR合并后,我想根据PR分支是否分别以refs/feature/或refs/bugfix/开始,自动发布新的次要版本或补丁版本


如何获取目标分支上此CI构建中的源PR分支的名称。(不是PR构建验证策略)

您可以使用RESTAPI获取PR的源分支

您可以添加一个脚本任务来调用Get,并指定
searchCriteria
来过滤最新的请求。请检查以下示例:

 - powershell: |
        $url = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/git/repositories/$(Build.Repository.ID)/pullrequests?searchCriteria.targetRefName=refs/heads/master&searchCriteria.status=completed&'$top=1&api-version=5.1"
        $result = Invoke-RestMethod -Uri $url -Headers @{authorization = "Bearer $(System.AccessToken)"} -Method get
        $branches = $result.value[0]
        echo "##vso[task.setvariable variable=sourceBranch]$($branches.sourceRefName)"
上述脚本将获取合并到主分支的最新拉取请求,然后检索sourceRefName并将其设置为变量sourceBranch。因此,您可以在以下任务中引用变量$(sourceBranch)

可用于获取sourceRefName的另一个api是。因为主分支上的CI生成在PR生成之后立即触发。您可以通过
$(build.BuildId)-1
在主分支CI生成中引用PR生成id。比如说

$buildid = $(Build.BuildId)-1    

$url="$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds/$buildid?api-version=5.1"

$bresult = Invoke-RestMethod -Uri $prurl -Headers @{authorization = "Bearer $(System.AccessToken)"}  -Method get

$para = $bresult.parameters | ConvertFrom-Json

echo "##vso[task.setvariable variable=sourceBranch]$($para.'system.pullRequest.sourceBranch')"

谢谢你的回答,我试试看!