未检索到 Azure 函数中的环境变量

Environment variable in Azure function is not retrieved

我有一个 Azure 函数。它应该像这样检索一些环境变量值:

   string username = Environment.GetEnvironmentVariable("BASIC_AUTH_USERNAME", EnvironmentVariableTarget.Process) ?? _config["BASIC_AUTH_USERNAME"] ?? _config["Values:BASIC_AUTH_USERNAME"];

对于本地测试,变量设置在local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
     "BASIC_AUTH_USERNAME": "Set in Azure. For development, set in User Secrets (secrets.json)",
  ...
  }
}

在 Azure 中,我在发布管道中设置了相应的变量:

- stage: Production
dependsOn: QA
variables:
- group: Production

一切似乎都已就绪,但是当我通过 Postman 运行 Azure 函数时,用户名是空的。我可以缺少什么?

ADO 中的变量可作为构建代理中的环境变量使用。但是你需要配置你的 Azure Functions 部署任务来传递你想要的任何配置变量。

假设您使用 Function Deploy 任务,查找 appSettingshttps://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-function-app?view=azure-devops