Using the New 'Script Steps sourced from a package' Feature in 3.3.0

I have an Apply.ps1 script in my NuGet package that I would like to execute but I would like to pass it a parameter:

Apply.ps1 -Role Web-Server

How can I achieve this using the new ‘Script Steps sourced from a package’ feature released in Ocropus Deploy version 3.3.0. It only has the option to add a ‘Script file name’. Also, does the script have to be at the root of the NuGet package?

Is it really the case that I have to create new files every time I want to pass a different parameter?

Hi,

Thanks for reaching out! Let me give you a hand with those questions

  1. How can I achieve this using the new ‘Script Steps sourced from a package’ feature released in Ocropus Deploy version 3.3.0. It only has the option to add a ‘Script file name’.

Right now its not possible to pass parameters, but you can change your script in way that it uses variables created in Octopus.

Lets say your script Apply.ps1 contains the below code

param(
[string]$role
)

write-output "The role is: $role"

And that you’ll normally invoke it like this

Apply.ps1 -Role Web-Server

Instead, change your script to be something like this

$role = $roleFromOctopus

Write-output "The role is: $role"

Where $roleFromOctopus is a variable defined in your project (as shown on the attached screenshot), or even an Octopus variable like $OctopusParameters['Octopus.Machine.Roles']

  1. Also, does the script have to be at the root of the NuGet package?

Not necesarily. If your script apply.ps1 is inside a folder called scripts in the root of the package, you could pass scripts\apply.ps1. I’ve created a github issue to get an example added for this on the UI https://github.com/OctopusDeploy/Issues/issues/2400

  1. Is it really the case that I have to create new files every time I want to pass a different parameter?

I’m not following you on that question. Why do you say you need to create a new file when you wanna add a different parameter?

Best regards,
Dalmiro

Thats pretty disappointing, can I request a feature for this? A better way for now is to run the following script using the ‘Pre-deployment script’ step:

$apply = [System.IO.Path]::Combine($OctopusParameters["Package.InstallationDirectoryPath"], 'Apply.ps1');

.$apply -Role 'Web-Server'

Also, the $OctopusParameters["Package.InstallationDirectoryPath"] parameter is super useful and should be more visible in the docs. I had to ask a question to become aware of it.

Hi,

I’ve actually created a feature request for this already. I just forgot to show it to you! https://octopusdeploy.uservoice.com/forums/170787-general/suggestions/12522837-add-a-way-to-pass-parameters-when-using-scripts-in

Please drop by and add some votes to it. I can see this feature getting implemented if we can get enough users to vouch for it.

Your pre-deployment script approach makes total sense.

Regards,
Dalmiro

My whole team has voted for this feature.

Thanks! Since your whole team is looking forward to this feature, you might as well think about adding a comment explaining why is this feature so important for your scenario. Describing a real-case scenario would be the best.

That’s the kind of feedback we usually look for when we grab these requests.

@rehansaeed & Dalmiro - I’m facing the same issue.

Could you clarify a bit when you’re referring to “pre-Deployment sript” what exactly you mean:

  1. you use PS Script step and “Script file inside a package” script source -> with Script File Name = Apply.ps1
    1.1 You’ve created script PreDeployment.ps1 and put that into your Nuget package with content:
    $apply = [System.IO.Path]::Combine($OctopusParameters[“Package.InstallationDirectoryPath”], ‘Apply.ps1’);
    .$apply -Role 'Web-Server’
    I suppose that I could refer to Octopus vars I defined in Project & Variables sets as well here? Like: .$apply -Role ‘$OctopusVarWebServer’ ?

  2. Something else?

Additional questions:

  1. What if I have 2 PS Script Steps in Ocotopus and those 2 reside within 1 Nuget Package - how could I provide PreDeploy.ps1 for each of them?
    Should I put scripts that require that workaround into different NugetFeeds?

Hi Alex,

Thanks for reaching out and for having voted & commented on the uservoice suggestion.

When @rehansaeed says Pre-Deplyoment script, he is referring to a script on the Pre-Deployment script field that shows up on the the Deploy a Package step after you’ve enabled the feature Custom Deployment Steps. Not an extra script inside of our package.

That pre-deployment script will run after the Nuget package was extracted on the work directory, and it will be executed from that same directory. This means that apply.ps1 will be available on the file system to be used like this:

.\apply.ps1 -Role $OctopusParameters['Octopus.Machine.Roles']

See attached screenshots for visual examples. The code of my apply.ps1 file is the following:

param(
$Role
)

Write-output "This script is running in a machine with the role: $role"

This could be solved by adding the proposed “Parameters” field, but for this we’ll need more users to vote for it so it scales up in our list of priorities.

Best regards,
Dalmiro