How to use environment variables set in Azure Pipeline in the web.config file of ASP.NET server?

给你一囗甜甜゛ 提交于 2021-01-27 12:34:38

问题


I intend to use the following env variables from Azure Pipeline in ASP.NET's web.config file:

<connectionStrings>
    <add name="ApplicationDbContext" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=tcp:app-$(DeploymentEnvironment).database.windows.net,1433;Initial Catalog=app-db;Persist Security Info=False;User ID=$(DeploymentDBUserName);Password=$(DeploymentDBPassword);Encrypt=True;Application Name=EntityFramework&quot;" providerName="System.Data.EntityClient" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
    <add name="ServiceBus" connectionString="Endpoint=sb://app-$(DeploymentEnvironment)-bus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=$(ServiceBusAccessKey)" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
    <add name="Cache" connectionString="app-$(DeploymentEnvironment)-cache.redis.cache.windows.net:6380,password=$(CachePassword),ssl=True,abortConnect=False,allowAdmin=True" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
<connectionStrings />

Could someone help?


回答1:


You can use extension task RegEx Find & Replace or RegEx Match & Replace Task to replace REGULAR EXPRESSIONS in the config files with pipeline variables.

First search and install RegEx Find & Replace extension (or RegEx Match & Replace Task) in your organization. Then add the task to replace the properties with the variables you defined in the pipeline.

You can also use Magic Chunk task to replace the properties in config files with azure pipeline environment variables.

Hope above helps!




回答2:


I have an example of how to replace the values in the YAML file, but the idea with XML would be the same. Essentially on my master branch(which triggers the build pipeline), I pushed instead of values some strings that I will replace in the bash script with variables. An example is below:

- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |

      sed -i "s%AzureWebJobsStorage_Value%$AZUREWEBJOBSSTORAGE_VALUE%g" "deploy.yml"

      sed -i "s%SPEECH_SERVICE_KEY_Value%$SPEECH_SERVICE_KEY_VALUE%g" "deploy.yml"

      sed -i "s%SPEECH_REGION_Value%$SPEECH_REGION_VALUE%g" "deploy.yml"

      sed -i "s%GPT2SERVICE_ENDPOINT_Value%$GPT2SERVICE_ENDPOINT_VALUE%g" "deploy.yml"

      sed -i "s%TRANSLATOR_KEY_Value%$TRANSLATOR_KEY_VALUE%g" "deploy.yml"

      sed -i "s%TranslatorEndpoint_Value%$TRANSLATORENDPOINT_VALUE%g" "deploy.yml"

As you can, for example, 'TranslatorEndpoint_Value' is the string I place I yaml, and replace it with variable $TRANSLATORENDPOINT_VALUE, just before I do the next step in which I build the solution.

I am sure there are maybe other solutions, but this is what I do in these cases. I hope this helps.




回答3:


You can use tool called "Replace Tokens" in Azure Pipelines

For example you have environment variable: var_1 = some_value

Then your tokenized config file will be something like that:

<connectionStrings>
    <add name="ApplicationDbContext" connectionString="con-#{var_1}#" xdt:Transform="SetAttributes(connectionString)" xdt:Locator="Match(name)"/>
<connectionStrings />

Then after transformation you will get:

<connectionStrings>
    <add name="ApplicationDbContext" connectionString="con-some_value" xdt:Transform="SetAttributes(connectionString)" xdt:Locator="Match(name)"/>
<connectionStrings />



来源:https://stackoverflow.com/questions/59962204/how-to-use-environment-variables-set-in-azure-pipeline-in-the-web-config-file-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!