Passing parameters to scripts

Octopus can pass parameters to your custom script files for any of the supported scripting languages. This means you can use existing scripts, or write and test your own parameterized scripts that have no knowledge of Octopus, passing Octopus Variables directly to your scripts as parameters. The Octopus scripting API is still available within the context of your script, meaning you can use a mixture of parameters and other Octopus variables and functions.

Consider this example PowerShell script:

PowerShell script using Octopus Variables

$environment = $OctopusParameters["Octopus.Environment.Name"]
Write-Host "Environment: $environment"

You can parameterize this script making it easier to test outside of Octopus:

PowerShell script using parameters

param (
	[Parameter(Mandatory=$True)]
	[string]$Environment
)
Write-Host "Environment: $Environment"

When you call external scripts (sourced from a file inside a package) you can pass parameters to your script. This means you can write “vanilla” scripts that are unaware of Octopus, and test them in your local development environment.

You can define your parameters in the Script Parameters field using the format expected by your scripting execution environment (see below for examples).

Script Parameters

Delimiting string values Don’t forget to correctly delimit your parameters correctly for the scripting engine. In the example above we have surrounded the parameter value in double-quotes to handle cases where the Environment Name has spaces: "#{Octopus.Environment.Name}"

Passing parameters to PowerShell scripts

You can pass parameters to PowerShell scripts as if you were calling the script yourself from PowerShell, using positional or named parameters.

Script Parameters in Octopus

-Environment "#{Octopus.Environment.Name}" -StoragePath "#{MyApplication.Storage.Path}"

Usage in PowerShell script

Param (
	[Parameter(Mandatory=$True)]
	[string]$Environment,
	[Parameter(Mandatory=$True)]
	[string]$StoragePath
)
 
Write-Host "$Environment storage path: $StoragePath"

Passing parameters to C# scripts

You can pass parameters to C# scripts as described here for the ScriptCS engine. ScriptCS only supports positional parameters.

Script Parameters in Octopus

-- "#{Octopus.Environment.Name}" "#{MyApplication.Storage.Path}"

Usage in C# script

var environment = Env.ScriptArgs[0]
var storagePath = Env.ScriptArgs[1]
Console.WriteLine("{0} storage path: {1}", environment, storagePath);

Passing parameters to Bash scripts

You can pass parameters to Bash scripts as described in Bash manual.

Script Parameters in Octopus

"#{Octopus.Environment.Name}" "#{MyApplication.Storage.Path}"

Usage in Bash script

environment="$1"
storagePath="$2"
echo "$environment storage path: $storagePath"

Passing parameters to F# scripts

You can pass parameters to FSharp scripts as described by the F# documentation.

Script Parameters in Octopus

"#{Octopus.Environment.Name}" "#{MyApplication.Storage.Path}"

Usage in F# script

let environment = fsi.CommandLineArgs.[1]
let storagePath = fsi.CommandLineArgs.[2]
printfn "$s storage path: $s" environment storagePath

Passing parameters to Python3 scripts

You can pass parameters to python scripts as described by the python documentation.

Script Parameters in Octopus

'#{Octopus.Environment.Name}' '#{MyApplication.Storage.Path}'

Usage in Python3 script

environment=sys.argv[1]
storagePath=sys.argv[2]
print("Parameters {} {}".format(environment, storagePath))

Note: If your python scripts make use of argparse, it’s possible you might encounter an error at execution time, as Calamari bootstraps the execution of the python script as part of the deployment or runbook run.

Help us continuously improve

Please let us know if you have any feedback about this page.

Send feedback

Page updated on Sunday, January 1, 2023