[英]How to configure ASPNET Core with Docker using VSTS to build, run tests and deploy to Azure with Unit Tests and Environment Variables
[英]Azure Pipeline Unit Tests & Environment Variables
努力尋找另一個問題的答案。 我在單元測試中有以下代碼(變量名已更改)。 此信息用於我的集成測試
var configuration = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddEnvironmentVariables()
.AddUserSecrets<MyTestTests>()
.Build();
var option= new Option();
option.x1 = configuration.GetValue<string>("Option:x1");
option.x2 = configuration.GetValue<string>("Option:x2");
option.x3 = configuration.GetValue<string>("Option:x3");
option.x3= configuration.GetValue<string>("Option:x4");
return option;
當我的單元測試在本地運行時,這在本地運行良好。 但是,當我的集成測試在 Azure 管道中運行時,它不會獲取環境變量。
我以以下格式創建了它們
option__x1其中 _ 是雙下划線。
如果環境變量是開放的,那么它可以工作,但是,如果它們被設置為秘密,那么它就不起作用。
有人有什么主意嗎?
Azure 管道單元測試和環境變量
此行為旨在保護秘密變量不被暴露在任務中。
該文檔指出秘密變量是:
這就是您不能在任務中使用秘密變量的原因。
為了解決這個問題,我們需要明確 map 秘密變量:
variables:
GLOBAL_MYSECRET: $(mySecret)
GLOBAL_MY_MAPPED_ENV_VAR: foo
steps:
- powershell: |
env:
MY_MAPPED_ENV_VAR: $(mySecret) # right way to map to an env variable
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.