Substitute variables in file using a script

resolved
usability
known
(Pdur) #1

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

(Paul Calvert) #3

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

(Pdur) #4

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 ?

(Paul Calvert) #5

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.

(Pdur) #7

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}
}

(Paul Calvert) #8

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
(Pdur) #9

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}

(Paul Calvert) #10

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’

(Pdur) #11

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}'

(Paul Calvert) #12

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}

(Pdur) #13

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

(Paul Calvert) #14

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.

(Paul Calvert) made this topic a personal message #15
(Pdur) #16

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?

(Paul Calvert) #17

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.

(Paul Calvert) made this topic public #18
(Pdur) #19

Thanks a lot Paul.

1 Like