如何参数化Azure DevOps部署YAML管道以部署到多个环境

发布于 2025-02-02 12:38:33 字数 308 浏览 6 评论 0原文

我有一个Azure DevOps部署YAML管道,该管道可创建Azure App服务并将代码部署到它。实际管道更为复杂,但是我正在简化此问题。

当前,我的管道可以成功部署到特定的Azure订阅(服务连接器),并具有变量中定义的资源名称。

我需要对管道进行参数化,以便它可以使用多个服务连接器部署到几个不同的环境(意味着Azure订阅)。每个环境都有不同的Azure资源命名约定。

有什么方法可以从XML或JSON文件中读取管道变量的值?这样,我可以为每个环境拥有多个配置文件,并将它们作为存储库的一部分存储。

这是多环境部署管道配置的正确方法吗?

I have an Azure DevOps Deployment YAML Pipeline that creates an Azure App Service and deploys code to it. The actual pipeline is more complex, but I am simplifying it for this question.

Currently my pipeline can successfully deploy to a specific Azure Subscription (Service Connector) with resource names defined in variables.

I need to parametrize the pipeline so that it can deploy to several different environments (means Azure Subscriptions) using multiple Service Connectors. Each environment has a different Azure Resource naming convention.

Is there any way to read the value of pipeline variables from an XML or JSON file? This way I can have multiple config files for each environment and store them as part of my repository.

Is this a right approach for multi-environment deployment pipeline configuration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪我终i 2025-02-09 12:38:33

您可以使用变量模板。还有另一个有趣的链接:了解有关使用模板可变重复使用的更多信息

在这里,我有这个平坦的文件夹结构(为了清楚示例的清晰度):

.
|  deploy-app.job.yaml
|  deploy-app.pipeline.yaml
|  variables.dev.yaml
|  variables.prod.yaml

因此,我们正在尝试运行可重复使用的 job deploly-app.job.yaml,带有不同的变量集。

我已经在每个变量中定义了一些变量。{env} .yaml文件

# variables.dev.yaml
variables:
  vmImage: ubuntu-20.04
  serviceConnection: dev-service-connection

# variables.prod.yaml
variables:
  vmImage: ubuntu-20.04
  serviceConnection: prod-service-connection

deploy-app.job.yaml文件接受一个允许注入变量模板的参数:

# deploy-app.job.yaml
parameters:
- name: envVariablesTemplate
  type: string

jobs:
- deployment: deploy
  variables:
  # Inject the verianle template here
  - template: ${{ parameters.envVariablesTemplate }}
  pool:
    # Use the variable from the template
    vmImage: ${{ variables.vmImage }}
  strategy:
    runOnce:
      deploy:
        steps:
        - task: AzureCLI@2
          displayName: Hello from azure cli
          inputs:
            # Use the variable from the template
            azureSubscription: ${{ variables.serviceConnection }}
            scriptType: pscore
            scriptLocation: inlineScript
            inlineScript: echo 'Hello from azure cli'

在主管道中,我可以创建不同的阶段并注入所需的Vairable:

# deploy-app.pipeline..yaml
stages:
- stage: dev
  condition: succeeded()
  jobs:
  - template: ./deploy-app.job.yaml
    parameters:
      envVariablesTemplate: ./variables.dev.yaml

- stage: prod
  dependsOn: dev
  condition: succeeded()
  jobs:
  - template: ./deploy-app.job.yaml
    parameters:
      envVariablesTemplate: ./variables.prod.yaml

根据您的需求,您可以添加多个变量模板,具有命名约定等。确实取决于您,并取决于管道的复杂性。

You can use variable templates. There is another interesting link: Learn more about variable reuse with templates.

Here I have this flat folder structure (for the clarity of the sample):

.
|  deploy-app.job.yaml
|  deploy-app.pipeline.yaml
|  variables.dev.yaml
|  variables.prod.yaml

So here we're trying to run the reusable job deploy-app.job.yaml with different variable sets.

I've defined some variables in each variable.{env}.yaml files

# variables.dev.yaml
variables:
  vmImage: ubuntu-20.04
  serviceConnection: dev-service-connection

# variables.prod.yaml
variables:
  vmImage: ubuntu-20.04
  serviceConnection: prod-service-connection

The deploy-app.job.yaml file accepts a parameter that allow to inject a variable template:

# deploy-app.job.yaml
parameters:
- name: envVariablesTemplate
  type: string

jobs:
- deployment: deploy
  variables:
  # Inject the verianle template here
  - template: ${{ parameters.envVariablesTemplate }}
  pool:
    # Use the variable from the template
    vmImage: ${{ variables.vmImage }}
  strategy:
    runOnce:
      deploy:
        steps:
        - task: AzureCLI@2
          displayName: Hello from azure cli
          inputs:
            # Use the variable from the template
            azureSubscription: ${{ variables.serviceConnection }}
            scriptType: pscore
            scriptLocation: inlineScript
            inlineScript: echo 'Hello from azure cli'

In the main pipeline, I can create different stages and inject the desired vairables:

# deploy-app.pipeline..yaml
stages:
- stage: dev
  condition: succeeded()
  jobs:
  - template: ./deploy-app.job.yaml
    parameters:
      envVariablesTemplate: ./variables.dev.yaml

- stage: prod
  dependsOn: dev
  condition: succeeded()
  jobs:
  - template: ./deploy-app.job.yaml
    parameters:
      envVariablesTemplate: ./variables.prod.yaml

Based on your needs, you can add multiple variable templates, having a naming convention etc. Really up to you and depends on the complexity of your pipelines.

提笔落墨 2025-02-09 12:38:33

通过使用XML变换,我们可以执行操作。检查以下链接以获取完整的步骤。

https://www.dragonspears.com/博客/操作方向 - 连续访问 - 跨度 - 元素环境

  1. 创建不同的阶段。

  1. 使用XML变压器

  1. ​敏感信息可以存储和固定在Azure Pipelines中,而在纯文本转换文件中。

  1. 连续部署

”“在此处输入图像描述”

上面提到的链接中提到的相同步骤。

By using XML transformation, we can perform the operation. Check the below link to get the complete steps.

https://www.dragonspears.com/blog/how-to-handle-continuous-deployment-across-multiple-environments

  1. Create different stages.

enter image description here

  1. Create build transformation using XML transformer

enter image description here

  1. Another option is to utilize ‘XML variable substitution.’ Sensitive information can be stored and secured within Azure Pipelines vs. in plain text transformation files.

enter image description here

  1. Continuous Deployment

enter image description here

The same steps mentioned here are available in the link mentioned above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文