E_FreeLancer
E_FreeLancer

Reputation: 91

can we update the web.config file using WinRM - IIS Web App Deployment Task

Is there any way we can update the web.config file while deploying the application on local IIS Server using WinRM - IIS Web App Deployment task from Azure DevOps release Pipeline.

Below is the problem statement.

I know ideally this can be done using other tasks like Update Config task present in AzureDevops Marketplace.

First up all I want to check if this is feasible

Can we achieve it using Web Deploy Parameter File & Override Parameters Options which are available in the WinRM - IIS Web App Deployment Task.

ReleasePipeline

Logs from the Task is available below:

2019-07-24T14:08:09.5094037Z ##[debug]PowerShellHandler.Execute - AddParameter(WebDeployParamFile=E:\NewAgent\_work\r2\a/POC-ASP.NET-CI Build/drop/SampleWebApp.SetParameters.xml)
2019-07-24T14:08:09.5098039Z ##[debug]PowerShellHandler.Execute - AddParameter(OverRideParams=name="SampleWebAppDb-Web.config Connection String",value="new connectionstring"
2019-07-24T14:08:09.5100161Z name="TargetValueChange",value="Value is Changed")
2019-07-24T14:08:09.7289513Z ##[debug]Loading module from path 'E:\NewAgent\_work\_tasks\IISWebAppDeploy_50acc50f-7d15-470b-83c1-578b3f3eeba2\1.4.6\DeploymentSDK\InvokeRemoteDeployment.ps1'.
2019-07-24T14:08:09.7352020Z ##[debug]Dot-sourcing the script file 'E:\NewAgent\_work\_tasks\IISWebAppDeploy_50acc50f-7d15-470b-83c1-578b3f3eeba2\1.4.6\DeploymentSDK\InvokeRemoteDeployment.ps1'.
2019-07-24T14:08:09.7560871Z ##[debug]Entering script DeployIISWebApp.ps1
2019-07-24T14:08:09.7695237Z ##[debug]machinesList = localhost
2019-07-24T14:08:09.7696254Z ##[debug]adminUserName = desktop-0dnhide\hp
2019-07-24T14:08:09.7698716Z ##[debug]winrmProtocol  = Http
2019-07-24T14:08:09.7699874Z ##[debug]testCertificate = true
2019-07-24T14:08:09.7702175Z ##[debug]webDeployPackage = E:\NewAgent\_work\r2\a/POC-ASP.NET-CI Build/drop/SampleWebApp.zip
2019-07-24T14:08:09.7702410Z ##[debug]webDeployParamFile = E:\NewAgent\_work\r2\a/POC-ASP.NET-CI Build/drop/SampleWebApp.SetParameters.xml
2019-07-24T14:08:09.7703755Z ##[debug]overRideParams = name="SampleWebAppDb-Web.config Connection String",value="new connectionstring"
2019-07-24T14:08:09.7703971Z name="TargetValueChange",value="Value is Changed"
2019-07-24T14:08:09.7705526Z ##[debug]websiteName = MainApp/APP1
2019-07-24T14:08:09.7706521Z ##[debug]removeAdditionalFiles = false
2019-07-24T14:08:09.7708165Z ##[debug]excludeFilesFromAppData = false
2019-07-24T14:08:09.7708778Z ##[debug]takeAppOffline = false
2019-07-24T14:08:09.7710018Z ##[debug]additionalArguments = 
2019-07-24T14:08:09.7710541Z ##[debug]deployInParallel = true
2019-07-24T14:08:09.7761001Z ##[debug]Triming inputs for excess spaces, double quotes
2019-07-24T14:08:09.8269846Z ##[debug]Executing main function in MsDeployOnTargetMachines : Execute-Main -WebDeployPackage "E:\NewAgent\_work\r2\a/POC-ASP.NET-CI Build/drop/SampleWebApp.zip" -WebDeployParamFile "E:\NewAgent\_work\r2\a/POC-ASP.NET-CI Build/drop/SampleWebApp.SetParameters.xml" -OverRideParams "name=`"SampleWebAppDb-Web.config Connection String`",value=`"new connectionstring`"
2019-07-24T14:08:09.8270269Z name=`"TargetValueChange`",value=`"Value is Changed`"" -WebsiteName "MainApp/APP1" -RemoveAdditionalFiles false -ExcludeFilesFromAppData false -TakeAppOffline false -AdditionalArguments ""
2019-07-24T14:08:09.8566125Z Starting deployment of IIS Web Deploy Package : E:\NewAgent\_work\r2\a/POC-ASP.NET-CI Build/drop/SampleWebApp.zip
2019-07-24T14:08:09.8566522Z 
2019-07-24T14:08:09.8566618Z 
2019-07-24T14:08:09.8721777Z ##[debug]Entered Invoke-RemoteDeployment function
2019-07-24T14:08:09.8722346Z ##[debug]machinesList = localhost
2019-07-24T14:08:09.8723967Z ##[debug]adminUserName = desktop-0dnhide\hp
2019-07-24T14:08:09.8725568Z ##[debug]protocol = Http
2019-07-24T14:08:09.8769871Z ##[debug]Creating credentials object for connecting to remote host
2019-07-24T14:08:09.8918408Z ##[debug]Tokenizing machine name and port, to create dictonary
2019-07-24T14:08:09.9235805Z ##[debug]Splitting machine name and port into tokens
2019-07-24T14:08:10.0182997Z ##[debug]Using skipCA = True
2019-07-24T14:08:10.0194123Z ##[debug]Using useHttp = True
2019-07-24T14:08:10.0216648Z Performing deployment in parallel on all the machines.
2019-07-24T14:08:10.0217058Z 
2019-07-24T14:08:10.0217233Z 
2019-07-24T14:08:10.0309977Z Deployment started for machine: localhost with port 5985.
2019-07-24T14:08:10.0316467Z 
2019-07-24T14:08:10.0317179Z 
2019-07-24T14:08:10.2247380Z ##[debug]Exporting function 'New-PSWorkflowSession'.
2019-07-24T14:08:10.2250179Z ##[debug]Exporting alias 'nwsn'.
2019-07-24T14:08:30.8101839Z ##[debug]Processed: ##vso[task.setprogress value=-1;] 
2019-07-24T14:08:30.8185994Z Deployment status for machine localhost : Passed
2019-07-24T14:08:30.8186905Z 
2019-07-24T14:08:30.8187252Z 
2019-07-24T14:08:30.8221717Z 
2019-07-24T14:08:30.8223208Z     Deployment started on target machine...
2019-07-24T14:08:30.8223964Z 
2019-07-24T14:08:30.8225201Z Entering script MsDeployOnTargetMachines.ps1
2019-07-24T14:08:30.8226227Z 
2019-07-24T14:08:30.8226516Z Entering Execute-Main function
2019-07-24T14:08:30.8226760Z 
2019-07-24T14:08:30.8226994Z WebDeployPackage = E:\NewAgent\_work\r2\a/POC-ASP.NET-CI Build/drop/SampleWebApp.zip
2019-07-24T14:08:30.8227565Z 
2019-07-24T14:08:30.8228092Z WebDeployParamFile = E:\NewAgent\_work\r2\a/POC-ASP.NET-CI Build/drop/SampleWebApp.SetParameters.xml
2019-07-24T14:08:30.8228215Z 
2019-07-24T14:08:30.8228421Z OverRideParams = name="SampleWebAppDb-Web.config Connection String",value="new connectionstring"
2019-07-24T14:08:30.8228607Z name="TargetValueChange",value="Value is Changed"
2019-07-24T14:08:30.8232239Z 
2019-07-24T14:08:30.8232354Z ##[command]"C:\Program Files\IIS\Microsoft Web Deploy V3\msDeploy.exe"  -verb:getParameters -source:package='E:\NewAgent\_work\r2\a/POC-ASP.NET-CI Build/drop/SampleWebApp.zip'
2019-07-24T14:08:30.8233350Z 
2019-07-24T14:08:30.8233526Z MsDeploy command line arguments:  -verb:sync -source:package="E:\NewAgent\_work\r2\a/POC-ASP.NET-CI Build/drop/SampleWebApp.zip" -dest:auto -setParamFile="E:\NewAgent\_work\r2\a/POC-ASP.NET-CI Build/drop/SampleWebApp.SetParameters.xml" -setParam:name="SampleWebAppDb-Web.config Connection String",value="new connectionstring" -setParam:name="TargetValueChange",value="Value is Changed" -setParam:name="IIS Web Application Name",value="MainApp/APP1" -enableRule:DoNotDeleteRule -retryAttempts:3 -retryInterval:3000
2019-07-24T14:08:30.8233600Z 
2019-07-24T14:08:30.8233781Z Deploying website. Running command: "C:\Program Files\IIS\Microsoft Web Deploy V3\msDeploy.exe"  -verb:sync -source:package="E:\NewAgent\_work\r2\a/POC-ASP.NET-CI Build/drop/SampleWebApp.zip" -dest:auto -setParamFile="E:\NewAgent\_work\r2\a/POC-ASP.NET-CI Build/drop/SampleWebApp.SetParameters.xml" -setParam:name="SampleWebAppDb-Web.config Connection String",value="new connectionstring" -setParam:name="TargetValueChange",value="Value is Changed" -setParam:name="IIS Web Application Name",value="MainApp/APP1" -enableRule:DoNotDeleteRule -retryAttempts:3 -retryInterval:3000

Upvotes: 3

Views: 1599

Answers (3)

starian chen-MSFT
starian chen-MSFT

Reputation: 33708

You can override parameters value by specifying them in Override Parameters. For example:

Web.config:

<connectionStrings>
    <add name="DefaultConnection" connectionString="old string" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="email" value="[email protected]"/>
    <add key="url" value="example.com"/>
  </appSettings>

Parameters.xml:

<?xml version="1.0" encoding="utf-8" ?>
<parameters>
  <parameter name="email" description="description"> 
    <parameterEntry kind="XmlFile"
                    scope="\\web.config$"
                    match="/configuration/appSettings/add[@key='email']/@value" />
  </parameter>
  <parameter name="url" description="description">
    <parameterEntry kind="XmlFile"
                    scope="\\web.config$"
                    match="/configuration/appSettings/add[@key='url']/@value" />
  </parameter>
</parameters>

Create MSDeploy package during build (Visual Studio Build/MSBuild task):

enter image description here

/p:SkipInvalidConfigurations=true /p:DeployOnBuild=true  /p:WebPublishMethod=Package /p:PackageLocation="$(build.artifactstagingdirectory)" /P:PackageTempRootDir=""

Then, it will generates these files:

enter image description here

Content of xx.SetParameters.xml:

<?xml version="1.0" encoding="UTF-8"?>

<parameters>

<setParameter value="" name="email"/>

<setParameter value="" name="url"/>

<setParameter value="Data Source=old string" name="DefaultConnection-Web.config Connection String"/>

</parameters>

Override Parameters of WinRM-IIS Web App Deployment task:

enter image description here

name="DefaultConnection-Web.config Connection String",value="new connectionstring"
name="email",value="[email protected]"
name="url",value="newurl"

Upvotes: 3

Mengdi Liang
Mengdi Liang

Reputation: 18978

Can we achieve it using Web Deploy Parameter File & Override Parameters Options which are available in the WinRM - IIS Web App Deployment Task.

I think it can. As Sayki said, it will for an .xml file. You can generate a SetParameters.xml first, and then add this SetParameters.xml into Web Deploy Parameter File to execute what you want.

For example, suppose your web.config file is the following format:

<connectionStrings>
    <add name="DefaultConnection" connectionString="<string1>"
      providerName="<SqlClient Name>" />
 </connectionStrings>

And in SetParameters.xml, you can refer the follow format:

<?xml version="1.0" encoding="utf-8"?>
    <parameters>
      <setParameter name="DefaultConnection" value="<string2>" />
</parameters>

For configuration about the task:

enter image description here

At this time, the DefaultConnection will be <string2>

Upvotes: 2

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41575

The Web Deploy Parameter File and the Override Parameters they are for a .xml parameters file that can be used in the IIS for configruation settings. so you need to create a .xml file with your parameters and then you can override them in the task.

If you want to use web.config it also can be done with the .xml file, but it's little complicated, see here how to do it. after that you can use the Override in the task.

I recommend to use web.config transformation task and not in the .xml option (it's old way...).

Upvotes: 1

Related Questions