Multiple packages in custom step

(Hakan Lindestaf) #1

I’m writing a custom step (Powershell) where I’m going to update the image tag in AWS ECR. My process may have multiple packages (from an ECR Feed), and I see the Octopus.Action.Package.PackageVersion property, but what if there are multiple packages, how will I get a list (so I can iterate)? And I actually need to get the repository name as well, would that be available somewhere?

The best solution would be if something was added to Octopus that would automatically tag an image from a package feed when it’s been deployed, so I wouldn’t have to do a custom step like this. I believe when Octopus is using the built-in package management then you don’t delete packages that are deployed/in-use, this is the same thing for the external feed (specifically for AWS though I assume).

(Matthew Casperson) #3

Hi Hakan, thanks for reaching out.

Support for multiple packages was added in a recent version of Octopus. There is a blog post at https://octopus.com/blog/script-step-packages#package-variables that documents the process, and also lists the variables that are exposed to the step for each additional package. The variables take the form Octopus.Action.Package[PackageName].Property. The Octopus variable replacement syntax also supports looping over indexed variables like this, which is documented at https://octopus.com/docs/deployment-process/variables/variable-substitution-syntax#VariableSubstitutionSyntax-Repetition.

A useful trick for inspecting the variables that are exposed to your script is to use the debugging feature documented at https://octopus.com/docs/support/debug-problems-with-octopus-variables#DebugproblemswithOctopusvariables-Writethevariablestothedeploymentlog. With the OctopusPrintEvaluatedVariables set to true, your deployment logs will list all the variables that you can use in that step.

If automatic tagging of an image during deployment is a feature that would be valuable to you, you can create a suggestion in the Octopus User Voice page at https://octopusdeploy.uservoice.com/.

However, depending on your use case, you may get some value from channel rules. These rules can be used to limit the packages that are available for deployment based on the version tags. This feature is documented at https://octopus.com/docs/deployment-process/channels.

You are correct that when a package is deployed it is not deleted from any feed managed by Octopus.

Regards
Matt C

(Hakan Lindestaf) #6

Matt,
thanks for your quick response. So that helped me a little, but the issue is that in my process I have 4 steps. Two of them are deploying two different images to a K8s cluster from ECR, so in each of those steps the Octopus.Action.Package variable is set with the correct package. But then I go down to my last step, which is to tag all images in the ECR that they are now deployed. I then want to iterate through all used packages (from all previous steps) and do the WriteTag-stuff on each one. I enabled the output of variables, but since that step doesn’t directly use any of the packages the Octopus.Action.Package variable isn’t set (empty), so there’s nothing to iterate. I saw your link about the variable replacement, although it’s useful, but in this case I’m writing PowerShell and the variable doesn’t even seem to be set.
I added the suggestion to UserVoice.

I basically need a way to list all packages used in a process in one of the steps (like when I click “Create Release” it shows all used packages, that’s the data I need, but inside the last step).

(Matthew Casperson) #7

Hi Hakan,

The packages used by previous steps are exposed in variables like Octopus.Action[StepName].Package[PackageName].PackageId. So you can loop over every previous step and the packages they used with a script like:

#{each action in Octopus.Action}
    #{each package in action.Package}
        Write-Host "#{package.PackageId}"
    #{/each}
#{/each}

In this example I have printed the package ID, but any action could be take inside the nested each loop.

Regards
Matt C

(Hakan Lindestaf) #8

Ah, that’s great, wasn’t thinking of that. And I didn’t realize the variable replacement code could be used in PowerShell, cool, I’ll give this a shot!

(Hakan Lindestaf) #9

That worked great, thanks for your help!