简体   繁体   English

在 azure devops 中:将 docker 容器部署到 azure 时,如何将我的管道变量作为 appsettings 注入?

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

I'm having trouble setting my appsettings in a deployed docker container on azure.我在 azure 上部署的 docker 容器中设置我的应用设置时遇到问题。

My setup:我的设置:

  • I have a .NET core app我有一个 .NET 核心应用程序
  • My build pipeline builds a docker image and pushes it to my container registry on azure.我的构建管道构建了一个 docker 映像并将其推送到我在 azure 上的容器注册表。
  • My release pipeline pulls the image based on a tag and deploys it to an azure web app.我的发布管道根据标签提取图像并将其部署到 Azure Web 应用程序。

I need to deploy the image to multiple environments.我需要将映像部署到多个环境。 Every environment has different appsettings.每个环境都有不同的应用程序设置。 I defined the variables in my pipeline "variables tab":我在管道“变量选项卡”中定义了变量: 在此处输入图片说明 And I need to send these to my azure so they can be used.我需要将这些发送到我的 azure 以便它们可以使用。 在此处输入图片说明

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) (见截图1) 在此处输入图片说明 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.因为每次添加或删除新的 appsetting 时我都必须编辑 appsettings 管道。 Also I believe that removing an appsetting here will just leave it on the deployed environment.此外,我相信在此处删除 appsetting 只会将其保留在已部署的环境中。

I'm deploying an existing docker image, so i'm unable to edit the appsetting.json file.我正在部署现有的 docker 映像,因此无法编辑 appsetting.json 文件。 I also won't make different docker files for each environment.我也不会为每个环境制作不同的 docker 文件。

Is there a way to achieve this?有没有办法实现这一目标? How can I extract / list the variables defined in my pipeline as docker variables or appsettings?如何提取/列出我的管道中定义的变量作为 docker 变量或 appsettings?

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).您可以在管道中定义管道变量,并将它们附加到特定范围(读取阶段)或发布范围(适用于所有阶段)。

Eg I have a variable defined as EnvironmentConnectionString which is defined in two scopes:例如,我有一个定义为EnvironmentConnectionString的变量,它定义在两个范围内:

  • Scope Test: "EnvironmentConnectionString = server=test-db; ...."范围测试:“EnvironmentConnectionString = server=test-db; ....”
  • Scope QA: "EnvironmentConnectionString = server=qa-db;..."范围 QA:“EnvironmentConnectionString = server=qa-db;...”
  • Score Release: "logging_flag=enabled"分数发布:“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注意使用这些变量的 $(variable name) 语法

When the different stages of the pipeline run, they automatically pick up the values specific to the stage and apply to azure app settings.当管道的不同阶段运行时,它们会自动选取特定于该阶段的值并应用于 azure 应用程序设置。

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.举个例子: Dev变集团和Release集团都有变数PortRequestTimeout ...该PortDev是4999,而PortRelease可能是5000,我们可以链接这些组特定的舞台范围, Dev变量组Dev阶段和Release阶段的Release组。

[![enter image description here][1]][1] [![在此处输入图像描述][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.我们必须appsettings定义appsettings输入。 [1]: https://i.stack.imgur.com/ukbjs.png [1]: https : //i.stack.imgur.com/ukbjs.png

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在通过 Azure DevOps 管道在 Azure Webapp 上部署容器映像时传递动态变量/秘密? - How can i pass dynamic variables/secrets while deploying container image on Azure Webapp through Azure DevOps pipeline? 在 Azure Devops 中,如何在运行 shell 脚本的“Azure CLI task v.2”中使用管道变量? - In Azure Devops, how can i use pipeline variables in a “Azure CLI task v.2” running shell script? 如何在Azure Devops中使用我自己的docker注册表? - how can i use my own docker registry in Azure Devops? 在 azure devops 上创建管道以在 azure 应用服务上部署 docker 容器? - Create a pipeline on azure devops to deploy a docker container on azure app service? 当新图像在不同的容器注册表上可用时,如何触发azure devops构建管道? - How can azure devops build pipeline be triggered when a new image is available on different container registries? 从Azure DevOps发布管道部署Azure Function时如何修改function.json? - How to modify function.json when deploying Azure Function from Azure DevOps release pipeline? 如何小写 Azure DevOps 管道变量名称 - How to lowercase Azure DevOps pipeline variables names 如何将 azure cli 变量传递给 Azure Devops 变量? - How can i pass an azure cli variable to Azure Devops variables? Azure DevOps CI/CD 未在活动中部署我的管道触发器 state - Azure DevOps CI/CD not deploying my pipeline triggers in an Active state Azure DevOps 发布管道 - 将环境变量传递给 docker 容器 - Azure DevOps Release Pipeline - Pass environment variable to docker container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM