Azure DevOps发布管道Web.Config编辑

问题描述 投票:0回答:1

[我知道,在Azure DevOps中创建发布管道时,可以使用管道中的变量来更新应用程序的web.config,并且适用于所有appSettings值。

但是,在发布管道中,我想更新web.config的其他部分,特别是sessionState提供者节点。我已经尝试了一些发布管道的插件,例如Magic Chunks的Config Transform,但问题是它需要您指定要编辑的配置文件的路径,但是到发布管道时,源文件才位于zip存档。通过某种方式,appSettings的常规转换可以在未压缩的版本上工作,但是在文件解压缩后我无法进行其他转换。

我知道您可以在构建管道中进行更改,但是出于某些原因,我们希望在发布管道中进行更改。

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

azure-devops web-config azure-pipelines azure-pipelines-release-pipeline web.config-transform
1个回答
0
投票

您可以使用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>

我使用此脚本:

# 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()

之前:

enter image description here

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.