Azure devops Azure DevOps发布管道Web.Config编辑

Azure devops Azure DevOps发布管道Web.Config编辑,azure-devops,web-config,azure-pipelines,azure-pipelines-release-pipeline,web.config-transform,Azure Devops,Web Config,Azure Pipelines,Azure Pipelines Release Pipeline,Web.config Transform,我知道,在Azure DevOps中创建发布管道时,可以使用管道中的变量更新应用程序的web.config,这对于所有appSettings值都非常有效 但是,在发布过程中,我想更新web.config的另一部分,特别是sessionStateprovider节点。我已经尝试了一些用于发布管道的插件,如Magic Chunks的Config Transform,但问题是它需要您指定要编辑的配置文件的路径,但当它到达发布管道时,源文件已经在zip存档中。不知何故,appSettings的正常转换能

我知道,在Azure DevOps中创建发布管道时,可以使用管道中的变量更新应用程序的web.config,这对于所有appSettings值都非常有效

但是,在发布过程中,我想更新web.config的另一部分,特别是
sessionState
provider节点。我已经尝试了一些用于发布管道的插件,如Magic Chunks的Config Transform,但问题是它需要您指定要编辑的配置文件的路径,但当它到达发布管道时,源文件已经在zip存档中。不知何故,appSettings的正常转换能够在解压版本下工作,但我无法在文件解压后进行其他转换

我知道您可以在构建管道中进行更改,但我们有理由希望在发布管道中进行更改


有人知道在Azure应用程序服务的发布管道中的appSettings分组之外对web.config进行更改的方法吗?

您可以使用PowerShell在zip文件中进行转换

例如,我在
web.config
中有这个节点:

<configuration>
  <sessionstate 
      mode="__mode__"
      cookieless="false" 
      timeout="20" 
      sqlconnectionstring="data source=127.0.0.1;user id=<user id>;password=<password>"
      server="127.0.0.1" 
      port="42424" 
  />
</configuration>
之前:

之后(在zip文件中,无需解压缩和重新压缩):


我想做一些类似的事情,但发现有一个内置的任务叫做文件转换[由Microsoft提供。如果它是一个简单的替代品,您只需在web.config中定义一个带有键的变量即可。如果您需要更复杂的转换,您也可以指定它。

好主意。我今天将尝试一下,看看是否能使它工作。谢谢。@NickOlsen您检查过吗?您就是那个人。我找不到任何现有的TAk这将替换ZIP文件中XML文件中的任意值。非常感谢!!!嗨,@Nick Olsen你的测试结果是什么?你能和我们分享一下吗:)我还需要替换webconfig文件中的值,还使用了替换令牌任务,但在实际的主web.Config上实现令牌化后,我仍然有点困惑文件也被开发人员使用,并且还添加了令牌,如果我添加test='#{token}#',那么开发人员将无法运行该应用程序,因为使用#{}#更改的值是他们的任何方法,我们可以在不使用“替换令牌”的情况下替换值
# cd to the agent artifcats direcory (where the zip file exist)
cd $env:Agent_ReleaseDirectory
$fileToEdit = "web.config"

[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem");
# Open zip and find the particular file (assumes only one inside the Zip file)
$zipfileName = dir -filter '*.zip'
$zip =  [System.IO.Compression.ZipFile]::Open($zipfileName.FullName,"Update")

$configFile = $zip.Entries.Where({$_.name -like $fileToEdit})

# Read the contents of the file
$desiredFile = [System.IO.StreamReader]($configFile).Open()
$text = $desiredFile.ReadToEnd()
$desiredFile.Close()
$desiredFile.Dispose()
$text = $text -replace  '__mode__',"stateserver"
#update file with new content
$desiredFile = [System.IO.StreamWriter]($configFile).Open()
$desiredFile.BaseStream.SetLength(0)

# Insert the $text to the file and close
$desiredFile.Write($text)
$desiredFile.Flush()
$desiredFile.Close()

# Write the changes and close the zip file
$zip.Dispose()