Azure DevOps发布管道Web.Config编辑

5
我知道在 Azure DevOps 中创建发布流水线时,你可以使用流水线中的变量更新应用程序的 web.config 文件,并且这对所有 appSettings 值都很有效。但是,在发布流水线期间,我想更新 web.config 的不同部分,具体来说是 sessionState 提供程序节点。我尝试了一些发布流水线插件,比如 Magic Chunks 的 Config Transform,但问题在于它需要您指定要编辑的配置文件的路径,但到达发布流水线时,源文件已经压缩为 zip 存档文件。不知何故,appSettings 的常规转换能够在解压缩版本上运行,但我无法在文件解压缩后进行其他转换。我知道你可以在构建流水线中进行更改,但我们有理由想要在发布流水线中执行它。是否有人知道一种在 Azure 应用服务的发布流水线中,在 appSettings 分组之外更改 web.config 的方法?

嗨,@Nick Olsen,你的测试结果是什么?你能和我们分享一下吗 :) - Hugh Lin
3个回答

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>

我使用这个脚本:
# 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()

之前:

输入图像描述

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

输入图像描述


好主意。我今天会尝试一下,看看能否让它正常工作。谢谢。 - Nick Olsen
@NickOlsen 你检查过了吗? - Shayki Abramczyk
2
您真是太好了。我找不到任何现有的任务可以替换ZIP文件中XML文件内的任意值。非常感谢!!! - Mike Brunner
在我的发布流水线中,我有“ IIS Web App Manage”和“ IIS Web App Deploy”。 我已经将其放置在“ IIS Web App Deploy”下面,即使它执行了,也没有更新文件。尝试将其放置在上面,但失败了。你能告诉我这在IIS预设上不起作用,还是我应该使用YAML配置? - Kadaj

3

2
我还需要替换webconfig文件中的值,为此我也使用了“Replace token”任务,但仍然有些困惑。在对实际的主web.Config文件进行标记后,该文件也被开发人员使用,并添加了令牌。如果我添加test = '#{token}#',那么开发人员将无法运行应用程序,因为值会改变为#{}#。是否有任何方法可以在不使用“Replace token”的情况下替换值? - Saad Awan

1

由于我遇到了边缘情况,在这种情况下,我在PUTDELETE上收到了405状态,如下所示: WebAPI Delete not working - 405 Method Not Allowed

这要求我更改web.config文件,该文件仅在项目发布时创建。因此,我需要在web.config中插入几行代码,例如:

    <modules>
        <remove name="WebDAVModule" />
    </modules>

还有一些。

我的答案基于@Shayki Abramczyk的答案,我认为它提供了另一种更新的解决此问题的方法。由于他的答案对我来说并没有完全起作用,而且对于那些不是DevOps领域专业人员,而是想要自动化CI-CD工作流程的程序员来说,这个答案更加适用。

我认为目前存在的问题是这行代码:

cd $env:Agent_ReleaseDirectory

没有导航到正确的文件夹。您仍需要导航到文件夹并拖放您的 zip 文件,就像这样:cd _Your.Project-CI\drop

因此,请通过以下方式在发布流水线中添加另一个 PowerShell 组件:

enter image description here

并将以下代码添加到其中:

# cd to the agent artifacts directory (where the zip file exist)
cd $env:Agent_ReleaseDirectory
cd _Your.Project-CI\drop
$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()

$contentToAdd1 = @'
    <system.webServer>
    <modules>
    <remove name="WebDAVModule" />
    </modules>
'@
#$text[3] = $text[3] -replace '<system.webServer>',$contentToAdd1
$text = $text -replace '<system.webServer>',$contentToAdd1
 
$contentToAdd2 = @'
    <handlers>
    <remove name="WebDAV" />
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,PUT,DELETE,DEBUG" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0" />
'@
# $text[4] = $text[4] -replace '<handlers>',$contentToAdd2
$text = $text -replace '<handlers>',$contentToAdd2


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

唯一剩下要做的事情就是用你的项目名称替换:cd _Your.Project-CI\drop,例如 cd _Weather.Front-CI\drop

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