In azure devops: how can I inject my pipeline variables as appsettings when deploying a docker container to azure?

Viewed 1845

I'm having trouble setting my appsettings in a deployed docker container on azure.

My setup:

  • I have a .NET core app
  • My build pipeline builds a docker image and pushes it to my container registry on azure.
  • My release pipeline pulls the image based on a tag and deploys it to an azure web app.

I need to deploy the image to multiple environments. Every environment has different appsettings. I defined the variables in my pipeline "variables tab": enter image description here And I need to send these to my azure so they can be used. enter image description here

When I manually add them it works, but i want to extract them from my variables, so I only have to add them once. (see screenshot 1) enter image description here Edit: The screenshot above works. But this is not what I'm looking for. As I'd have to edit the appsettings pipeline each time I add or remove a new appsetting. Also I believe that removing an appsetting here will just leave it on the deployed environment.

I'm deploying an existing docker image, so i'm unable to edit the appsetting.json file. I also won't make different docker files for each environment.

Is there a way to achieve this? How can I extract / list the variables defined in my pipeline as docker variables or appsettings?

2 Answers

You can define pipeline variables in your pipeline and have them attached to a specific scope (read stage) or the release scope (applied to all stages).

E.g. I have a variable defined as EnvironmentConnectionString which is defined in two scopes:

  • Scope Test: "EnvironmentConnectionString = server=test-db; ...."
  • Scope QA: "EnvironmentConnectionString = server=qa-db;..."
  • Score Release: "logging_flag=enabled"

Then you can set this up in your "Application and Configuration Settings" like

- ConnectionString $(EnvironmentConnectionString)
- Logging $(logging_flag)

Note the $(variable name) syntax for using these variables

When the different stages of the pipeline run, they automatically pick up the values specific to the stage and apply to azure app settings.

You can have different variable groups for different stages. These Variable Groups should have same variables defined with different values.

For example: The Dev Variable Group and Release group both have variables Port, RequestTimeout... The Port in Dev is 4999 while the Port in Release could be 5000. We can link these groups to specific Stage scope, Dev variable group for Dev stage and Release group for Release stage.

[![enter image description here][1]][1]

Make sure all your stages have same settings like this, and then the variables with be replaced with correspondings values for different scopes.

Update:

Each stage in the pipeline is independent, they represent different environments. So we have to define the settings of stage or settings of the tasks within the stages one by one. We have to define the appsettings input one by one. [1]: https://i.stack.imgur.com/ukbjs.png

Related