Azure DevOps 发布管道 Web.Config 编辑

Nic*_*sen 5 web-config web.config-transform azure-devops azure-pipelines azure-pipelines-release-pipeline

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

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

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

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

Sha*_*zyk 5

您可以使用 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>
Run Code Online (Sandbox Code Playgroud)

我使用这个脚本:

# 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()
Run Code Online (Sandbox Code Playgroud)

前:

在此输入图像描述

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

在此输入图像描述

  • 你就是那个男人。我找不到任何现有任务可以替换 ZIP 文件中 XML 文件中的任意值。非常感谢!!! (2认同)