Reputation: 924
I'm able to inject environmental variables into an Azure Functions app just fine from the Microsoft Azure Functions App Dashboard:
Using System.Environment.GetEnvironmentVariable("AppName")
, I can retrieve the configured value:
[FunctionName("Status")]
public static async Task<IActionResult> Status(
[HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req)
{
var appName = System.Environment.GetEnvironmentVariable("AppName");
return new OkObjectResult($"{appName} - OK");
}
But I'd like environmental variables to come from the Azure DevOps Pipeline Variables as part of the release pipeline so that the value can be tied to a particular release, scope, etc:
How can this be done? Can it be done?
Upvotes: 2
Views: 3631
Reputation: 686
Azure Dev Ops (ADO) will typically make your pipeline variables, or linked groups of pipeline variables from its library, available as appsettings, but that means different things in different stacks.
To utilize those ADO-defined pipeline variables as a System.Environment.EnvironmentVariable
as C#/dotnet uses them (and which may be required for a library or framework you're using), there is an extra step.
Within the options for the deploy task "Deploy Azure Function App" in ADO, there is a section called "Application and Configuration Settings", and within that expandable section a text box for "App settings." Here you can define what would be injected as System.Environment.EnvironmentVariable
s, and give them a value that refers to the pipeline variables you set in ADO.
The syntax is -FUNCTION_APP_ENVIRONMENT_VARIABLE_NAME $(PipelineOrGroupVariableName)
. Where the text after the dash is the eventual name of the enviro variable within your dotnet application, followed by a space, and the value. To have the value refer to the value of our ADO pipeline variable, you place the pipeline variable name between the parentheses in $()
.
Multiple variables are fine, just use a space to separate, and never use a newline. If your pipeline variable's value has a space in it, you must use double quotes around the reference to it, like so: "$(PipelineVariableName)"
In OP's case, -AppName $(AppName)
in that text box would allow them to retrieve their value as an environment variable at runtime with var appName = System.Environment.GetEnvironmentVariable("AppName");
Upvotes: 6
Reputation: 222522
You can use Azure Key Vault to store secrets and access into your Azure functions then you can get the values from your Azure Devops and assign it.
Here is a sample
on how to do it.
Upvotes: 1