Using powershell variables in deployment process steps

Hi,

I have a pre-deploy script (PreDeploy.ps1) from which I initialise a variable as such:

$hostName = "www.mysite.com"

I’d like to be able to use this from the deployment step, e.g. to set the IIS host name or the custom deployment directory. However, if I use #{hostName} as the value (see screenshot) no substitution seems to occur.

Is this possible?

We master all environment configuration values centrally, in source control. I have a script to transform this into a Poweshell script with a series of variable assignments as a part of the build process. It would be great to be able to use that rather than to manually manage the variables/variable sets within Octopus.

Thanks,
Nick

Hi Nick,

Thanks for reaching out! This blog post shows how to create a variable from a Powershell script, and how to use it on other steps.

The catch here is that the variables will be available once the step where you created them has finished. So if you want to use a variable you created from Powershell on your deployment step, you’re gonna have to do something like this:

  1. Powershell step - On this step you’ll import the contents from source control, and then create the variables using Set-OctopusVariable as shown on the link above

  2. NuGet package deploy step - On this step you’ll use the variables created on the Powershell step as shown on the link.

Let me know if it makes sense to you :slight_smile:

Dalmiro

Hi Dalmiro,

Thanks - I had seen that post, but we were actually looking for a way to
use the variables within the same step.

It’s a chicken & egg situation for us:

  • we deploy the Nuget package with contains the variable definition using a
    deployment step
  • we need to specify the destination path for the deployment
  • the destination path for the deployment is contained in the variable
    definition within the Nuget package
  • we don’t have access to the variable definition from the Nuget package in
    the deployment step

Or is there a way we can receive the Nuget package without a deployment
step?

We couldn’t get this to work so we are just using the default deployment
path, which works fine but which is not an ideal result for our scenario.

Thanks,
Nick

Hi Nick,

Variables created using Set-OctopusVariable are evaluated at the end of the step where they were created. Only after they were evaluated they become available.

Our recommendation regarding deployment paths is to use a dynamic one that is created based on values of the current deployment. The default deployment path does exactly that, creating a final path that looks a bit like C:\Octopus\Applications\[Environment]\[Package name]\[Package Version]. You can later on use the variable OctopusOriginalPackageDirectoryPath (that will hold the full path) to re-map your websites/services to the path where you deployed the package. This way you practically don’t have to do anything on your end regarding the path, as Octopus will take care of it.

If you need to use a hardcoded path (like you were using on your package) no matter what, I’d recommend you to store it on an Octopus variable instead of inside in the NuGet package.

Thanks,

Dalmiro

Thanks Dalmiro -

We would like to derive the deployment path based on variables set during
the deployment step (because the variables are set inside the nuget
package, which is created dynamically by our build process; and we can’t
access the nuget package before the deployment step runs).

I think what you are saying is that this isn’t possible currently. We’ll
use the default path in the meantime.

Thanks,
Nick

Hi Nick,

Correct, its not possible to use the variables on the same step where where they were created.

Are you starting the deployment right from the build server by any chance? if that is the case you could trigger the deployment using octo.exe and pass a prompt variable value using --variable. Then during the deployment you would use this variable for the deployment path.

Since the call to octo.exe would happen during the build, you’d have all the values to build the deployment path string available and you wont have to insert them into a file on the package.

Thanks,

Dalmiro

Hi Dalmiro,

Great, thanks - that sounds like it would work great for us, we’ll give
that a go.

Thanks for following up -

Cheers,
Nick