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).database.windows.net,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

    1/10/2016 1:28:30 AM | Reply

    Is there an equivalent -appsettings hashtable for App Settings?

    • Donovan

      1/10/2016 10:51:29 PM | Reply

      Currently no.  However, all our task are open source. So you could add it and issue a pull request. Smile

  • Milo

    1/25/2016 4:45:29 PM | Reply

    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?

    Thanks.

    • Donovan

      2/28/2016 1:25:16 AM | Reply

      What version of RM are you using? The WPF client or VSTS?

      • Milo

        2/29/2016 4:39:56 PM | Reply

        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?

        • Donovan

          3/3/2016 5:01:56 AM | Reply

          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.  

          • Milo

            3/3/2016 12:25:42 PM | Reply

            I thought so, although I thought I should consult the professionals ;)

            Thanks again.

  • Omar Stewey

    3/29/2016 3:00:08 PM | Reply

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

  • biil jons

    10/21/2016 3:01:00 PM | Reply

    i like this blog

  • Adil G

    10/23/2017 10:19:34 AM | Reply

    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,

    Adil

    • Donovan

      12/3/2017 2:14:12 PM | Reply

      I am not sure I follow. Are you deploying to App Service or VMs?

  • Ray H

    6/5/2018 4:03:14 PM | Reply

    Unrecognized argument '-connectionString'

    Looks like it's no longer supported?

  • Arun

    11/29/2018 11:09:40 AM | Reply

    Hi Donovan,

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

    Regards
    Arun

    • Donovan

      12/13/2018 3:21:32 PM | Reply

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

Add comment

Loading