添加部署步骤以在 Octopus Deploy 中调用 http 端点

Adding a deployment step to call a http endpoint in Octopus Deploy

我正在尝试创建一个新的 Octopus 部署步骤,它将调用一个 http 端点。 我发现以下步骤类型看起来很有希望,但可以获取有关它的任何文档:

"Http Json 值检查 从 http 端点获取 json, 按键查找值并检查它是否与预定义值 匹配。如果值匹配,则存在带有成功代码的脚本,如果值不匹配,则存在带有失败代码的脚本。"

我不确定要输入什么: "Json Key" 和 "Expected Value"

有人做过吗?有例子或建议不同的方法来实现我正在尝试的吗?

这是我用来从端点获取 JSON 并检查有效值的 PowerShell 脚本。如果我能记得在我稍微修改之前从哪里获得代码库,我会感谢原作者。它适用于字符串或正则表达式。

#-------------------------------------------------------------------------
# Warmup.ps1
#-------------------------------------------------------------------------
[int]$returnme = 0
[int]$SleepTime = 5
[string]$regex = '[>"]?[aA]vailable["<]?'
[string]$strContains = $regex
# [string]$strContains = "log in"
[string]$hostName = hostname
[string]$domainName = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName .).DNSDomain
[string]$warmMeUp = "http://$hostName.$domainName/endpoint"
[string]$html = "Will Be Set Later"

#-------------------------------------------------------------------------
# Get-WebPage
#-------------------------------------------------------------------------
function Get-WebPage([string]$url) 
{
    try 
    {
        $wc = new-object net.webclient; 
        $wc.credentials = [System.Net.CredentialCache]::DefaultCredentials;
        [string]$pageContents = $wc.DownloadString($url);
        $wc.Dispose();
    }
    catch
    {
        Write-Host "First Try Failed.  Second Try in $SleepTime Seconds."
        try
        {
            Start-Sleep -s $SleepTime
            $wc = new-object net.webclient; 
            $wc.credentials = [System.Net.CredentialCache]::DefaultCredentials;
            $pageContents = $wc.DownloadString($url);
            $wc.Dispose();
        }
        catch
        {
            $pageContents = GetWebSiteStatusCode($url)
        }
    }
    return $pageContents;
}

#-------------------------------------------------------------------------
# GetWebSiteStatusCode
#-------------------------------------------------------------------------
function GetWebSiteStatusCode 
{
    param (
        [string] $testUri,
        [int] $maximumRedirection = 5
    )
    $request = $null
    try {
        $request = Invoke-WebRequest -Uri $testUri -MaximumRedirection $maximumRedirection -ErrorAction SilentlyContinue
    } 
    catch [System.Net.WebException] {
        $request = $_.ErrorDetails.Message
    }
    catch {
        Write-Error $_.Exception
        return $null
    }
    return $request
}

#-------------------------------------------------------------------------
# Main Application Logic
#-------------------------------------------------------------------------
"Warming up '{0}'..." -F $warmMeUp;
$html = Get-WebPage -url $warmMeUp;

Write-Host "Looking for Pattern $strContains"
if ($html.ToLower().Contains("unavailable") -or !($html -match $strContains))
{
    $returnme = -1
    Write-Host "Warm Up Failed. html returned:`n" + $html
}

exit $returnme