Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

How to find the deployment environment of an azure function app in code

Scenerio is that:

  1. We have Azure DevOps and we can run a pipeline into one of x number of named environments enter image description here

  2. We make use of Azure App Configuration, and labels for the values for each environment. So for each setting, it might have a different value depending on the label

enter image description here

It occurs to me that if i match up the label to the same as the names of the environments, then in code, when i get the config value, if I can somehow determine what environment I've been deployed to (speaking from the code's point of view) then i can just pass this variable when getting the app config and i will have the correct config settings for my environment.

            var environment = // HERE find my deployed to environment as in pipeline (1.)
        var credentials = new DefaultAzureCredential();


            configurationBuild.AddAzureAppConfiguration(options =>
            {
                options.Connect(settings.GetValue<string>("ConnectionStrings:AppConfig"))
                        .Select(KeyFilter.Any, LabelFilter.Null)
                        .Select(KeyFilter.Any, labelFilter: environment);
            });

I was thinking that the solution would be something of the form of setting the environment in the azure-pipelines.yaml where the pipeline somehow knows the choice of environment and then reading it in code back out of the environment variable. but i dont know how to do that, or if there is a better way to do it? Thanks in advance for any help offered.

question from:https://stackoverflow.com/questions/65912361/how-to-find-the-deployment-environment-of-an-azure-function-app-in-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the pipeline variables to pass the environment value to your code. The pipeline variables you defined in azure-pipelines.yaml will get injected as environment variables for your platform, which allows you to get their values in your code using Environment.GetEnvironmentVariable().

So you can define a pipeline variable in the azure-pipelines.yaml like below example(ie.DeployEnv):

parameters:
- name: Environment
  displayName: Deploy to environment
  type: string
  values:
  - none
  - test
  - dev

variables:
  DeployEnv: ${{parameters.Environment}}

trigger: none

pool:
  vmImage: 'windows-latest'

Then you can get the pipeline variable (ie.DeployEnv) in you code like below:

 using System;

 var environment = Environment.GetEnvironmentVariable("DeployEnv");
 
 var credentials = new DefaultAzureCredential();
 ....

Another workaround is to define an environment property in the config(eg.web.config) file. And you can read the environment property in your code. In the pipeline you need to add tasks to replace the value of the environment property in the config file. See this thread for more information.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...