本文介绍了通过powershell获取压缩的TFS 2015(vNext)构建输出日志(就像构建后的下载链接一样)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm wondering if anyone has a PowerShell script to either download all the current build logs for this build id (up to the current step) through the Rest API for TFS 2015 (vNext), create separate text files for each logged build step, and zip all the text files?

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

Or, if there is already a way to get a download URL to do what the "Download all logs as zip" link already does (after a build), how can I get it in a PowerShell script?

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

I can probably do this myself given a little time, but I thought I'd ask, because someone must have this functionality already, and if you do, please share it.

推荐答案

回答我自己的帖子:

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

Ok, so I went ahead and spent the time to write a powershell script that does exactly what I wanted it to do, which essentially duplicates functionality of the "Download all logs as zip" link that TFS 2015 (vNext) does.

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

You can either put it into a step in a running build, or modify it to run as a post build step in a build definition after the running build.

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

The difference being that in the running build, it will only give you logs up to the step before this powershell script step. A post build definition with this powershell script allows you to get all of the logs.

我选择不使用构建后定义,因为此步骤是我正在运行的构建定义中的最后步骤之一,并且随后的步骤对于获取日志信息来说是无关紧要的(例如,我的最后两个步骤是CreateZippedLogs. ps1,然后将压缩日志复制到文件服务器.

I chose not to use a post build definition since this step was one of the last steps in my running build definition, and the steps that followed were inconsequential to me to have log information for (e.g. My last 2 steps are CreateZippedLogs.ps1 and Copy the Zipped Logs to a file server).

仅供参考,在撰写本文时,我正在使用本地TFS 2015(更新3).使用适合您情况的适当授权证书替换Invoke-RestMethod行中的-UseDefaultCredentials.

FYI, I'm using on-premise TFS 2015 (Update 3) as of this writing. Replace the -UseDefaultCredentials in the Invoke-RestMethod lines with your appropriate authorization credentials, suitable for your situation.

这是CreateZippedLogs.ps1,根据您的喜好对其进行修改,并享受:

Here is CreateZippedLogs.ps1, modify it to your liking, and enjoy:

[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获取压缩的TFS 2015(vNext)构建输出日志(就像构建后的下载链接一样)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 11:53