在 DevOps 管道中使用带有 ' 和 " 的 Build.SourceVersionMessage

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

在天蓝色的 DevOps 管道中,我想使用 Build.SourceVersionMessage 变量作为我用来发布工件的 NuGet.nuspec 文件中的发布注释。

在 NuGet.nuspec 中我有这一行 $release_notes$

我发现我需要使用像这样的powershell脚本来转义xml字符。

- task: PowerShell@2
   displayName: 'Modify NuGet.nuspec with release notes'
   inputs:
     targetType: 'inline'
     script: |
       # I can not find a way to pass a value with semicolons via the -Properties to nuget pack. Therefor we modify the NuGet.nuspec file directly.
       $escapedMsg = [System.Security.SecurityElement]::Escape("$(Build.SourceVersionMessage)")
       Write-Host "Escaped release notes: $escapedMsg"
       (Get-Content "NuGet.nuspec") -replace '\$release_notes\$', $escapedMsg | Set-Content "NuGet.nuspec"

但是,如果消息包含双引号,此脚本仍然存在问题。 powershell 脚本失败,因为 Escape(...) 内的字符串格式错误。

然后我可以在脚本中使用单引号,但如果消息有单引号,它将不起作用。

有什么方法可以将包含任何字符的 SourceVersionMessage 获取到 NuGet.nuspec 文件中吗?

谢谢

azure-devops nuget devops pipeline
1个回答
0
投票

有什么方法可以将包含任何字符的 SourceVersionMessage 获取到 NuGet.nuspec 文件中吗?

我建议我们可以使用替换令牌扩展中的替换令牌任务。

步骤如下:

Step1:在 NuGet.nuspec 文件中设置 #{varname}# (例如

#{Build.SourceVersionMessage}#
)。

例如:

<?xml version="1.0"?>
<package>
  <metadata minClientVersion="3.3.0">
    <id>ContentFilesExample</id>
    <version>1.0.0</version>
    <authors>nuget</authors>
    <owners>nuget</owners>
    <releaseNotes>#{Build.SourceVersionMessage}#</releaseNotes>
    ....
  </metadata>
</package>

Step2:使用替换标记任务时,变量

$(Build.SourceVersionMessage)
将自动替换NuGet.nuspec中的占位符。

- task: replacetokens@6
  displayName: 'Replace tokens'
  inputs:
    root: filepath
    sources: NuGet.nuspec

在这种情况下,所有特殊字符都将成为变量值(字符串类型)的一部分。使用此方法时我们不需要翻译特殊字符。

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