Nuget Variable Substitution Question

Hi,’

My company has started using Octopus and I am have been busy deploying my first package and one thing I am stuck on is how to use variables in my NuGet package.

I have created a variable in Octopus “DeploymentPath” and have tried to add this variable into both the .NUSPEC folder with many different convensions such as:
${DestinationPath}
#{DestinationPath}
$DestinationPath$

eg. as follows:


However it does not substitute the variable name with the variable value. I am trying to figure out to dynamically change this based on the environment.

Can anybody help me?

Thanks,
Damien

Hi Damien,

Thanks for getting in touch, and welcome to Octopus and the community!

Since the <files> section you pasted seems to have been stripped out on this thread, I’ll just paste it below for easy reference.

  <files>
    <file src="content\**" target="${DestinationPath}" />
  </files>

This is used to add additional files to the package, where src is where the files are before including them, and target is where to include them in the directory structure of the package. $DestinationPath$ is the correct syntax for using NuGet replacement tokens, but maybe you just need to define them with the -properties switch in the pack command? e.g. -properties DestinationPath="VALUE". The following doc page has more information detailed in regards to replacement tokens.

Does that help you achieve what you’re after? Please let me know if I’ve misunderstood your scenario in any way. :slight_smile:

Best regards,

Kenny

Hi Kenneth,

Really appreciate your reply! Yes, I am brand new to Octopus and read the documentation and watched the youtube videos and struggling a little bit :frowning:

I tried following the steps you provided. I updated the Target attriute with ${DestinationPath} and passed the -Properties when running the nuget pack command, however yes it substituted the path with the value provided in the Properties, however it didn’t substitute with the actual Variable value within Octopus…

Basically the intention for this is so that I can dynamically update the deployment values depending on the environment being deployed to. Basically the development team are creating a package in which placeholder values should be added to the target values. They should not be specifying targets and instead Octopus should be determining target locations depending on the environment being deployed to.

My intention on using the variables to to achieve this transformation at the time of deployment. Is there a better way of doing this than dynamically updaing the values coming from the .nuspec file? Is there another type of config file that can be added to the package that we can read and determine deployment properties on a environment basis?

Thanks,
Damien

Hi Damien,

Thanks for following up! There is certainly a bit of a learning curve. :slight_smile:

So what this is doing in your nuspec is defining any external files in which you want to include into your package. This is before the package gets to Octopus, so variables defined in your Octopus project won’t be applicable quite yet.

If you have all of your files you want in your package, you can upload it to the built-in package repository which can then be specified in your project to deploy it to your targets. In the package step you can enable the substitute variables in templates feature to specify the files within the package being deployed to perform variable substitution in. Octopus will find these files during the deployment, and replace values defined in Octopus on variables matching Octopus’ variable substitution syntax (e.g. #{VariableName} in a text file will be replaced with the value of a variable in Octopus named VariableName).

I think that might be the feature you’re after?

I hope this helps, and please let me know if I can try to help more moving forward.

Best regards,

Kenny

Thanks Kenny, I have tried following your steps and does not seem to be working… My Steps are below:

.nuspec file:
image

Contents of “content” source folder:

image

Both testconfig.txt and testconfig.txt.config both contain the following

Note the use of #{ServerName}

Variable “ServerName” is setup in Octopus:

image

Octopus Process contains the following configuration:

image

After loading package, creating new release and deploying, I receive the following errors:

image

Really appreciate all your help with this… I am very excited as to what I will be able to achieve with Octopus!

Thanks,
Damien

Hi Damien,

You are more than welcome! Thanks for following up with that great level of detail.

At first read through it looked like everything was set up as it should so I set up a local test reproducing these details and I finally spotted what I think the issue might be - you’re specifying the pattern **\contents\*.txt where testconfig.txt exists in a folder named \content\ (no s). Updating the target pattern to match that should get testconfig.txt to substitute as expected. However you’ll also need to specify the .config file type as well to substitute variables in the same way into testconfig.txt.config.

Note after any changes to the deployment process or variables, you’ll need to create a new release to apply the changes.

I hope this helps progress things! Let me know how you go or if you have any further questions or concerns. :slight_smile:

Best regards,

Kenny

Thanks Kenny! Yes, I removed the S and tried again and also removed the first pattern .txt and all files ending in .txt in that directory now get substituted. Thankyou for your fast replies!

Hi Damien,

Thanks for the update, and that’s great to hear! Don’t hesitate to reach out if you have any questions or concerns we can help with in the future. :slight_smile:

Best regards,

Kenny