Created
December 30, 2015 20:42
-
-
Save rendicott/09db17e654860794d46c to your computer and use it in GitHub Desktop.
Uses Powershell to interact with the Octopus REST API to create a deployment to a specific machine / release / environment combination.
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]$machineName, | |
[string]$desiredVersion, | |
[string]$environmentString, | |
[string]$project | |
) | |
Import-Module Octopus-Cmdlets | |
#$machineName = 'DOGEN09' | |
#$desiredVersion = '0.666.420.709' | |
#$environmentString = 'NEW' | |
#$project = 'OPS_SelfServe' | |
$releaseId = $null | |
$apiKey = 'API-DZ8ABCABCABCABCABCABCABCHCQ' | |
$baseUrl = 'http://vmoctopusdeploy:8090/' | |
# connect using Octopus-Cmdlets | |
# (use choco install psget to 'import-module octopus-cmdlets' if you don't have this) | |
Connect-OctoServer $baseUrl $apiKey | |
# Find the ID of the release we want within the project | |
write-host "Searching for releases in project '$project'..." | |
$releases = get-octorelease -project $project | |
foreach ($release in $releases) { | |
if ($release.version -eq $desiredVersion) { | |
$releaseId = $release.Id | |
} | |
} | |
if ($releaseId -eq $null) { | |
write-host "ERROR: No release could be found with the version '$desiredVersion'. Exiting" | |
Exit -1 | |
} | |
# Find the Environment ID we want | |
write-host "Searching for ID of Environment '$environmentString'..." | |
$environment = Get-OctoEnvironment -name $environmentString | |
$environmentId = $environment.id | |
if ($environmentId -eq $null) { | |
write-host "ERROR: No environment could be found with the name '$environmentString'. Exiting" | |
Exit -1 | |
} | |
write-host "Environment ID = " | |
# find the machine ID | |
write-host "Searching for ID of machine '$machineName'..." | |
$machine = Get-OctoMachine -name $machineName | |
$machineId = $machine.id | |
if ($machine -eq $null) { | |
write-host "ERROR: No machine could be found with the name '$machineName'. Exiting" | |
Exit -1 | |
} | |
write-host "Machine ID = " $machineId | |
#first update the release variables | |
write-host "Updating release variables for release '$releaseId'" | |
$uri = $baseUrl + '/api/releases/' + $releaseId + '/snapshot-variables' | |
Invoke-RestMethod -Uri $uri -Method POST -Header @{ "x-Octopus-ApiKey" = $apiKey } -Body $null | |
# build the JSON with a here-string | |
<# this would be better with hash but this works too | |
for more complicated POSTS you can run fiddler while | |
you kick off a deployment and get a feel for how the | |
web GUI is building the API call | |
#> | |
$json = @" | |
{ | |
"ReleaseId":"$releaseId", | |
"EnvironmentId":"$environmentId", | |
"SkipActions":[], | |
"SpecificMachineIds":["$machineId"], | |
"QueueTime":null, | |
"FormValues":{}, | |
"ForcePackageDownload":false, | |
"UseGuidedFailure":false | |
} | |
"@ | |
# couldn't get this to work | |
<# | |
$hash = @{ | |
ReleaseId = $releaseId | |
EnvironmentId = $environment.id | |
SkipActions = @() | |
SpecificMachineIds = @($machine.id) | |
QueueTime = null | |
FormValues = | |
} | |
$json = $hash | convertto-json | |
#> | |
write-host "JSON we're sending = " $json | |
$uri = $baseUrl + '/api/deployments' | |
# call the API | |
$results = Invoke-RestMethod -Uri $uri -Method POST -Header @{ "x-Octopus-ApiKey" = $apiKey } -Body $json | |
write-host $results | |
foreach ($link in $results.links) { $uuu = $baseUrl + $link.Web } | |
# output the URL that the user can go to to watch deployment steps status | |
write-host "See status here: " $uuu |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment