Get Name of Package during Script Run

I am trying to get the name of the folder that my package is being extracted to for my Run a Script step. The best way I can figure to do this is to get the name of the package.

I have tried the following variables, but they were all empty when my script runs:

$OctopusParameters["Octopus.Action.Package.InstallationDirectoryPath"]
$OctopusParameters["Octopus.Release.Package"]
$OctopusParameters["Octopus.Action.Package.PackageId"]

I can go to the release page and see that there is a package there. And when I run Get-ChildItem . in my script I can see the folder for my package is created. (But I am making a template, so I cannot just hard code the folder name.)

How can I get the name of the package (or folder) for my Run a Script step?

Hi @OctopusSchaff,

Thanks for getting in touch!

If this package is a referenced package within the script step, then you should be able to use the Octopus.Action.Package[Acme].ExtractedPath system variable.

Regards,
Paul

I tried that like this:

$pathToPackage = $Octopus.Action.Package["SangerWorkflowManager"].ExtractedPath
Write-Output "Package Extracted Path: $pathToPackage"

When I do that, I get this error:

InvalidOperation: Cannot index into a null array.

This tells me that the Package array is empty. (Which is confirmed by my other tests.)

Just to forestall any concerns that I don’t have a package attached to my release. Here is a screen shot of it in my release:

The name of the step I am doing my PowerShell script in is called “Deploy Release”.

Also, when I run Get-ChildItem . I can see the extracted package in the list of folders. (So I know that the package is there and being extracted. I just need to get its name programmatically.)

The “Deploy Release” step is a script step (not a normal IIS type deployment) that is running on a worker. Does a script step on a worker populate different variables than a normal IIS type deployment to a target?

A referenced package within a script step will use different variables to a package within a package deploy step (such as a Deploy to IIS step), but the fact it is running on a worker shouldn’t matter.

The best option may be to add the OctopusPrintEvaluatedVariables system variable with a value of true to your project, which will output all available variables to the task log. You should then be able to find the correct one that includes the folder location.

When I run with that variable set, this is one of the variable values it prints out:

[Octopus.Action.Package[SangerWorkflowManager].PackageId] = ‘SangerWorkflowManager’

But I am still getting this error when trying to get that value:

InvalidOperation: Cannot index into a null array.

Maybe my powershell is wrong? This is how I am trying to reference this in my Powershell script:

$packageId = $Octopus.Action.Package["SangerWorkflowManager"].PackageId
Write-Output "Package ID:  $packageId"

Is this syntax correct?

Hi @OctopusSchaff,

I’m stepping in for Paul on this one, but I’m happy to help!

In doing some testing on my side, the following syntax worked for me (where RandomQuotes is my package name):

Write-Host "Package ID: " $OctopusParameters["Octopus.Action.Package[RandomQuotes].PackageId"]

Hopefully this syntax works for you too, but let me know how it goes, and I would be glad to assist further if needed.

Regards,

Britton

That syntax works! But, now that I have working syntax, I still need a way to get the name of the package, without needing the name of the package.

To clarify, in your example, the name of the package is “RandomQuotes”. But you need to put that in your code (Between the [ ] ) to get the PackageId.

Is there a way to list all the Referenced Packages?

I tried this:

$packages = $OctopusParameters["Octopus.Action.Package"]
foreach ($package in $packages) {
    $packageId = $package.PackageId
    Write-Output "Package Id = $packageId"
}

But the $packages variable was empty.

I also tried this:

$packageId = $OctopusParameters["Octopus.Action.Package[1].PackageId"]
Write-Output "Package Id = $packageId"

But $packageId was empty.

Is there a way I can list all of the Referenced Packages? (or somehow get my package name without needing my package name to get it?)

Hi @OctopusSchaff,

Just stepping in for Britton while he’s offline, that’s a really great question which unfortunately I haven’t found a great answer for just yet so I’ll definitely be digging into it further.

Our System Variable docs mentions that the Reference package variables are available per package and I’m not able to spot any Action level variables that will give a list of packages so it doesn’t seem possible to use System variables but I’ve asked around for ideas: System variables | Documentation and Support

One potential workaround if the packages are being extracted, would be to grab the package names from the folders being created (by default in the current working directory). E.g

$packages = Get-ChildItem . -Directory -Name 

foreach ($package in $packages) {
	write-host "Package ID: $package"
}	

I’ll keep you posted if I have any other suggestions from the internal discussion or if any Github issues get raised.

Hope that helps but feel free to reach out with any questions at all!

Best Regards,

Hi @OctopusSchaff,

Just an update from the internal discussion, one of the devs provided the following Powershell for using wildcards with our System Variables, so it should work for any type of referenced package:

$keys = $OctopusParameters.Keys | Where {$_ -like "Octopus.Action.Package*.PackageId"}
$packages = $OctopusParameters[$keys]

foreach ($package in $packages) {
    Write-Output "Package Id = $package"
}

Feel free to let us know if there are any issues with it or you have any questions at all!

Best Regards,

1 Like

This worked perfectly! Thank you!

3 Likes

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