Redeployment as an option/alternative to Deploy Release step

Good day!
I have a couple of runbooks on Octopus Server that automate some of the maintenance/cleanup tasks needed for applications. As of now, after running a runbook that copies a database from a production environment to a QA one, I need to trigger a redeploy for two projects that the runbook affects. Sadly, the Deploy Release step I was looking at is not quite what is needed for me in that regard, as it does not allow to just redeploy QA environment, or at least to select a last release in the environment, instead giving me the latest release serverwide, which might not be meant for QA at all. It is not that much of an issue for me to just do redeploys after the runbook would finish its job, but I wonder if there is a way of triggering a redeploy as a runbook step or somehow alike.
Thanks in advance!

Hi @sergey.shcherbakov,

Firstly, let me welcome you to the Octopus Deploy community, it is great to see you here.

Thank you for your question.

If I have understood your question, then I have an idea that springs to mind that may help.

If you are looking to trigger a release for a certain version on a specific environment as part of your Runbook process, then I wonder if this idea may help you.

I set up a small test in my Octopus sandbox to do the following:

I set up a Prompted Variable to allow me to add in the release version as part of my Runbook:

As part of my Runbook I added the Run a script step to leverage the OctopusCLI and in particular the Deploy Release command. Just to note here, you will need to install the CLI where the step will be run (I am using a worker here, but you could use the Octopus Server).

Within this step I added the following (under the “Inline Source Code” section):

octo deploy-release --project $OctopusParameters["Octopus.Project.Name"] --releaseNumber $OctopusParameters["Project.Release.Version"] --deployto $OctopusParameters["Octopus.Environment.Name"] --server $OctopusParameters["Octopus.Web.BaseUrl"] --apiKey $OctopusParameters["Octopus.API.Key"]

For visual purposes:

When I test this and run my Runbook, I can enter the release number for the prompted variable (in my example I want to re-deploy version 0.0.2 to Test):

After the step has finished, this is my result:

If I look in my project overview, I can see the release 0.0.2 in Test has been deployed:

I hope this helps, if you have any questions then please do let me know.

All the best,

Hi @doug.pipe,
Thank you for both warm greetings and for your input!
I am yet to look into Octopus CLI as a whole, however, going through that script you have provided makes me wonder what scope would the parameters be from?
To clarify more on the situation I am currently in. I have a runbook in a project A, and during its run I would want to redeploy the currently active QA/Hotfix/Sandbox release of projects B and C. Would the --releaseNumber $OctopusParameters["Project.Release.Version"] part be picked based on all the rest of the parameters that the script would take along with the version parameter (e.g., project name and environment name), and would that parameter be of the latest release within the current scope?
Thank you again.

Hey @sergey.shcherbakov,

Great question. Unfortunately, with Octopus System Variables these are generated during the runtime of the release or Runbook where some variables can’t be referenced through a Runbook (such as Project.Release.Version).

However, for your scenario you should be able to achieve something similar to this using the Octopus REST API to achieve what you are after.

In case you haven’t come across our this we do have Samples to help get you started with this.

In particular we do have a sample script to GetLastSuccessfulForProjectAndEnvironment.

I had a look at using this for your example and needed to tweak it slightly, but I feel it may give you the desired output you are after.

I added this script to my Runbook step that was doing the re-deployment:

$OctopusURL = $OctopusParameters["Octopus.Web.BaseUrl"]
$OctopusAPIKey = $OctopusParameters["Octopus.API.Key"]

$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$ProjectID = $OctopusParameters['Octopus.Project.ID']
$EnvironmentID = $OctopusParameters['Octopus.Environment.ID']

$ProjectDashboardReleases = (Invoke-WebRequest $OctopusURL/api/progression/$ProjectID -UseBasicParsing -Method Get -Headers $header).content | ConvertFrom-Json
$LastSuccessfullRelease = $ProjectDashboardReleases.Releases.Deployments.$EnvironmentId | ?{$_.IsCurrent -eq "True"} | select -First 1
$CurrentReleaseVersion = $LastSuccessfullRelease.ReleaseVersion

Write-Host "Current release version is: $CurrentReleaseVersion"

octo deploy-release --project $OctopusParameters["Octopus.Project.Name"] --releaseNumber $CurrentReleaseVersion --deployto $OctopusParameters["Octopus.Environment.Name"] --server $OctopusParameters["Octopus.Web.BaseUrl"] --apiKey $OctopusParameters["Octopus.API.Key"]

When this step is run, it will re-deploy the latest deployed version in your desired environment.

I swapped:

?{$_.state -eq "Success"}


?{$_.IsCurrent -eq "True"}

I also removed my prompted variable as was no longer required for this scenario.

I hope this helps you get where you need to be.

All the best,

Hey @doug.pipe,
Thank you for all of that information, it gives me a lot of tools and solutions to my situation. I would definitely look closer at both CLI and API that Octopus Server provides. And also thank you for the warm welcome to the community!

Hi @sergey.shcherbakov,

It is my pleasure; I hope you get this working for you.

Please feel to reach out to us in future if you have any other questions (in the same way as you did with this question) where we are more than happy to help keep your deployments running smoothly.

Have a great rest of your week.

All the best,