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

3

有没有办法在使用Azure DevOps发布流程中的WinRM - IIS Web App Deployment任务部署应用程序到本地IIS服务器时更新web.config文件。

以下是问题陈述:

  • 我有一个Asp.net应用程序存在源代码管理(AzureDevops Repositories)中
  • 我想更新Web.config文件以进行数据库连接、外部第三方URL等操作

  • 我想使用WinRM - IIS Web App Deployment任务执行相同的操作

我知道理论上可以使用AzureDevops Marketplace中的其他任务,如Update Config任务来完成此操作。

首先,我想检查这是否可行

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

ReleasePipeline

任务日志如下:

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
3个回答

3
您可以通过在“覆盖参数”中指定参数值来覆盖它们。例如:

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任务):

enter image description here

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

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

enter image description here

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>

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

enter image description here

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

@E_FreeLancer,你在覆盖参数中指定 name="TargetValueChange",value="xxx" 吗?另外,你可以指定默认值 <parameter name="email" description="description" defaultValue="default value"> - starian chen-MSFT
我认为我已经找到了区别。我能够更改SampleWebAppDb的值,但不能更改TargetValueChange的值。在Web.config文件中,它使用<appSettings>     <add key="TargetValueChange" value="NotChanged" />   </appSettings>进行配置,而SampleWebAppDb是使用<connectionStrings>     <add name ="SampleWebAppDb" connectionString ="SomeConection"/>   </connectionStrings> 进行配置的。一个按名称配置,另一个按键配置,这会导致问题吗? - E_FreeLancer
@E_FreeLancer 不,对于我的示例,电子邮件和网址在 appSettings 中。 - starian chen-MSFT
@E_FreeLancer,parameters.xml文件的内容是什么? - starian chen-MSFT
@E_FreeLancer 把代码修改成这样:<?xml version="1.0" encoding="utf-8" ?> <parameters> <parameter name="TargetValueChange" description="description" value="default-value" defaultValue="default-value"> <parameterEntry kind="XmlFile" scope="\\web.config$" match="/configuration/appSettings/add[@key='TargetValueChange']/@value" /> </parameter> </parameters> - starian chen-MSFT
显示剩余6条评论

2
我们可以使用WinRM - IIS Web应用程序部署任务中提供的Web Deploy参数文件和覆盖参数选项来实现它。我认为这是可能的,就像Sayki所说的那样,它适用于.xml文件。您可以首先生成一个SetParameters.xml文件,然后将此SetParameters.xml文件添加到Web Deploy参数文件中以执行您想要的操作。
例如,假设您的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>

关于任务的配置:

enter image description here

此时,DefaultConnection 将是 <string2>

1
Web Deploy参数文件和覆盖参数是用于在IIS中使用的配置设置的.xml参数文件。因此,您需要创建一个带有参数的.xml文件,然后可以在任务中覆盖它们。
如果您想要使用web.config,也可以使用.xml文件完成,但这有点复杂,请参见这里如何操作。之后,您可以在任务中使用覆盖。
我建议使用web.config转换任务而不是.xml选项(这是旧方法...)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接