Dynamic System variables

I am using Octopus Cloud. In my powershell step, I want to dynamically reference a variable in System Variable

$referencePackage = $OctopusParameters[“PackageReference”]
$newArchiveFileName = $OctopusParameters[“Octopus.Action.Package[’$referencePackage’].PackageFilePath”]
$packageVersion = $OctopusParameters[“Octopus.Action.Package[’$referencePackage’].PackageVersion”]

How can System Variable Octopus.Action.Package reference another variable referencePackage?

Hi @vaibhavn,

Thanks for getting in touch! Great question, and from what I can see initially your syntax looks correct and it should evaluate (assuming you have the variable PackageReference defined in your project, and a value of it is applicable for your deployment via scoping). I’ll go through a test locally using your sample to see if I can spot anything. Just to clarify - you have a variable named PackageReference which contains a package ID as its value, and is being deployed within the same deployment, correct?

Best regards,

Kenny

Yes, the PackageReference is a project variable with no scope. If this variable is scoped, I cannot create release.

Refer:

Hi @vaibhavn,

Thanks for clarifying that! After some testing and researching, I think I’ve uncovered some relevant information about how this works. :slight_smile:

Firstly regarding the Octopus.Action.Package['$referencePackage'].PackageVersion variable, it seems to only be possible to call this by indexing on package ID like you’re attempting when your script step has this package as a reference (refer to doc section below).

I was able to get it to call the PackageVersion from a package deployed via a previous package step by instead specifying the package step instead of package ID like so:

$packageVersion = $OctopusParameters[“Octopus.Action[Package_Step_Name].Package.PackageVersion”]

Octopus.Tentacle.CurrentDeployment.PackageFilePath looks to not be stored as an output variable after it’s set in the package step, meaning it can only be called within the same package step and not a subsequent script step. This variable evaluated as expected when instead calling it in a PostDeploy.ps1 custom script on the package step, for example. But getting it working on the following script step, that might require storing this as an output variable then call that in your script step.

I hope that helps clarify how this works. Let me know what you think and how you go!

Best regards,

Kenny

But problem using
[“Octopus.Action[Package_Step_Name].Package.PackageVersion”]

is that Package_Step_Name is dynamic and is different for different projects. So here Package_Step_Name becomes a variable.

I also tried
$newArchiveFileName = $OctopusParameters[“Octopus.Tentacle.CurrentDeployment.PackageFilePath”]
$packageVersion = $OctopusParameters[“Octopus.Action.Package.PackageVersion”]

but not working.

Also FYI, all my script and package extraction are in same single step.

Hi @vaibhavn,

Thank you for following up! The script step and package reference within the same should allow for this PackageVersion variable to work by specifying the package ID instead of the step, so that sounds like something odd is happening. I’m also assuming you have this script step defined as a custom step template, with a package parameter defined so as to let each project select the package. Is that correct?

I think the best information to continue troubleshooting would be to have a look at a copy of your verbose task log with debugging variables enabled. These are OctopusPrintVariables and OctopusPrintEvaluatedVariables, both with a value of True. The resulting task log will have every variable written to it, before and after evaluation, and will give us a good look into what’s going on.

If you’re able/willing to send that through, you can send it privately to support@octopus.com and we can grab it there, or mark this thread as private before uploading. :slight_smile:

I look forward to hearing back and getting to the bottom of this one!

Best regards,

Kenny

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.