在 appSettings 之外更新 web.config 中的变量

SK-*_*USA 5 azure-devops

我需要更新 Azure TFS 中 web.config 中的值。我能够将 connectionString 的值替换为 appSettings 的一部分(为此,我需要启用名为 XML 变量替换的设置(在 IIS Web 部署下)。

但是,web.config 中还有其他区域不会被替换。

我尝试了几种不同的方法,使用不同的任务进行令牌替换,使用变量的“发布”或“环境”设置,使用变量组。然而,这些都没有奏效。

目前我正在使用替换令牌任务(可在https://github.com/qetza/vsts-replacetokens-task#readme 获得

我已将令牌前缀和后缀设置为 __(以匹配 web.config)

这是 web.config 文件的摘录

<?xml version="1.0"?>
<configuration>
    <appSettings>
        <add key="ConnectionString" value="__ConnectionString__"/>
    </appSettings>
    <system.web>
        <pages theme="__Theme__" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
            <controls>
                <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            </controls>
        </pages>
    </system.web>
    <system.serviceModel>       
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
        <bindings>
            <customBinding>
                <binding name="TestBinding1">
                    <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16" messageVersion="Soap12" writeEncoding="utf-8">
                        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    </textMessageEncoding>
                    <httpsTransport manualAddressing="false" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous" bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard" keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous" realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false" useDefaultWebProxy="true" requireClientCertificate="false" />
                </binding>
            </customBinding>
            __basicHttpBindingOptionalBinding__
        </bindings>
        <client>
            <endpoint address="__TestEndPoint__" binding="customBinding" bindingConfiguration="TestBinding1" contract="BSEInspectionsWebServiceForFDA.StateDataTransfer" name="StateDataTransferPort" />
            __endpointOptionalEndpoint__
        </client>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我希望 basicHttpBindingOptionalBinding 和 endpointOptionalEndpoint 只有空行。的主题需要与TestTheme被替换和TestEndPoint需要与在变量定义的值来设置。

Lev*_*SFT 0

对于替换__Theme__, __TestEndPoint__,我推荐扩展任务Magic Chunks

您可以将 Magic Chunks 任务安装到您的组织并将其添加到您的管道中。它使用以下魔术块任务的示例设置测试了您的 web.config:在此输入图像描述

为了替换basicHttpBindingOptionalBinding,我推荐另一个扩展任务RegEx Find & Replace

在此输入图像描述

您可以在这两个任务中引用管道变量。我将以上两个任务添加到我的测试管道中。web.config 中的值已成功替换。

更新:要使用 powershell 任务从构建工件中提取 web.config 来运行以下脚本:

你需要自己$sourceFile配置$destFile$sourceFile应该是 zip 文件的绝对路径。

$sourceFile="$(Build.ArtifactStagingDirectory)\AboutSite.zip"
$destFile="$(Build.BinariesDirectory)\web.config"

Add-Type -Assembly System.IO.Compression.FileSystem
$zip = [IO.Compression.ZipFile]::OpenRead($sourceFile)
$zip.Entries | where {$_.Name -like 'web.config'} | foreach {[System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, $destFile, $true)}
$zip.Dispose()
Run Code Online (Sandbox Code Playgroud)