2

I'm able to inject environmental variables into an Azure Functions app just fine from the Microsoft Azure Functions App Dashboard:

Azure Functions App Application Settings

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");
}

And it returns reliably: Postman Result

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: Azure DevOps Pipeline Variables

How can this be done? Can it be done?

2 Answers 2

6

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.EnvironmentVariables, 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");

image of azure devops portal's Application and Configuration Settings section, with the App Settings text box filled with -FUNCTION_APP_ENVIRONMENT_VARIABLE_NAME $(PipelineOrGroupVariableName)

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

This wasn't the complete answer, but it led me to it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.