NuGetPackageVersion form a list of packages names

Hi,

I wanted to get all package versions in the deployed project.i tried to use

#For step “Deploy Database”
$OctopusParameters['Octopus.Action[Deploy Database].Package.NuGetPackageVersion'] and it worked. But its a repetitive work as i have to do the same for all packages and i tried to loop it through in power shell like below. I tried to make it as a template so i can use the template for other projects too.

$packages = $OctopusParameters['Packages']

foreach ($package in $packages.split(';')) 
{
  write-host "package name is $package"

  $PkgVersion = $OctopusParameters['Octopus.Action[$package].Package.NuGetPackageVersion']

  write-host "package version is $PkgVersion"

}

so i am looping through a ;(e.g. Deploy Database;Deploy WebUI) list and trying to get the NuGetPackageVersion for each package. The $package variable displays name properly but the $PkgVersion is always empty.

Cant we access the Octopus.Action[$package].Package.NuGetPackageVersion access a variable?

Please suggest.

Thanks,
Rasmita

Hi Rasmita,

Thanks for reaching out.

I believe the issue we’re running into with your code is in the Octopus.Action[$package]… section, the $package should be the step name.

You would need to iterate through all the step names with packages associated with them and then get the variables, [Octopus.Action.Package[STEPNAME].PackageId] and [Octopus.Action.Package[STEPNAME].PackageVersion] (or .NuGetPackageVersion).

A way you could automate this would be to go through the deployment process json of the project, find all the steps that have an associated package, create an array of the step names, then iterate through that array on those variables.

Pseudo Code:
API Call to /api/Spaces-1/deploymentprocesses/deploymentprocess-Projects-###
Iterate through all steps, when Packages is not empty, add “Name” to array.
Iterate through array of step names to get the desired Package Ids and Version Numbers using the system variables above.

Please let me know if that makes sense and works, or if we need to dig a bit deeper.

Thanks,
Jeremy

May be i confused you with my previous explanation. I am passing all step name with the $OctopusParameters[‘Packages’] parameter to the power shell and the $package contains the step name. So what you are saying i am also doing but the step name is getting passed as a variable to here
[Octopus.Action.Package[$package].PackageVersion]

My question is how can i pass the step name as a variable?


and this is how i am passing the list to the powershell

Hey Rasmita,

My apologies, I thought it was the package names.

I did some tinkering and changing the line to this syntax worked for me. $PkgVariable = $OctopusParameters["Octopus.Action[$($package)].Package.NuGetPackageVersion"]

Here is my final code, i changed some variable names:

$steps = $OctopusParameters['Packages']

foreach ($step in $steps.split(';')) 
{
  $packageId = $OctopusParameters["Octopus.Action[$($step)].Package.NuGetPackageId"]
  $PkgVersion = $OctopusParameters["Octopus.Action[$($step)].Package.NuGetPackageVersion"]
  write-host "Step $($step) contains package $($packageId) version $($pkgversion)"
}

It results in output like this

Step Deploy contains package DateFormattingSub version 1.0.5

Step Deploy a Package contains package Octopus.Client version 1.0.0

Please let me know if that works for you.

Thanks,
Jeremy

Thank you. It worked.

Thanks,
Rasmita

1 Like

Hi Rasmita,

You’re very welcome. Thank you for letting me know it worked.

I hope you have a great rest of your week.

Best,
Jeremy