Azure Devops 实现自动替换字符串(二)

之前写了一个替换配置是用powershell去替换,powershell命令替换可以避免cmd替换时的某些特殊字符出问题的大坑。

第二种替换方式是直接使用deploy task自带的功能。

比如使用Deploy Azure App Service 任务,它提供了一个叫File Transforms & Variable Substitution Options 的配置,

其功能为对指定的文件(xml或json)进行字符串替换。

如下,我配置了对网站下的appsettings.json文件进行参数替换操作。

然后,在variables中,设置需要替换的键值,我这边是json格式的,设置键时应该从根节点到目标节点,以“.”点去分割。

 这是我json文件中实际内容:

{
  "ConnectionStrings": {
  },
  "App": {
    "AppKeyVault": "AppCorsConfig"
  },
  "Authentication": {
    "KeyVaultApp": {
      "TenantId": "{TenantId}",
      "ClientId": "{ClientId}",
      "ClientSecret": "{ClientSecret}",
      "KeyVaultEndPoint": "{KeyVaultEndPoint}",
      "DBConnectionString": "DBConnectionString"
    },
    "Storage": {
      "StorageKeyVault": "StorageConfig"
    },
    "WeChatAPI": {
      "WeChatAPIKeyVault": "WeChatAPIConfig"

    }


  }
}

  

最后执行效果,就是json文件中的 {TenantId},{ClientId},{ClientSecret},{KeyVaultEndPoint} 被替换为了variables中设置的值

另一种替换方法:

选择DeployAzureAppService中的【PostDeploymentAction】,选择InlineScript,执行脚本:powershell -Command "(gc  ApplicationInsightsDeploy.config) -replace '{ApplicationInsightsKey}', '$(ApplicationInsights.InstrumentationKey)' | Out-File ApplicationInsights.config"

其中:$(ApplicationInsights.InstrumentationKey)是定义在Variable中的变量

PS:一个额外小命令记录一下

查询所有环境变量:

Get-ChildItem Env:

原文地址:https://www.cnblogs.com/King-JJ/p/14271258.html