Created
March 5, 2015 07:17
-
-
Save Dalmirog-zz/05fb70903c0b3c0d9572 to your computer and use it in GitHub Desktop.
New Release for specific machines
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
$apiKey = "" #Octopus API Key | |
$OctopusURL = "" #Octopus URL | |
$Header = @{ "X-Octopus-ApiKey" = $apiKey } | |
$ProjectName = "" #project name | |
$EnvironmentName = "" #environment name | |
[string[]]$filter = "" #Pattern to filter machines. e.g. "*:10934","*db*","http://Webserver1" | |
###START### | |
#Getting Environment and Project By Name | |
$Project = Invoke-WebRequest -Uri "$OctopusURL/api/projects/$ProjectName" -Headers $Header| ConvertFrom-Json | |
$Environment = Invoke-WebRequest -Uri "$OctopusURL/api/Environments/all" -Headers $Header| ConvertFrom-Json | |
$Environment = $Environment | ?{$_.name -eq $EnvironmentName} | |
#getting machines on the environment | |
$machines = Invoke-WebRequest -Uri ($OctopusURL + $Environment.Links.Machines) -Headers $Header| ConvertFrom-Json | |
##Filtering machines for each filter declared on $filder | |
##If you want to filter by name or URI, comment one of the lines inside the foreach loop | |
$machineIDs = @() | |
foreach($f in $filter){ | |
#$ID = ($machines.Items | ?{$_.name -like $f}).id #Use to filter by machine name on Octopus | |
$ID = ($machines.Items | ?{$_.uri -like $f}).id #Use to filter by URI | |
if($ID -eq $null){ | |
write-host "No machine was found with the pattern $f on environment $($environment.Name)" -ForegroundColor Red | |
} | |
else{ | |
$machineIDs += $ID | |
} | |
} | |
If($machineIDs -eq $null){ | |
#If there are not IDs on $machineIDs and you just proceed, it'll deploy to all machines by default | |
#to prevent this scenario, we are adding a Throw here to stop the entire script | |
Throw "No machines where found with the pattern/s '$filter' on $($environment.name)" | |
} | |
#Getting Deployment Template to get Next version | |
$dt = Invoke-WebRequest -Uri "$OctopusURL/api/deploymentprocesses/deploymentprocess-$($Project.id)/template" -Headers $Header | ConvertFrom-Json | |
#Creating Release | |
$ReleaseBody = @{ Projectid = $Project.Id | |
version = $dt.nextversionincrement } | ConvertTo-Json | |
$r = Invoke-WebRequest -Uri $OctopusURL/api/releases -Method Post -Headers $Header -Body $ReleaseBody | ConvertFrom-Json | |
#Creating deployment with the specific machine IDs | |
$DeploymentBody = @{ | |
ReleaseID = $r.Id #mandatory | |
EnvironmentID = $Environment.id #mandatory | |
specificmachineIDs = $machineIDs | |
} | ConvertTo-Json | |
$d = Invoke-WebRequest -Uri $OctopusURL/api/deployments -Method Post -Headers $Header -Body $DeploymentBody |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment