How to get the "Previous Release number" for the Project and Environment you are currently deploying to

Hi!,

Ok - I’m trying to build an confirmation email step, and I need a system variable that will show the “last release deployed to a specific environment/Project combination.” I know there is Octopus.Release.Previous.Number, but it appears that is showing the previous release for “the project”, and not taking into account that the previous project release might not have been deployed to the current environment.

I need this variable to create a delta report that dev teams can look at and see what changes will be pushed to the environment. any help is much appreciated!

Thanks,

Hi Chad,

Thanks for getting in touch! It appears we do not have a specific variable for that. We do however have the following Octopus.Tentacle.PreviousInstallation.PackageVersion and if you happen to use the release version to match the package version then this will work for you. You would have to define it in a PowerShell step and then call it from the email step.

Please let me know if this would suit.
Vanessa

Thanks Vanessa for the response. I’ve changed our setup so that the package version number will match the release version. However - Could you explain a bit more about “defining it in a powershell step, and then call it from the email step”?

How would I do that?

thanks!

Hi Chad,

So as part of your package deployment inside the deploy.ps1 you add the following line:
Set-OctopusVariable -name "Package1PreviousVersion" -value $OctopusParameters['Octopus.Tentacle.PreviousInstallation.PackageVersion']

Then inside your email step you can call it as such: #{Octopus.Action[STEPNAME].Output[MACHINENAME].Package1PreviousVersion}
You will need to change the step name to the package step name, and the machine name to the nominated machine name.

Vanessa

Hi Vanessa,

That worked thanks! however, my requirements have changed a bit, due to requests from our team :confused:

No matter what environment I’m deploying to, i need to access the current production release version. I can do this using your solution, if i’m deploying to production, but when i’m deploying to staging, or UAT, i can’t figure out a way to get the current production release. One possible solution is to write the current version to disk somehwere via powershell, then grab that, but it’s not that elegant of a solution. Instead, is there any way I could write to some of global project variable in octopus deploy(like the ones on the variable tab) and then read from it at a later point in time? (ex. create a project variable on the variables tab called “productionVersion” and then update it once we roll out to production…then i could access at a later point?

Thanks.

Hi Chad,

This isn’t available as a variable, but you could get it using the API.

For an example, see here:

https://demo.octopusdeploy.com/api/dashboard/dynamic

You can also restrict it to the current project and a given environment (E.g., prod):

https://demo.octopusdeploy.com/api/dashboard/dynamic?environments=Environments-33&projects=projects-65

(You’ll have to click Sign in as Guest, then reload the links)

Hope this helps, if you need any more information just let us know. Thanks!

Paul

I would love to see this as a variable in a future update.

Hi Shane,

There is a variable called $OctopusParameters[‘Octopus.Release.Previous.Number’] which contains the latest release created. The problem is that it is not environment-aware and it just returns the previous release, even though that one wasnt deploy to the environment you are currently deploying.

I’ve created a github issue to have this variable be environment-aware

Thanks!

Dalmiro.

this seems to do the job.

public ReleaseResource GetPreviousRelease(ProjectResource project, string environmentName)
{
DashboardResource dr = this.repository.Dashboards.GetDashboard();
DashboardItemResource dir = dr.PreviousItems.Where(x => x.ProjectId == project.Id).Where(x=>x.EnvironmentId == this.GetEnvironmentId(environmentName)).First();
ReleaseResource previousRelease = this.repository.Releases.Get(dir.ReleaseId);
return previousRelease;
}

Using the variable $OctopusParameters[Octopus.Release.PreviousForEnvironment.Number] should be a bit easier for this

How do I do that using the .net repository?

Sent via the Samsung Galaxy Note® Edge, an AT&T 4G LTE smartphone

Hi Steve,

Your approach would do using the .NET client. There’s no object property that holds this value, as we calculate it during the deployment.