Mark Beattie
Mark Beattie

Reputation: 175

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

I am trying to create a new Octopus deploy step, which will call a http endpoint. I have found the following step type that seems promising, but can get any documentation on it:

"Http Json Value Check Gets json from http endpoint, looks-up a value by key and checks that it matches a predefined value. If value matches then script exists with a success code, if value does not match then script exists with a failure code."

I am not sure what to enter for the: "Json Key" and the "Expected Value"

Has anyone done this? have an example or suggest a different method to achieve what I am trying?

Upvotes: 1

Views: 1159

Answers (1)

BFoust
BFoust

Reputation: 683

Here is a PowerShell script I use to get the JSON from an endpoint and check for a valid Value. If I could remember where I got the code base before I modified it a little I would give credit to the original author. It will work with either a string or a regex.

#-------------------------------------------------------------------------
# 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

Upvotes: 1

Related Questions