我想知道是否有人有一个 PowerShell 脚本,可以通过 TFS 2015 (vNext) 的 Rest API 下载此构建 ID 的所有当前构建日志(直到当前步骤),为每个记录的构建步骤创建单独的文本文件,并压缩所有文本文件?
或者,如果已经有一种方法可以获取下载 URL 来执行“将所有日志下载为 zip”链接已经执行的操作(构建后),那么如何在 PowerShell 脚本中获取它?
如果给我一点时间,我可能可以自己做这个,但我想我会问,因为肯定有人已经拥有这个功能,如果你这样做了,请分享它。
回复我自己的帖子:
好的,所以我继续花时间编写了一个 powershell 脚本,该脚本完全按照我想要的方式执行,该脚本本质上重复了 TFS 2015 (vNext) 所做的“将所有日志下载为 zip”链接的功能。
您可以将其放入正在运行的构建中的步骤中,或者将其修改为在运行构建之后作为构建定义中的构建后步骤运行。
不同之处在于,在正在运行的构建中,它只会向您提供此 powershell 脚本步骤之前的步骤的日志。使用此 powershell 脚本的构建后定义允许您获取所有日志。
我选择不使用构建后定义,因为此步骤是我正在运行的构建定义中的最后步骤之一,并且后续步骤对于我来说无关紧要,无法获取日志信息(例如,我的最后 2 个步骤是 CreateZippedLogs.ps1 和 Copy将压缩日志发送到文件服务器)。
仅供参考,截至撰写本文时,我正在使用本地 TFS 2015(更新 3)。将 Invoke-RestMethod 行中的 -UseDefaultCredentials 替换为适合您情况的适当授权凭据。
这是 CreateZippedLogs.ps1,根据您的喜好修改它,然后享受:
[CmdletBinding()]
param()
# Create base URI for the REST API
$baseURI = $Env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI + $Env:SYSTEM_TEAMPROJECT + "/_apis/"
# Get Build ID from the environment variable
$buildID = $Env:BUILD_BUILDID
# Build Timeline URI for $Env:BUILD_BUILDID
$buildTimelineURI = "$baseURI/build/builds/$buildID/timeline`?api-version=2.0"
# Output intent to create Zipped Log content for this build id
Write-Output "Attempting to create Zipped Log content for Build ID $buildID from URL: ""$buildTimelineURI"""
# Get sorted build timeline (ascending by default)
$buildTimeline = (Invoke-RestMethod -Uri $buildTimelineURI -UseDefaultCredentials).records | Sort-Object { $_.log.id }
# Get log count
$logsCount = $buildTimeline.Count # the number of timeline records (i.e. logs in the array)
# Remove sub-staging folder recursively for the logs
# Note: $Env:My_BuildLabel is defined as "$Env:BUILD_DEFINITIONNAME`_$Env:BUILD_BUILDNUMBER" for my builds; use whatever name is appropriate for your builds
$singleLogFileFolder = "$Env:BUILD_ARTIFACTSTAGINGDIRECTORY\$Env:My_BuildLabel`_Logs"
Remove-Item "$singleLogFileFolder" -recurse -ErrorAction SilentlyContinue -Verbose
# Remove any previously created Zip file with the same filename that we are about to create
Remove-Item "$singleLogFileFolder.zip" -ErrorAction SilentlyContinue -Verbose
# If number of logs in the array is > 0, create sub-staging folders for the logs
if ($logsCount -gt 0)
{
# First, a top-level folder to hold all logs in a single .txt file
New-Item -ItemType directory -Path "$singleLogFileFolder" -ErrorAction Stop -Verbose
$stepLogsSingleFile = "$singleLogFileFolder\1_Build.txt"
# Second, a subfolder to hold individual build step log files
$stepLogFilesFolder = "$singleLogFileFolder\Build"
New-Item -ItemType directory -Path "$stepLogFilesFolder" -ErrorAction Stop -Verbose
}
else
{
# Output that there were no logs found for this Build ID
Write-Output "No logs found for Build ID $buildID"
Exit 0 # Exit successfully
}
# Get list of invalid filename characters (in a regex string)
$invalidFilenameCharactersRegexString = "[{0}]+" -f [regex]::Escape(([System.IO.Path]::GetInvalidFileNameChars() -join ""))
# Output progress
Write-Output "Getting Log content and saving to files:"
# Iterate through each record in the build timeline array
foreach ($timelineRecord in $buildTimeline)
{
# Extract url for each log
$logURL = $timelineRecord.log.url
# Don't try to get empty log URL's
if ([string]::IsNullOrWhiteSpace($logURL))
{
continue
}
# Get log content
$logContent = (Invoke-RestMethod -Uri $logURL -UseDefaultCredentials).value
# Append step log output to the single file (not -Verbose because it duplicates information)
$logContent | Out-File "$stepLogsSingleFile" -Append
# Get log id
$logID = $timelineRecord.log.id
# Remove any invalid filename characters from the step name
$stepName = $timelineRecord.name -replace "$invalidFilenameCharactersRegexString", ""
# Create a step log output filename appropriate to the step number and name
$stepLogFile = "$stepLogFilesFolder\$logID`_$stepName`.txt"
# Save the step log content
$logContent | Out-File "$stepLogFile" -Verbose
}
Write-Output "Compressing Log files:"
if ($PSVersionTable.PSVersion.Major -lt 5)
{
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($singleLogFileFolder, $singleLogFileFolder + ".zip")
}
else
{
Compress-Archive "$singleLogFileFolder\*" -DestinationPath "$singleLogFileFolder.zip" -Verbose
}
# ----------------------------------------------------------
# Remove temporary sub-staging folder recursively for the logs (don't leave it hanging around)
Write-Output "Removing temporary Log folders and files:"
Remove-Item "$singleLogFileFolder" -recurse -ErrorAction SilentlyContinue -Verbose
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)