我们可以使用 WinRM 更新 web.config 文件吗 - IIS Web 应用程序部署任务

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

在使用 WinRM - IIS Web App Deployment 任务从 Azure DevOps 发布管道在本地 IIS 服务器上部署应用程序时,我们有什么方法可以更新 web.config 文件.

下面是问题陈述。

我知道理想情况下,这可以使用其他任务来完成,例如 AzureDevops Marketplace 中存在的 Update Config 任务。

首先我想检查这是否可行

我们能否使用 WinRM - IIS Web App Deployment 任务中可用的 Web Deploy Parameter FileOverride Parameters 选项来实现它。

任务日志如下:

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.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.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

Web 部署参数文件覆盖参数 它们用于 .xml 参数文件,可用于IIS 用于配置设置。所以你需要用你的参数创建一个 .xml 文件,然后你可以在任务中覆盖它们。

如果你想使用 web.config 也可以使用 .xml 文件来完成,但是有点复杂,请参阅 here 如何操作。之后就可以在任务中使用Override了。

我建议使用 web.config 转换任务,而不是在 .xml 选项中(这是旧方法...)。

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

我觉得可以。正如 Sayki 所说,它将用于 .xml 文件。你可以先生成一个SetParameters.xml,然后把这个SetParameters.xml加到Web Deploy Parameter File中去执行你想要的。

例如,假设您的 web.config 文件格式如下:

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

而在SetParameters.xml中,您可以参考以下格式:

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

关于任务的配置:

此时DefaultConnection<string2>

您可以通过在覆盖参数中指定来覆盖参数值。例如:

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="default@example.com"/>
    <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>

在构建期间创建 MSDeploy 程序包(Visual Studio Build/MSBuild 任务):

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

然后,它会生成这些文件:

xx.Set的内容Parameters.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>

覆盖 WinRM-IIS Web 应用程序部署任务的参数:

name="DefaultConnection-Web.config Connection String",value="new connectionstring"
name="email",value="newmail@xx.com"
name="url",value="newurl"