【问题标题】:Token replacement of .config files inside a nuget (powershell or AzureDevOps task)nuget 中 .config 文件的令牌替换(powershell 或 AzureDevOps 任务)
【发布时间】:2021-07-16 17:20:37
【问题描述】:

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

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

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

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

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

如果我有一个名为“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

有什么想法吗?

【问题讨论】:

    标签: powershell azure-devops


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-13
      • 2019-12-15
      • 1970-01-01
      • 2021-05-10
      • 2018-10-27
      • 1970-01-01
      • 2011-04-10
      • 2023-04-11
      相关资源
      最近更新 更多