2016-06-27 3 views
1

우리는 Git 및 Team System Build (이전 Build vNext)와 함께 Visual Studio Team Systems을 사용하고 있습니다. 풀 요청을 수행하면 단위 테스트를 실행하고 격리 된 테스트 시스템에 배포하는 데 사용되는 새 빌드가 트리거됩니다. 격리 된 시스템에 배포를 수행하려면 빌드 프로세스에서 실제 소스 분기 이름을 가져와야합니다. Build.SourceBranchName 변수가 항상 그러나 "병합"VSTS 끌어 오기 요청에서 실제 원본 분기 이름 얻기

예컨대 : BAR Build.SourceBranch를 타겟팅 소스 FOO에서

풀 요청 "심판이// 1/병합 풀"따라서 Build.SourceBranchName가 "병합"이다. 하지만 필자는 Power Shell 스크립트를 실행하여 시스템을 구성하기 위해 "FOO"를 얻을 필요가 있습니다.

VSTS 내부의 근력 끌기 요청 안에 실제 원본 분기 이름을 가져 오는 방법이 있습니까?

답변

2

아무 변수도 없지만 전원 쉘 스크립트를 만들어 Rest API을 통해 가져올 수 있습니다. 빌드 작업 내에서 사용 또는 빌드 작업을 외부에서 사용할 경우 제공된 매개 변수를 사용하는 경우

[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 
+0

감사를 작성하지 않고 문제를 해결해야 는 스크립트가 마법처럼 작동합니다. 조금 수정하여 디버깅이 쉬우 며 나중에 빌드 작업에 사용할 변수를 설정합니다. – wertzui

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" 
2

VSTS는 이제 System.PullRequest.SourceBranchSystem.PullRequest.TargetBranch 변수가 있습니다. 사용자 정의 스크립트

Build Variables

관련 문제