I have a Custom Step Template with script source set to source code, with the script in f# but I cannot find anyway to get the parameters passed through. I have a parameter setup called Site, which displays in the parameters tab as #{Site}
If I do (which is what you get when you pick the variable via the picker tool)
System.Console.WriteLine(Site)
Script.fsx(7,26): error FS0039: The value or constructor ‘Site’ is not defined
System.Console.WriteLine(#{Site})
Outputs ‘#{Site}’ i.e. just the literal string
System.Console.WriteLine(Octopus.Parameters[“Site”])
Script.fsx(3,34): error FS0039: The value, constructor, namespace or type ‘Parameters’ is not defined
What is the syntax to pass parameters into an fsharp script?
Hi Ian, thanks for reaching out.
You can find more information on using variables in F# scripts at https://octopus.com/docs/deploying-applications/custom-scripts#Customscripts-Variables.
For example:
System.Console.WriteLine("#{Site}")
System.Console.WriteLine(Octopus.findVariable "Site")
Regards
Matt C
Hi,
Thanks for you help, I’ve managed to get this working but just a couple of things to note, as you seem to be an employee:
I’ve just tested on Octopus 3.14.1592.
The first example:
System.Console.WriteLine("#{Site}")
Just outputs the literal ‘#{Site}’ rather than the value of the variable. So I may have found a bug. I’ve exported my step and attached it to help with any replication.
The second example:
System.Console.WriteLine(Octopus.findVariable “Site”)
However, this function is not mentioned in the linked documentation, so would be good to update it to include this.
Cheers,
Ian.
exported.txt (990 Bytes)
*The above should say the second example works
Hi Ian. Thanks for the feedback on the docs.
The literal #{Site}
will be printed if the variable is not defined or available. In your test System.Console.WriteLine("#{Site}")
printed the literal while System.Console.WriteLine(Octopus.findVariable "Site")
printed the variable with no other changes to the script? If so that is definitely a bug. In that case could you also attach a screenshot of the variables page?
Regards
Matt C
Hi,
I deleted my original test, so have created another replicating the issue with screenshots of the input and output.
Cheers,
Ian.
export.txt (1 KB)
Hi Ian. You are correct - the #{Site}
format doesn’t work for step template parameters. It does work for project variables, but for consistency you can use Octopus.tryFindVariable
, Octopus.findVariable
or Octopus.findVariableOrDefault
.
I’ve included some examples below.
// It's a good idea to copy the value into a variable to avoid quoting issues
// tryFindVariable : name:string -> string option
let connectionString = Octopus.tryFindVariable "MyApp.ConnectionString"
match connectionString with
| Some x -> printf "Connection string is: %s" x
| None -> printf "Connection string not found"
// Or one of the simplified versions
// Throws KeyNotFoundException when variable does not exist
// findVariable : name:string -> string
let connectionString = Octopus.findVariable "MyApp.ConnectionString"
// Returns default value when variable does not exist
// findVariableOrDefault : defaultValue:string -> name:string -> string
let connectionString = Octopus.findVariableOrDefault "Default Value" "MyApp.ConnectionString"
Regards
Matt C