gpt4 book ai didi

powershell - 通过powershell获取压缩的TFS 2015(vNext)构建输出日志(就像构建后的下载链接一样)

转载 作者:行者123 更新时间:2023-12-02 23:11:20 25 4
gpt4 key购买 nike

我想知道是否有人通过PowerShell脚本通过TFS 2015的Rest API(vNext)下载该构建ID的所有当前构建日志(到当前步骤),为每个记录的构建步骤创建单独的文本文件,并压缩所有文本文件?

或者,如果已经有一种获取下载URL的方式(在构建后)执行“将所有日志下载为zip”链接已经执行的方法,那么如何在PowerShell脚本中获取它?

我可以花一点时间自己做,但是我想问一下,因为某人必须已经具有此功能,如果有,请分享。

最佳答案

回答我自己的帖子:

好的,所以我继续并花了一些时间编写一个Powershell脚本,该脚本完全可以实现我想要的功能,该脚本实质上复制了TFS 2015(vNext)所做的“将所有日志以zip下载”功能。

您可以将其放入正在运行的构建中的某个步骤中,也可以对其进行修改以在运行的构建之后作为构建定义中的后期构建步骤来运行。

区别在于正在运行的构建中,它只会使您登录到该Powershell脚本步骤之前的步骤。使用此powershell脚本进行后期构建定义可让您获取所有日志。

我选择不使用构建后定义,因为此步骤是我正在运行的构建定义中的最后一个步骤,并且后续步骤对于获取日志信息来说是无关紧要的(例如,我的最后两个步骤是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

关于powershell - 通过powershell获取压缩的TFS 2015(vNext)构建输出日志(就像构建后的下载链接一样),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39715860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com