With the recent intensification of continuous integration, a scenario needs to pass the configuration to an ASP. NET Core application through the GITLAB-CI environment variable (Variables, CI/CD, Settings, Settings).
The configuration in Appsettings.json is like this
{" ossclient": { "Accesskeyid": "", "Accesskeysecret": "" }}
The question before you is how to pass the above configuration through an environment variable, without modifying the code that reads the configuration?
Services. Configure<osscredential> (configuration. GetSection ("ossclient"));
The ASP. NET Core enabled the read configuration from environment variable by default, and Config is included in Webhost.createdefaultbuilder (). Addenvironmentvariables (), as long as the configuration is passed in the correct format, it can be done.
The question remains one--what format provides ossclient Accesskeyid and Accesskeysecret configurations in environment variables?
Found a blog post on the Internet Read Connections Strings & Config Values from Environment Variables
Export connectionstrings__default= "server=database-server; Database=sample; Trusted_connection=true; "
It turns out that only 2 underscores are required to separate sectionname and key.
View Microsoft.Extensions.Configuration.EnvironmentVariables in Environmentvariablesconfigurationprovider implementation of the source code is also confirmed this point.
private static string Normalizekey (String key) { return key. Replace ("__", Configurationpath.keydelimiter);}
Validation in the actual project using the following environment variable configuration is indeed valid.
Export Ossclient__accesskeyid=xxxexport ossclient__accesskeysecret=yyy
Success is also verified in Gitlab-ci.
Real-world experience with the powerful and flexible configuration capabilities of ASP.
ASP. NET Core read configuration from GITLAB-CI environment variable