Modules/Wait-OctoDeployment.psm1

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
59
60
61
62
63
64
65
<#
.SYNOPSIS
Pause script while an Octopus deployment completes
 
.DESCRIPTION
Displays a sequence of dots on the screen while the specified deployment runs. If deployment is successful this will write to the host the build status and deployment
time while writing to output $true. Otherwise writes Build failed in (time) and writes to output $false.
 
.PARAMETER deployment
If a string then should be a DeploymentResource Id or can be a DeploymentResource. Other types are not accepted.
 
.EXAMPLE
Wait-OctoDeployment -deployment 802
 
.NOTES
General notes
#>

function Wait-OctoDeployment {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        $deployment
    )
    begin {
        $repository = Get-OctoRepository
    }
    
    process {
        [string]$id = $null
        if ($deployment -is [string]){
            $id = $deployment
        } elseif ($deployment -is [Octopus.Client.Model.DeploymentResource]) {
            $id = $deployment.Id
        } else {
            Write-Error -Message "Unable to infer Id from deployment input '$deployment'"
            return
        }
        $found =  $repository.Deployments.Get($id)
        $done = $false
        $task = $null
        Write-Host -Object 'Waiting for deployment [' -NoNewline
        while (-not $done) {
            Write-Host -Object '.' -NoNewline
            $task = Get-RestOcto $found.Links.Task
            if ($task.IsCompleted) {
                $done = $true
            } else {
                Start-Sleep -Seconds 1
            }
        }
        Write-Host -Object ']'
        Write-Host $task.Description
        if ($task.FinishedSuccessfully) {
            Write-Host -ForegroundColor Green "$($task.State) in $($task.Duration)"
            Write-Output $true
        } else {
            Write-Host -ForegroundColor Red "Failed in $($task.Duration): $($task.ErrorMessage)"
            Write-Output $false
        }
    }
    
    end {
    }
}