【问题标题】:Replacing dynamic variables using REGEX content as variable in Azure CD Pipeline Powershell Task使用 REGEX 内容替换动态变量作为 Azure CD 管道 Powershell 任务中的变量
【发布时间】:2020-08-11 16:48:29
【问题描述】:

我正在编写自定义 Powershell Azure CD 管道任务(用于 VM),其中我的 web.config 应替换为管道变量。我在 Azure CD 管道变量中定义了与 WebServiceAuditService 一样的示例配置文件。

<client>
      <endpoint address="__WebService__" binding="basicHttpBinding" bindingConfiguration="ServiceSoap" contract="WebServiceClient.ServiceSoap" name="NLSService" />
      <endpoint address="__AuditService__" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_Audit" contract="AuditApiService.Audit" name="AuditService" />
    </client>

我有 powershell 脚本作为

$zipfileName = "$(System.DefaultWorkingDirectory)\_WebService-CI\drop\Service.zip"
$fileToEdit = "web.config"
$reg = [regex] '__(.*?)__'


[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem");
# Open zip and find the particular file (assumes only one inside the Zip file)
$zip =  [System.IO.Compression.ZipFile]::Open($zipfileName,"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 $reg, $(${1}) -join "`r`n"
    #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()

那么我该如何动态替换“__”中的正则表达式内容并将该内容视为一个变量,该变量应该查看管道变量并在行中替换它:

$text = $text -replace $reg, $(${1}) -join "rn"

【问题讨论】:

标签: azure powershell azure-devops continuous-deployment


【解决方案1】:

如果您不能使用Replace TokensTransform Web.config file,这里有一些东西:

根据web.config文件包含的事实:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For ...
  -->
<configuration>
..
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="checkVatBinding" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="__WebService__" binding="basicHttpBinding" bindingConfiguration="ServiceSoap" contract="WebServiceClient.ServiceSoap" name="NLSService" />
      <endpoint address="__AuditService__" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_Audit" contract="AuditApiService.Audit" name="AuditService" />
    </client>
  </system.serviceModel>
</configuration>

您可以尝试以下方法:

# Get all the file in a single var in spite of an array    
$data = Get-Content "D:\temp\web.config" -raw
$reg = [Regex]::new( '__(.*?)__', [System.Text.RegularExpressions.RegexOptions]::Singleline)
# Here are the vars with the final values
$WebService = "http://Aurillac.fr"
$AuditService = "http://Cantal.fr"
# Here is how to replace
$reg.replace($data, {param ($p);return (Get-Variable -Name $p.groups[1]).Value})

解释:

  • Get-Content 中的-raw 允许获取单个字符串中的所有字符

  • 我使用带有选项 Singleline.NET RegEx 类来允许跨回车换行进行搜索(可能没有必要)

  • 我使用 Regex Replace 方法和 MatchEvaluator Delegate 方法在 PowerShell 中由 Scriptblock 转换,以获取具有 RegEx 捕获的名称的变量。

它给出了:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For ...
  -->
<configuration>
..
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="checkVatBinding" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://Aurillac.fr" binding="basicHttpBinding" bindingConfiguration="ServiceSoap" contract="WebServiceClient.ServiceSoap" name="NLSService" />
      <endpoint address="http://Cantal.fr" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_Audit" contract="AuditApiService.Audit" name="AuditService" />
    </client>
  </system.serviceModel>
</configuration>

【讨论】:

    【解决方案2】:

    是否有任何原因您不能使用市场上的替换令牌任务?这是我在配置文件中替换值的首选方法。

    https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens

    【讨论】:

    • 替换令牌步骤(目标文件是 SetParameters.xml 路径)确实说,它替换了令牌,但是当我使用 WinRM IIS Web 应用程序部署作为虚拟机的下一步部署时,我没有看到他们转变。
    猜你喜欢
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多