Runbook access to Octopus.Release.PreviousForEnvironment.Number

Hi,

Is it possible to access Octopus.Release.PreviousForEnvironment.Number from within the Runbook? It seems to be available only in Deployment projects.

The following returns an empty string:
$v = $OctopusParameters[‘Octopus.Release.PreviousForEnvironment.Number’]
Write-Output “Latest version: $v”

We need to get the latest version deployed to the environment as part of the maintenance procedure we want to build via Runbook.

Thanks.

Hi @canman,

Thanks for getting in touch! Currently this is not available in Octopus, though it sounds entirely reasonable to expect it to work. I’m going to bring it up with the team to see if this sort of behavior should be implemented.

In the meantime, there are a couple of options available to you. The first would be to use our API in a step during your runbook to reference the dashboard to return the desired release and associated version. As the dashboard represents the latest successful releases to each environment for each project, this information should be exactly what you are after.

Below is a simple script I put together that you can edit and make your own to best meet your needs if you decide to go with the API.

Add-Type -Path 'C:\MyScripts\Octopus.Client.dll'

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint("http://YourServer/", "API-KEY")
$client = New-Object Octopus.Client.OctopusClient($endpoint)

$envName = "Prod"  # Enter environment name.
$projName = "abc" # Enter project name.

$deployments = $client.Repository.Dashboards.GetDashboard().Items | ? {$_.EnvironmentId -match $client.Repository.Environments.FindByName($envName).Id -and $_.ProjectId -match $client.repository.projects.FindByName($projName).Id}
$deployments.ReleaseVersion

An alternative option is to use Octo.exe which can List latest deployments, though a bit more verbose.

octo.exe list-latestdeployments --project=NAME --environment=NAME --server=http://YourServer/ --apikey=API-KEY

Let me know if this helps or if you have any questions at all.

Best regards,
Daniel