Substitute variables in file using a script

I want to substitute variables in my code. The issue is, I have a shell script that takes care of deployment and i would like to that script to create variables then substitute the value using that script. once the deployment is done, I would like to delete the variables as well.
How can that be done?
the deployment script is configured in “script file in package” section

Hi @pdur,

Thanks for getting in touch!

You may be able to achieve this using output variables. They can be created during a deployment and only exist for that deployment, so you don’t need to worry about deleting them afterwards.

I hope this helps, please let me know if you have any further questions.

Regards,
Paul

thanks for the prompt reply.
I checked the concept of Output variables. And I am able to set it as well. But the thing is, can those be used in to substitute the values in files ?

They can, I’ve used a pre-deployment script to generate an output variable to then be used as a variable replacement in the same step previously.

Hi,
I tried this and am now able to set and fetch output variables. But how should I substitute them? I know that I have to use #{var} syntax in my code so that variable substitution takes place. Problem is, how will octopus know that I have to substitute these set of output variables?

Can i use something like this in my code where i need to do replacement?
{
“Host”:#{Octopus.Action[Install].Output.Host}
}

Hi,

To perform the substitution you’ll first need to enable the variable substitution in your deployment step.


Within that feature you will need to specify the file(s) that you want to replace variables in.

Then within the file itself you use the variable syntax for retrieving an output variable:
#{Octopus.Action[StepA].Output.TestResult}

So, your example (#{Octopus.Action[Install].Output.Host}) should be fine.

1 Like

Hi Paul,
I tried it but got an error

while processing event: {Update} failed: 400 Bad Request {“message”:“schema violation (host: invalid value: #{Octopus.Action[Install].Output.host})”,“name”:“schema violation”,“fields”:{“host”:“invalid value: #{Octopus.Action[Install].Output.host}”},“code”:2}

If you check the raw deployment logs do you see a line stating that the variable substitution took place, or perhaps it states that it wasn’t able to find the file?

e.g.

10:24:06 Verbose | Performing variable substitution on C:\Octopus\Applications\New\Dev\test\0.9\test.0.9\web.config’

or

10:17:52 Warning | No files were found that match the substitution target pattern ‘xxxx’

Performing variable substitution on ‘/etc/octopus/linuxworker01/Work/20200526105944-6618-186/KongDemoApiPramod.json’
10:59:47 Verbose | Parsing file ‘/etc/octopus/linuxworker01/Work/20200526105944-6618-186/KongDemoApiPramod.json’ with Octostache returned the following error: The following tokens were unable to be evaluated: '#{Octopus.Action[Install].Output.host}'

Ah, I think I know what is happening. I tested this using a package deploy step that has a feature where you can run a pre-deployment script.

As you’re using a script step instead, the variable replacement will be running before your script, causing that value to be blank.

The way around this would be to add another script step (named Setup for example) to run first that creates the output variables.
Doing it this way, you’ll just need to amend the variable in the file to reference the initial script e.g. #{Octopus.Action[Setup].Output.host}

I figured something like this as I saw one of the old blogs.
thats why I created 3 steps
1 Install (setting all the output variables)
2 packageDeploy with variable substitution enabled(this is where substitution happens)
3 Promote to higher env

It might help to enable logging of all variables values to see why it doesn’t seem to be able to retrieve that one.
If you add a project variable named OctopusPrintEvaluatedVariables with a value of true the next deployment will include this additional information.

I’ll make this topic private, so if you could then download and attach the deployment log I’ll take a look through and see what is happening.

It works now. Thanks a lot.
One short question, can the script which is referenced in the package point to other file in same package?

Absolutely, the entire package is extracted to the Octopus work folder prior to the script being run, so referencing other files that are inside that package will work fine.

Thanks a lot Paul.

1 Like

Hi Paul,
Although this is something different but it belongs to the same category.
Replacement of integer variable in octopus is giving an error. For example we have below JSON file
where string should be in double quotes and integer value should not be in double quotes.
here if i use variable replacement like
name : “#{Octopus.Action[Config].Output.A2_protocol}”
then it will work. but if i ise
ldap_port : #{Octopus.Action[Config].Output.A2_ldap_port}
then it gives an error

Is there a problem if I try to replace Integer variables in Json files?
{
“name”: “ldap-auth”,
“config”: {
“anonymous”: null,
“attribute”: “#{attribute}”,
“base_dn”: “#{base_dn}”,
“cache_ttl”: 60,
“header_type”: “Basic”,
“hide_credentials”: false,
“keepalive”: 60000,
“ldap_host”: “#{ldap_host}”,
“ldap_port”: 389,
“ldaps”: false,
“start_tls”: false,
“timeout”: 10000,
“verify_ldap_host”: false
}

Hi @pdur,

I’ve run a test on this and haven’t hit any errors. The only thing that may be worth checking is how you’re defining the variable for the ldap_port within Octopus. It should include the full hierarchy e.g.

Does that match your configuration?

yes. It does match my configuration.
also, I ran it again it started working. Don’t know what the issue was.

1 Like