Replace entire appsettings.json with environment version

Hello!

My question is regarding .netcore appsettings.json files. Currently, we have an innapropriate number of appsettings values and we have 4 appsettings files (for each environment). Is it possible that, in the beginning, I could just grab the environment version i need and rename it to appsettings.json on deployment instead of house each variable in config?

Hi Jeremiah,

Welcome to Octopus Help and thank you for contacting us about variables in appsettings.json files. You mention an ‘inappropriate number of appsettings values’ and I’m curious as to what you mean? I ask as it may help me understand why you are asking about appsettings variables.

With regard to your question, when working with variable substitutions we recommend using Structured Configuration Variables.

That way you only have one appsettings.json file in the code package. And rather than having placeholders in your appsettings.json file, you specify the variables in the deployment’s Variables tab and then scope each variable value with the environment/s you want it to be used in. Octopus will then replace these with the correct environment-specific values during deployment.

You enable the Structured Configuration Variables field by clicking the Configure Features button, then tick Structured Configuration Variables, and then update Variables to include values for each environment as required.

Regards

Mark

Mark Lamprecht

Solutions Architect | St Ives, Cambridgeshire, UK
OctopusDeployLogo

Hi Mark,

I think eventually I will be evolving toward complete use of structured configuration variables but today, many of the applications have close to 100 appsettings variables so the quickest way to POC using octopus for our company is to replace the appsettings.json with the appropriate appsettings file per environment. Unless you suggest an alternative?

Hi Jeremiah,

Thank you for your reply, before I answer could I just check - what type of targets are you deploying to (AWS EC2s, Azure VMs, inhouse servers, etc.)? And also, could you tell me your environment names too please?

I ask as, knowing a bit more about your infrastructure will help me point you in the right direction! :grinning:

Regards

Mark

Mark Lamprecht

Solutions Architect | St Ives, Cambridgeshire, UK
OctopusDeployLogo

Hey Mark,

No Problem! I will initially be deploying to Azure VMs and On some of those VMs, I will be deploying to Service Fabric.

Currently, I am looking at the following for environments:

  • development
  • qa1 and qa2 (I was considering leveraging tenants for this)
  • staging
  • production (canary)
  • production main

the two productions share the same appsettings.production.json file, the other 4 environments have their own (e.g. appsettings.staging.json) hope this helps!

Hi Jeremiah,

Thank you for your reply, based on that information you’ve given me plus the fact this is a PoC, we recommend you use a custom deployment script to rename the relevant appsettings.json file for the environment being deployed to.

  1. First ensure your Octopus environment names match your actual infrastructure environment names (development, qa1, qa2, staging, production_canary, production_main)
  2. Then you can reference them in your custom script by using the #{Octopus.Environment.Name} system variable.

For example:

Rename-Item -Path “#{rootPath}\appsettings.#{Octopus.Environment.Name}.json” -Name appsettings.json

Where #{rootPath} is the apps root folder taken from your projects Variables tab, and #{Octopus.Environment.Name} is the environment you are currently deploying to.

You can enable the Custom Deployment Scripts feature by clicking the Configure Features button in your deployment step, then tick Custom Deployment Scripts and add your script to the Pre-deployment script field.

By adding it to the Pre-deployment script you’re ensuring the renaming happens before the app is started i.e. before files are in use once the app starts.

I hope that helps.

Regards

Mark

You are awesome! Ill give this a try, thank you.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.