Functions/BuildLog/Get-BambooBuildLog.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<#
.SYNOPSIS
    Gets build logs for a single Stage in a Bamboo Build Result
.DESCRIPTION
    It fetches the build result for a specific or latest build plan and
    returns it as text.

.PARAMETER BuildKey
    Mandatory - Key for the exact Bamboo build to get the build results
.PARAMETER StageKey
    Mandatory - Key for the build stage
.PARAMETER PlanKey
    Mandatory - Key for the Bamboo Plans to filter
.PARAMETER Build
    Optional - Parameter to specify the build in question, by default it
    always returns the latest build. The valid values are:
        LATEST - to return the latest build info
        BUILD-Number - to return an exact build result by its id

.EXAMPLE
    Get-BambooBuildLog -PlanKey PSB-BB -StageKey BASE
.EXAMPLE
    Get-BambooBuildLog -BuildKey PSB-BB-22 -StageKey BASE

#>

function Get-BambooBuildLog {
    [CmdletBinding(DefaultParameterSetName="ByPlanKey")]
    param(
        [Parameter(Mandatory,ParameterSetName="ByBuildKey")]
        [Alias('BuildResultKey')]
        [ValidatePattern('\w+-\w+-\d+')]
        [string]$BuildKey,

        [Parameter(Mandatory,ParameterSetName="ByPlanKey")]

        [ValidatePattern('\w+-\w+')]
        [string]$PlanKey,

        [Parameter(ParameterSetName="ByPlanKey")]
        [ValidatePattern('\d+|latest')]
        [string]$Build='latest',

        [Parameter(Mandatory,ParameterSetName="ByPlanKey")]
        [Parameter(Mandatory,ParameterSetName="ByBuildKey")]
        [ValidatePattern('\w+')]
        [string]$StageKey
    )

    if ($BuildKey -and ($BuildKey -imatch '(\w+-\w+)-(\d+)')) {
        $PlanKey = $Matches[1]
        $Build = $Matches[2]
    }

    $buildResult = Get-BambooResult -PlanKey $PlanKey -Build $Build
    $logUrl = "$($script:BambooServer)/download/$PlanKey-$StageKey/build_logs/$PlanKey-$StageKey-$($buildResult.buildNumber).log"
    (Invoke-WebRequest -Uri $logUrl -UseBasicParsing -ErrorAction Stop).Content
}