Created
December 2, 2013 09:00
-
-
Save mastoj/7746853 to your computer and use it in GitHub Desktop.
Script for automating deployment using the v 2.0 api of Octopusdeploy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param([string]$ServerUrl, | |
[string]$ProjectName, | |
[string]$EnvironmentName, | |
[string]$Version, | |
[string]$RootUrl, | |
[string]$OctoApiKey) | |
Write-Host "Server url: $ServerUrl" | |
Write-Host "Project name: $ProjectName" | |
Write-Host "Environment name: $EnvironmentName" | |
Write-Host "Version: $Version" | |
Write-Host "RootUrl: $RootUrl" | |
Write-Host "Api key: $OctoApiKey" | |
function Get-Project { | |
Write-Host $ServerUrl | |
} | |
function Create-WebClient { | |
$wc = New-Object System.Net.webClient | |
$wc.Headers.Add("X-Octopus-ApiKey", "$octoApiKey") | |
return $wc | |
} | |
function Clean-ParamsFromTemplate($template) { | |
return $template -replace "{.+}" | |
} | |
function Get-Root($wc) { | |
$rootJson = $wc.DownloadString("$ServerUrl$RootUrl") | |
$root = ConvertFrom-Json $rootJson | |
return $root | |
} | |
function Get-ItemFromNode($node, $wc, $itemName) { | |
$itemTemplateUrlProp = $node.Links.PSObject.Properties | Where { $_.Name -eq $itemName } | Select -f 1 | |
$itemRelUrl = Clean-ParamsFromTemplate $itemTemplateUrlProp.Value | |
$itemJson = $wc.DownloadString("$ServerUrl$itemRelUrl") | |
$item = ConvertFrom-Json $itemJson | |
return $item | |
} | |
function Get-Project($root, $wc) { | |
$projects = Get-ItemFromNode $root $wc "Projects" | |
$project = $projects.Items | Where { $_.Name -eq $ProjectName } | Select -f 1 | |
return $project | |
} | |
function Get-Environment($root, $wc) { | |
$environments = Get-ItemFromNode $root $wc "Environments" | |
$environment = $environments.Items | Where { $_.Name -eq $EnvironmentName } | Select -f 1 | |
return $environment | |
} | |
function Create-CreateReleaseBody ($project, $wc) { | |
$deploymentProcess = Get-ItemFromNode $project $wc "DeploymentProcess" | |
$stepsArr = @() | |
$deploymentProcess.Steps | Select { [string]::Format("{{""StepName"": ""{0}"", ""Version"":""{1}""}}", $_.Name, $Version) } | % { $stepsArr = $stepsArr + $_.PSObject.Properties.Value} | |
$stepsStr = [string]::Join(",", $stepsArr) | |
$body = [string]::Format("{{""ProjectId"":""{0}"", ""Version"": ""{1}"", ""SelectedPackages"":[{2}]}}", $project.Id, $Version, $stepsStr) | |
return $body | |
} | |
function Post-ToOcto ($body, $url, $wc) { | |
$wc.Headers.Add("Content-Type", "application/json") | |
$json = $wc.UploadString("$url", "POST", $body) | |
$item = ConvertFrom-Json $json | |
return $item | |
} | |
function Create-Release ($project, $root, $wc) { | |
$body = Create-CreateReleaseBody $project $wc | |
Write-Host "Create release body: $createReleaseBody" | |
$releaseRelUrl = Clean-ParamsFromTemplate $root.Links.Releases | |
$release = Post-ToOcto $body "$ServerUrl$releaseRelUrl" $wc | |
Write-Host "Release created with id: " $release.Id | |
return $release | |
} | |
function Create-Deploy ($release, $environment, $root, $wc) { | |
$body = [string]::Format("{{""ReleaseId"":""{0}"",""EnvironmentId"":""{1}"",""UseGuidedFailure"":false,""SkipActions"":[]}}", $release.Id, $environment.Id) | |
Write-Host "Create deploy body: " $body | |
$deployRelUrl = Clean-ParamsFromTemplate $root.Links.Deployments | |
Write-Host $deployRelUrl | |
$deploy = Post-ToOcto $body "$ServerUrl$deployRelUrl" $wc | |
Write-Host "Deployment created with id: " $deploy.Id | |
return $deploy | |
} | |
function Check-DeployStatus ($deploy, $root, $wc) { | |
$deployRelUrl = $deploy.Links.Self | |
$deployJson = $wc.DownloadString("$ServerUrl$deployRelUrl") | |
$deploy = ConvertFrom-Json $deployJson | |
$taskRelUrl = $deploy.Links.Task | |
$taskFinished = $false | |
while(!$taskFinished) { | |
$taskJson = $webclient.DownloadString("$serverUrl$taskRelUrl") | |
$task = ConvertFrom-Json $taskJson | |
if($task.IsCompleted) { | |
return $task | |
} | |
Write-Host "Deploy not finished, waiting 5 s for next check..." | |
Start-Sleep -s 5 | |
} | |
} | |
try { | |
$webClient = Create-WebClient | |
$root = Get-Root $webClient | |
$project = Get-Project $root $webClient | |
Write-Host "Project " $project.Name "has id: " $project.Id | |
$release = Create-Release $project $root $webClient | |
$environment = Get-Environment $root $webClient | |
Write-Host "Starting deployment of release" | |
$deploy = Create-Deploy $release $environment $root $webClient | |
Write-Host "Waiting for deployment to finish" | |
$task = Check-DeployStatus $deploy $root $webClient | |
if($task.FinishedSuccessfully) { | |
Write-Host "Deployment done, everything looks ok" | |
} | |
else { | |
Write-Host "Deploy failed" | |
exit 1 | |
} | |
} | |
catch [Exception]{ | |
Write-Host "Script failed: " $_.ToString() | |
exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment