对于 VSTS 构建,您可以添加 PowerShell 脚本任务以通过VSTS Rest API获取当前构建的提交消息。
以下是获取此脚本的示例 powershell 脚本,您需要在构建定义中启用“允许脚本访问 OAuth 令牌”选项才能使用此脚本。
$collectionuri = $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI
$buildid = $env:BUILD_BUILDID
$project = $env:SYSTEM_TEAMPROJECT
$token = $env:SYSTEM_ACCESSTOKEN
$basicAuth= ("{0}:{1}"-f "anys",$token)
$basicAuth=[System.Text.Encoding]::UTF8.GetBytes($basicAuth)
$basicAuth=[System.Convert]::ToBase64String($basicAuth)
$headers= @{Authorization=("Basic {0}"-f $basicAuth)}
$url= $collectionuri + $project + "/_apis/build/builds/" + $buildID + "/changes?api-version=2.0"
$getbuildchanges = Invoke-RestMethod -Uri $url -headers $headers -Method Get | select value
foreach ($commit in $getbuildchanges.value)
{
Write-Host $commit.id;
$commit.id | Out-File -filepath commitmessages.txt -Append;
Write-Host $commit.message;
$commit.message | Out-File -filepath commitmessages.txt -Append;
}
更新:请尝试以下脚本:
$collectionuri = $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI
$buildid = $env:BUILD_BUILDID
$project = $env:SYSTEM_TEAMPROJECT
$token = $env:SYSTEM_ACCESSTOKEN
$basicAuth= ("{0}:{1}"-f "anys",$token)
$basicAuth=[System.Text.Encoding]::UTF8.GetBytes($basicAuth)
$basicAuth=[System.Convert]::ToBase64String($basicAuth)
$headers= @{Authorization=("Basic {0}"-f $basicAuth)}
$url= $collectionuri + $project + "/_apis/build/builds/" + $buildID + "/changes?api-version=2.0"
$getbuildchanges = Invoke-RestMethod -Uri $url -headers $headers -Method Get;
if($getbuildchanges.count -ne 0)
{
foreach ($commit in $getbuildchanges.value)
{
Write-Host $commit.id;
$commit.id | Out-File -filepath release_notes.txt -Append;
Write-Host $commit.message;
$commit.message | Out-File -filepath release_notes.txt -Append;
}
}
else
{
$nocommitfound = "There is no commit related to current build.";
Write-Host $nocommitfound;
$nocommitfound | Out-File -filepath release_notes.txt -Append;
}