问题
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="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"" 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