nuget 中 .config 文件的令牌替换(powershell 或 AzureDevOps 任务)

时间:2021-04-22 13:15:54

标签: powershell azure-devops

我在 AzureDevOps 中有一个具有以下阶段的管道:

  • 构建:构建并生产一些 nuget(使用蛋糕构建)。 nuget 包含用于某些设置的标记化 web.config,例如 "value=#{tokenName}"
  • 部署到环境 X:只需要替换 nuget 中 web.config 中的令牌并将其推送到 nuget 存储库。 (这适用于不同的环境)。

我只是想知道一种简单的方法来完成这项工作。

我知道有 AzureDevOps 任务“替换令牌”可以实现此替换,但这将需要拆包所有 nuget,替换并再次打包。对于大型 nuget,这是一项耗时的任务。

我想如果我只是从 nuget 中解压匹配的 .config 文件,转换它们,然后再次打包这些文件,那么速度会更快。这将非常快。 问题是,在这种情况下,工作需要在 powershell 中完成,令牌被 azuredevops 变量组中的值替换。

如果我有一个名为“Dev”的变量组和一个名为“IdentityService.BaseUrl”的变量

我在 powershell 中试过这样的:

    $content = "Example file string with token #{IdentityService.BaseUrl} end"
    $variableName = [Regex]::Matches($content, '(?<=#{)(.*?)(?=})') | Select -ExpandProperty Value
    
    # variableName is now "IdentityService.BaseUrl"
    
    Write-Host "Value of $variableName is $($variableName)"
    # This will print: Value of IdentityService.BaseUrl is IdentityService.BaseUrl
    # I need to print the name of the variable and the variable value from the azureDevOpsGroup

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

根据您目前的情况,我们不能使用双变量。在azure devops管道中如果我们可以确定第一个变量(比如在变量中设置值),那么我们可以使用脚本$(${{variables.variableName}})来帮助我们找到真正的值。

但是在您的脚本中,第一个变量是无法确定的,所以我们建议如果可能,您可以使用 if()eles() 来帮助您设置第一个变量。

相关问题