How can I change the connection strings of my Azure Web Apps during deployment from VSTS build or release?

The Azure Web App Deployment task can actually configure the desired values for your connection strings.  If you were to review an Azure Web App’s Applications settings blade in the portal, you will find a section for App settings and Connection strings.

When you are hosting an ASP.NET application in your Azure Web App these values will be used instead of what is in your web.config.  Any access to your web.config is intercepted by the Azure Web App and if the value is stored in this section that is the value that will be provided to the application.

If you are hosting a non-ASP.NET application each of these values are stored as environment variables so any technology can gain access to the values stored here.

To set the connection string value when using the Azure Web App Deployment task simply use the Additional Arguments field and enter a value like the one below.

-connectionString @{"$(ConnectionStringName)"="Server=tcp:$(ServerName),1433;Database=$(DatabaseName);User ID=$(AdministratorLogin)@$(ServerName);Password=$(AdministratorLoginPassword);Trusted_Connection=False;Encrypt=True;"}

The -connectionString argument is a hashtable so you can set as many connection strings as you need.

Comments (14) -

  • Joe
    Is there an equivalent -appsettings hashtable for App Settings?
    • Currently no.  However, all our task are open source. So you could add it and issue a pull request. Smile
  • Hi Donovan, I got a general question, apologies for posting this here.

    I've a MVC app with all the environments in the "publish", usually we right click, select publish and then select the Profile and wait for build + deploy. We want to move to RM. (inconsistent build quality)

    I can get the "BUILD" to work in VSTS with the switches "/pLaughingeployOnBuild=True /pTongublishProfile=$(BuildProfile)" (setting a BuildProfile variable).

    However the release part I'm not sure what task to apply, I can't apply the MSBuild task as the version is already build, into the version number on the build agent? If I copy the files, the config transforms will not work?

    Am I going about this the right way?

    • What version of RM are you using? The WPF client or VSTS?
      • Hi Donovan,

        I'm using

        - VSTS
        - On site build agent
        - Local server (UNC path + WebDeploy)

        From another site, I was advised to use the Tokenization plugin for VSTS, but that would require a refresh approach to configuration setup.

        Its a shame it can't use the integral method of configuration setting.

        Any ideas?
        • Are you talking about the debug and release web.config transforms? If so those require that you rebuild your code which is a DevOps anti-pattern.  You want to promote the same 0's and 1's not rebuild them.  Tokenization allows only the config file to be changed and the binaries to stay the same.  
          • I thought so, although I thought I should consult the professionals ;)

            Thanks again.
  • This is extremely helpful, thank you. Where can I find the source code so I can add support for 'appsettings'?
  • Hi Donovan,

    Can I provide connection string in Additional Arguments field when working with AzureRM Endpoints?
    I am struggling to get it done.

    What is the best way to manage connection strings with AzureRM? Thanks!

    Kind regards,

    • I am not sure I follow. Are you deploying to App Service or VMs?
  • Unrecognized argument '-connectionString'

    Looks like it's no longer supported?
  • Hi Donovan,

    Is there any other way to achieve same as i am getting "Unrecognized argument '-connectionString' " because it sseems no longer supported

    • There are new versions of the tasks in Azure DevOps that do that. You can also do this with an ARM template.

Add comment