Access to the Lifecyle retention policy parameters as variable

Hello,

How can I access to the lifecyle retention policy parameters as variables into my process steps to have the number of release to keep?

Regards,

Benjamin V.

Hi Benjamin,

The variable $OctopusParameters['OctopusRetentionPolicyItemsToKeep'] would return the number of releases to keep by the retention policy. Is this what you were looking for?

Thanks,

Dalmiro

Hello,

Thank you for the answer but no, it look like this variable is empty I have add a very simple powerhsell step to test like this:
Write-Host Retention backups: $OctopusParameters[‘OctopusRetentionPolicyItemsToKeep’] but the result is just:
Executing script on tentacle 'XXX’
Retention backups:

Hi Benjamin,

Did you escape the Powershell variable using the $() sintax? This works on my end:

Write-Host "Retention backups: $($OctopusParameters['OctopusRetentionPolicyItemsToKeep'])"

If it still doesn’t work for you, please follow these steps to send us a deployment log that shows the values of your variables so we can see why its not showing up:

  1. Add these 2 variables to your project http://docs.octopusdeploy.com/display/OD/Debug+problems+with+Octopus+variables

  2. Create a new release (so the new variables take effect) and deploy it. If possible skip as many steps as you can and only leave step we are troubleshooting in order to avoid the noise in the log.

  3. Send us the raw log of that deployment http://docs.octopusdeploy.com/display/OD/Get+the+raw+output+from+a+task

Thanks,

Dalmiro

Still doesn’t work. I have attached the logs to this message.

Regards,

Benjamin

ServerTasks-408.log.txt (219 KB)

Hi Benjamin,

The variable $($OctopusParameters['OctopusRetentionPolicyItemsToKeep']) is only available when your retention policy’s phase is NOT set to “Keep all”.

Is it possible that the phase you are deploying to has a retention policy set to “keep all” as shown on the attached screenshot?

Dalmiro

Ok, when I set the “Default Retention Policy” it work I get the Retention policy of the curent phase. It sound like a bug but setting the default is a workaround who work for me thank you!

Another question about this how can I set it as paramter of a step? I have use:
#{OctopusParameters[‘OctopusRetentionPolicyItemsToKeep’]} but I didn’t retreive it as a value

Hi Benjamin,

Regardless of what you have for the “default retention policy”, you should be getting the value of the current phase. I’ve double tested it on my end and it prints the correct values for each phase.

You can use $($OctopusParameters['OctopusRetentionPolicyItemsToKeep']) as a Variable in any step, but not as a parameter. Could you explain a bit more what you are trying to do so I understand the context?

Thanks,

Dalmiro

For the point 1, yes the value is correct it is the value of the current phase. It is what I was trying to say in my bad english :wink:

About what I try to do: I have create a custom step template to perform a backup and another step template to only keep the last X backups. So for this second step template, I have a parameter with the number of backups to keep. Of course I can hardcode the value but I use this template 4 times and I don’t want to apply this logic to base it on the retention policy of Octopus each time.

Regards,

Hi Benjamin,

Instead of having a parameter with the number of backups to keep, you could just use the variable $($OctopusParameters['OctopusRetentionPolicyItemsToKeep']) directly on your template. Import this step template example on your Octopus to see what I’m talking about:

{
  "Id": "ActionTemplates-1",
  "Name": "Print RetPol items to keep",
  "Description": null,
  "ActionType": "Octopus.Script",
  "Version": 17,
  "Properties": {
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "#Using the variable straight on the script\n$itemsToKeep = $OctopusParameters['OctopusRetentionPolicyItemsToKeep']\n\nwrite-output \"Items to keep on this lifecycle: $itemstokeep\""
  },
  "SensitiveProperties": {},
  "Parameters": [],
  "$Meta": {
    "ExportedAt": "2015-10-06T14:58:47.694Z",
    "OctopusVersion": "3.1.3",
    "Type": "ActionTemplate"
  }
}

Dalmiro