List all projects with latest successfully deployments from specific environment and deploy again

Hello,

Is it possible, using octo.exe to, for instance: get/list all projects with latest successfully deployed releases into a specific environment (example: Development)?

Here is some code that I am trying to set it up:

# Get list of all projects | trip unecessary strings | write into txt file nice list of all projects
$projects = .\Octo.exe list-projects --APIkey $octopusAPIKey --server $octopusURL | Select- 
Object -Skip 6 
$all = $projects.Substring(3) -replace "\(ID.*\)",'' | Out-File projects.txt

# Deploy latest releases to the all DEV environments 
.\Octo.exe deploy-release --version latest --deployto "DEV" --APIkey $env:octopusAPIKey --server 
$env:octopusURL --project  $all

How to make this to work? As I do not know how to pass all projects to –project and if it is possible to do it on this way?

Hi,

Thanks for getting in touch.

You may be able to use scheduled deployment triggers to accomplish what you are trying to do. You can configure a trigger to automatically re-deploy a release at scheduled intervals: https://octopus.com/docs/deployment-process/project-triggers/scheduled-project-trigger

Regarding your script, Octo.exe deploy-release works for one project at a time. You could iterate through your projects using Octo.exe deploy-release for each project. For example:

foreach ($project in $all) {
  .\Octo.exe deploy-release --version latest --deployto "DEV" --APIkey $env:octopusAPIKey --server 
$env:octopusURL --project  $project
}

To get the latest deployments for an environment you can use Octo.exe list-latestdeployments and pass the environment argument.

I hope this helps.

Cheers,
Shane

1 Like

Hi @Shane_Gill,

Thank you for your prompt answer, it seems your advice have a positive results. But, is it possible to add Target role, so deploy-release can redeploy only, for instance on Web servers?
Example:

.\Octo.exe deploy-release --version latest --deployto "DEV" --APIkey $octopusAPIKey --server $octopusURL --project $all --target "Webservers"

Hi,

There is currently no way to deploy to one specific role. Can I ask what your scenario is? I might be able help with a solution.

Cheers,
Shane

Hi @Shane_Gill,

  1. we have around 300 projects
  2. we add a new web server called WEB5 and want to redeploy to WEB5 all latest deployed releases which are deployed only to “Development” environment, and have target role “Webservers” where are all WEB1-4 servers. As WEB1-4 will be deleted at one moment, this is practically migration.

Your previous post works for us, but it will redeploy latest deployments which are marked as “Development” to all target roles. And we want only to redeploy “Development” to target role “Webservers” where we include a new WEB5. In that case, we’ll have consistent environments to all web servers and then we can power off one old node (WEB1-4 servers). Trigger part won;t work as it will take a lot of time to setup trigger to each project.

Hope this is make sense, let me know if need to clarify a bit.

Thank you,
B

Hi,

You can create a deployment to a specific machine via the API if you know the ID of the machine. This is not supported using Octo.exe so you would need to write some custom API script to do it. I can help if you’d like.

It sounds like you could use automatic deployment triggers that will deploy to the WebServers role in the Development environment. You could create the triggers programmatically on all of the projects with an API script, I can also help with that if you want.

Cheers,
Shane

Hi,

If it is possible to create the trigger to all projects from one shot, then this might be useful. I didn;'t work with API in the past, this is all new to me.
How to trigger deployments to a new node with WebServer role in the Deployment environment?

Hi,

Here is an example PowerShell script you could use to create project triggers for all of your projects:

The script is idempotent as long as the script name remains the same, so you can re-run this script when you add new projects or change the action and filter options and will not create duplicate triggers.

Cheers,
Shane

1 Like

Hi @Shane_Gill,

This is great! Thank you.

It seems it won’t put the right trigger role in the trigger. Whit this code, it creates the trigger, where environment is DEV and target roles are DEV. variable $triggerRole = "Web-server only applies in the name of the trigger. And we need target role to be Web-server.

Am I missing something ?

I messed this line up, it should be:

$triggerFilter.Roles.Add($triggerRole)

Sorry about that, I’ll update the script.

Perfect, I’ll test today. Thank you so much!
One more question, how to delete all these new created triggers? As there is a lot project which won’t goes to this target roles, etc…

To delete every trigger would be like this:

Add-Type -Path 'Octopus.Client.dll' 

$apikey = 'API-MYAPIKEY' # Get this from your profile
$octopusURI = 'http://MY-OCTOPUS' # Your server address

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey 
$repository = New-Object Octopus.Client.OctopusRepository $endpoint

$triggers = $repository.ProjectTriggers.FindAll()

foreach ($trigger in $triggers) {
    $repository.ProjectTriggers.Delete($trigger)
}

This line doesn’t work on my side. IS there a way to pass some argument and value, like delete all triggers with name “Automatically deploy to Web-Server”?

To find by name, you would use: $triggers = $repository.ProjectTriggers.FindByName("Automatically deploy to Web-Server")

I need to head off for the weekend but would be happy to help on Monday if you still need it. There are a lot of examples in our GitHub repository: https://github.com/OctopusDeploy/OctopusDeploy-Api/tree/master/Octopus.Client/PowerShell

If you use PowerShell ISE there should be intellisense to help you figure out which methods are available, or if you prefer Octopus.Client also work with C#.

Cheers,
Shane

1 Like

I am getting similar error on the same line:

Exception calling "FindByName" with "1" argument(s): "Unable to process response from server: 
Requested value 'DailySchedule' was not found.. Response co
ntent: {
"ItemType": "ProjectTrigger",
"TotalResults": 10,
"ItemsPerPage": 30,
"NumberOfPages":"
At C:\Users\adm-bradjur\Desktop\DeleteTriggers.ps1:11 char:1
+ $triggers = $repository.ProjectTriggers.FindByName("Automatically dep ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : OctopusDeserializationException

Thank you for your help!
I’ll try to figure this out and let you know for the outcome.

PS. Not sure why this error occurs, but after rebooting the server, script is working, except it only delete trigger from one project (one trigger). Each time I execute script, it delete one trigger by one.

All the best,
B

Hi,

Have you had success getting the triggers configured?

To delete all of the triggers by name you may need to loop over $repository.ProjectTriggers.FindByName.

Cheers
Shane

Thank you for pointing out !
This loop deletes one trigger only after execution. I’ve to execute multiple times to delete all triggers.
Whole code:

Add-Type -Path 'Octopus.Client.dll' 
$apikey = 'API-****' 
$octopusURI = 'http://****' 
$triggerName = "Automatically deploy to ****"
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey 
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$triggers = $repository.ProjectTriggers.FindByName($triggerName)
foreach ($trigger in $triggers) {
$repository.ProjectTriggers.Delete($trigger)
}

Try this instead:

while ($true) {
    $trigger = $repository.ProjectTriggers.FindByName("Automatically deploy to ****")
    if (!$trigger) { break; }
    $repository.ProjectTriggers.Delete($trigger)
}
1 Like

That’s it…thank you so much for helping, I appreciate it !

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.