Skip to content

Instantly share code, notes, and snippets.

@jeremygaither
Forked from Dalmirog-zz/file.ps1
Created October 2, 2018 00:54
Show Gist options
  • Save jeremygaither/1d7f6415ac1e0c0460d4ea91551009a7 to your computer and use it in GitHub Desktop.
Save jeremygaither/1d7f6415ac1e0c0460d4ea91551009a7 to your computer and use it in GitHub Desktop.

Revisions

  1. jeremygaither renamed this gist Oct 2, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @dalmirog dalmirog created this gist Mar 5, 2015.
    67 changes: 67 additions & 0 deletions file.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    $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