Delete feature branch releases

We are using Octopus with TeamCity to automatically deploy feature branches. And we faced with a problem that we are running out of disk space very quickly.

So I’m trying to find a way how to delete “outdated” releases with all related files included.
TeamCity builds solution and creates Octopus release with four NuGet packages. Octopus deploy release to Tentacle which creates folders for every package in \Octopus\Application<Environment Id><Package Id><Version>. If the developer has committed something into the same feature branch (which is quite normal) a new release is created and deployed which create new folders and so on. The same is true for every single feature branch.

We can’t rely on Retention policy because nobody knows how many version is going to be deployed.

I was thinking about adding post deploy task, which will filter all releases by feature branch pattern and delete old ones. Like if I have releases in format: 1.0-feature1.19.97, then delete old release using octo.exe delete-releases --server=http://xxxxxxxx.com/ --apiKey=API-KEY --project=Project1 --minversion=1.0-feature1.0.0 --maxversion=1.0-feature1.19.96; Unfortunately, it works not in the way I’m expecting: releases are deleted from Octopus server but folders still remain on Tentacle machine.

Any ideas how to handle this situation would be very appreciated.

Hi,

Thanks for getting in touch.

Unfortunately the answer is a bit convoluted. There is currently not an easy way to clean up releases from Tentacle besides through retention policy. We have an open suggestion that you could support: https://octopusdeploy.uservoice.com/forums/170787-general/suggestions/18644866-advanced-tentacle-cleanup-for-deleted-octopus-item

In the meantime here is what you could do:

  • create a channel per feature branch
  • change the retention policy set for the project to be the channel name
  • create a project that can clean up all deployments for a specific retention policy set

A channel per feature branch is probably a good idea anyway. For each branch you would create a new channel, “feature-foo”, “feature-bar” and create releases for those branches only into the respective channel. There is help here if you need it: https://octopus.com/docs/key-concepts/projects/channels

The next step would be to configure Octopus to use the channel name as the retention policy set. You can do this by adding a variable to your project called “OctopusRetentionPolicySet” and setting the value to “#{Octopus.Release.Channel.Name}”. Be ware that adding this to multiple projects will cause the same retention policy to be applied across those projects. So if you had the same channel name in multiple projects and had a retention policy of keep 1 release, only one release total across the projects would be kept.

The third step is to run a script to delete deployments that belong to a channel’s retention set. If you wanted to delete the branch “feature-foo”, just run the script inputting “feature-foo”. I have set this up as a project with a run script step:

[xml]$deploymentJournal = Get-Content -Path $env:TentacleHome\DeploymentJournal.xml

$deploymentsToDelete = $deploymentJournal.Deployments.Deployment | Where-Object {$_.RetentionPolicySet -eq $FeatureBranch}

foreach ($deploymentToDelete in $deploymentsToDelete) {    
    Write-Host "Cleaning application folder $($deploymentToDelete.ExtractedTo)"
    Remove-Item -Recurse -Force $deploymentToDelete.ExtractedTo
    Write-Host "Removing package $deploymentToDelete.ExtractedFrom"
    Remove-Item $deploymentToDelete.ExtractedFrom

    $deploymentJournal.Deployments.RemoveChild($deploymentToDelete) | Out-Null
}

$deploymentJournal.Save("$($env:TentacleHome)\DeploymentJournal.xml") | Out-Null

There is a variable called “FeatureBranch” that needs to be created for that project. I have configured it as a prompted variable so each time I go to clean up the Tentacle it will prompt for the branch to delete.

The side effect of doing this will be that you could use retention policy to keep n releases of each feature branch on the Tentacle.

I hope this helps and is not too confusing. Please let me know if you have any questions.

Cheers,
Shane

Hi, Shane!

Thank you very much for such detailed explanation. I really appreciate it.
I have a few more question if you don’t mind:

A channel per feature branch is probably a good idea anyway. For each branch you would create a new channel, “feature-foo”, “feature-bar” and create releases for those branches only into the respective channel

As I don’t know how many feature branches would I have, I need to create channels dynamically, is there a good way to achieve this?

Also, I’ve found some information about Retention policies with Channels (Retention policy Tentacle cleanup and troubleshooting | Documentation and Support), but I can’t find Discrete Channel Releases flag at under Deployment Target settings, any special setting is needed?

Regarding script you have provided, it looks like it will delete all deployments for branch instead of all previous deployment.

Anyway, I didn’t know about the possibility of running scripts on Tentacle, now I’m flexible to write my own which can delete the previous releases based on my custom logic.

Hi,

I need to create channels dynamically, is there a good way to achieve this?

You can use Octo.exe create-channel to achieve this.

I can’t find Discrete Channel Releases flag at under Deployment Target settings

This will only show in Octopus 3.12.2+ and when a project has more than one channel.

Yes, the script I sent will delete all releases for a given channel. Sorry, I thought you wanted to clean up an entire feature branch when you no longer needed it. It sounds like using channel retention policy will get you most of what you want and a custom script running on Tentacle can do the rest.

Cheers,
Shane