在VSTS Pull Request

时间:2016-06-27 06:47:27

标签: git azure-devops azure-pipelines

我们正在使用带有Git和Team System Build的Visual Studio Team Systems(以前的Build vNext)。 当我们执行Pull Request时,会触发一个新的Build,用于运行Unit Tests并部署到隔离的测试系统。 要执行到隔离系统的部署,我需要在构建过程中获取真正的源分支名称。 但是Build.SourceBranchName变量始终是" merge",

E.g:

从源FOO向目标BAR拉取请求 Build.SourceBranch是" refs / pull / 1 / merge"因此Build.SourceBranchName是"合并"。 但我需要以某种方式得到" FOO"运行我的Power Shell脚本来配置系统。

有没有办法在VSTS内的Git Pull Request中获取真正的源分支名称?

5 个答案:

答案 0 :(得分:3)

没有任何变量,但你可以创建一个power-shell脚本来通过fiddle获取它。

[String]$projecturi = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"
[String]$sourcebranch = "$env:BUILD_SOURCEBRANCH"
[String]$repoid = "$env:BUILD_REPOSITORY_ID"

$username="alternativeusername"
$password="alternativepassword"

$basicAuth= ("{0}:{1}"-f $username,$password)
$basicAuth=[System.Text.Encoding]::UTF8.GetBytes($basicAuth)
$basicAuth=[System.Convert]::ToBase64String($basicAuth)
$headers= @{Authorization=("Basic {0}"-f $basicAuth)}

#get pull request ID via regex
$pullrequest = "refs/pull/+(?<pullnumber>\w+?)/merge+"
if($sourcebranch -match $pullrequest){        
        $pullrequestid = $Matches.pullnumber;
    }
else { write-host "Cannot find pull request ID" }

#get pull request information via API
$url= $projecturi + "_apis/git/repositories/" + $repoid + "/pullRequests/" + $pullrequestid + "?api-version=1.0-preview.1"

Write-Host $url

$getpullrequest = Invoke-RestMethod -Uri $url -headers $headers -Method Get

#get sourcebranch and targetbranch
$sourceref = $getpullrequest.sourceRefName
$targetref = $getpullrequest.targetRefName

答案 1 :(得分:3)

VSTS现在有System.PullRequest.SourceBranchSystem.PullRequest.TargetBranch个变量。 这应该可以解决您的问题,而无需编写任何自定义脚本

Build Variables

答案 2 :(得分:1)

您仍然不能在构建定义中为$(System.PullRequest.SourceBranch)使用BuildNumberFormat,而不会出现此错误:

  

内部版本号格式字符串$(BuildDefinitionName)-$(System.PullRequest.SourceBranch)-$(Date:yyyyMMdd)$(Rev:.r)生成内部版本号“ MyBuildName- refs / heads / myBranch -20190831.1”,其中包含无效字符,太长或结尾为“。”。

答案 3 :(得分:0)

如果在构建任务中使用,此脚本将从环境变量中读取参数,或者如果在构建任务外部使用,则使用提供的参数。变量$ sourcebranch将设置为在以后的构建任务中使用。

[CmdletBinding()]
param (
    [Parameter(Mandatory=$false)][string]$projectUri = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI",
    [Parameter(Mandatory=$false)][string]$branch = "$env:BUILD_branch",
    [Parameter(Mandatory=$false)][string]$repositoryName = "$env:BUILD_REPOSITORY_NAME",
    [Parameter(Mandatory=$false)][string]$projectName = "$env:SYSTEM_TEAMPROJECT",
    [Parameter(Mandatory=$false)][string]$oAuthToken = "$env:SYSTEM_ACCESSTOKEN",
    [Parameter(Mandatory=$false)][string]$username,
    [Parameter(Mandatory=$false)][string]$password
)

#check all parameters
if(!$oAuthToken) {
    if(!$username -or !$password) {
        throw "You must either supply an OAuth Token or a username and a password. You can supply the token via the environment variable SYSTEM_ACCESSTOKEN"
    }

    $basicAuth= ("{0}:{1}"-f $username,$password)
    $basicAuth=[System.Text.Encoding]::UTF8.GetBytes($basicAuth)
    $basicAuth=[System.Convert]::ToBase64String($basicAuth)
    $headers= @{Authorization=("Basic {0}"-f $basicAuth)}
}
else {
    $headers= @{Authorization="Bearer $oAuthToken"}
}

if(!$projectUri) {
    throw "You must supply a project uri or set the Environment variable SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"
}

if(!$branch) {
    throw "You must supply a branch or set the Environment variable BUILD_branch"
}

if(!$repositoryName) {
    throw "You must supply a repository name or set the Environment variable BUILD_REPOSITORY_NAME"
}

if(!$projectName) {
    throw "You must supply a project name or set the Environment variable SYSTEM_TEAMPROJECT"
}

#get pull request ID via regex
$pullrequest = "refs/pull/+(?<pullnumber>\w+?)/merge+"
if($branch -match $pullrequest) {        
    $pullrequestid = $Matches.pullnumber;
    Write-Output "Pull request ID is $pullrequestid"
}
else { 
    Write-Output "Cannot find pull request ID" 
}

#get pull request information via API
$url= $projectUri + "DefaultCollection/$projectName/_apis/git/repositories/$repositoryName/pullRequests/$pullrequestid\?api-version=1.0-preview.1"
Write-Output "Getting info from $url"
$getpullrequest = Invoke-RestMethod -Uri $url -headers $headers -Method Get

#get sourcebranch and targetbranch ref
$sourceref = $getpullrequest.sourceRefName
$targetref = $getpullrequest.targetRefName

#get the branch name via regex
$branchref = "refs/heads/(?<realBranchname>.*)"
if($sourceref -match $branchref) {        
    $sourcebranch = $Matches.realBranchname;
    Write-Output "Real source branch is $sourcebranch"
}
else { 
    Write-Output "Cannot find real source branch" 
}
if($targetref -match $branchref) {        
    $targetbranch = $Matches.realBranchname;
    Write-Output "Real target branch is $targetbranch"
}
else { 
    Write-Output "Cannot find real target branch" 
}

#set a variable "sourcebranch" to use it in another build task
Write-Output "##vso[task.setvariable variable=sourcebranch;]$sourcebranch"

答案 4 :(得分:0)

您可以创建一个bash脚本,将较短的分支名称分配给变量。

# Bash script
BRANCH_NAME=$(echo "$(System.PullRequest.TargetBranch)" | awk -F/ '{print $NF}')
echo "##vso[task.setvariable variable=PullRequest_Target_Branch;]$BRANCH_NAME"

然后您可以在管道中引用$(PullRequest_Target_Branch)