• 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:

  • 相关阅读:
    Mybatis
    spring2
    servlet
    MyBatis1
    Docker容器保存为镜像文件并发布到DockerHub上
    【JS】提取字符串中的分数,汇总后算出平均分,并与每个分数比较,输出
    CentOS下创建管理员权限用户
    Centos安装文件传输软件rz sz
    2022年的零日漏洞影响了哪些平台?
    关于渗透测试的优缺点,你知道吗?
  • 原文地址:https://www.cnblogs.com/King-JJ/p/14271258.html
Copyright © 2020-2023  润新知