2015-11-12 53 views
0

我正在嘗試創建一個新的Octopus部署步驟,它將調用一個http端點。 我發現,似乎有希望接下來的步驟類型,但可以得到任何文件:在Octopus Deploy中添加部署步驟來調用http端點

「宀的Json值檢測 從HTTP端點獲取JSON,看起來,一個值的關鍵,並檢查它匹配一個預定義的值。如果值匹配,則腳本以成功代碼存在,如果值不匹配,則腳本以失敗代碼存在。「

我不知道該怎麼對進入: 「Json的重點」和「期望值」

有沒有人這樣做呢?有一個例子或建議一個不同的方法來實現我想要的?

+0

你從哪裏獲得構建步驟模板? – Fenton

回答

0

以下是我用來從端點獲取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