用于仅部署更改的 arm 模板的 Azure DevOps 管道 [英] Azure DevOps pipeline for deploying only changed arm templates

查看:22
本文介绍了用于仅部署更改的 arm 模板的 Azure DevOps 管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 Azure DevOps 上有一个带有 repo 的项目,我们在其中存储了基础架构的 ARM 模板.我们想要实现的是在 master 分支的每个提交上部署模板.

We have a project with repo on Azure DevOps where we store ARM templates of our infrastructure. What we want to achieve is to deploy templates on every commit on master branch.

问题是:是否可以定义一个只能触发部署随该提交更改的 ARM 模板的管道?让我们举个例子.我们在 repo 中有 3 个模板:

The question is: is it possible to define one pipeline which could trigger a deployment only of ARM templates changed with that commit? Let's go with example. We 3 templates in repo:

t1.json
t2.json
t3.json

最新的提交只更改了 t2.json.在这种情况下,我们希望管道仅将 t2.json 部署为 t1.json 并且 t3.json 在此提交中未更改.

The latest commit changed only t2.json. In this case we want pipeline to only deploy t2.json as t1.json and t3.json hasn't been changed in this commit.

是否可以创建一个通用管道,或者我们应该为每个由特定文件提交触发的模板创建单独的管道?

Is it possible to create one universal pipeline or we should rather create separate pipeline for every template which is triggered by commit on specific file?

推荐答案

可以只定义一个管道来部署更改的模板.您需要添加一个脚本任务来获取管道中已更改的模板文件名.

It is possible to define only one pipeline to deploy the changed template. You need to add a script task to get the changed template file name in your pipeline.

使用 git 命令很容易获取更改的文件 git diff-tree --no-commit-id --name-only -r commitId.当您获得更改后的文件名时,您需要 使用表达式##vso[task.setvariable variable=VariableName]value将其分配给一个变量.然后你可以在 AzureResourceGroupDeployment 任务

It is easy to get the changed files using git commands git diff-tree --no-commit-id --name-only -r commitId. When you get the changed file's name, you need to assign it to a variable using expression ##vso[task.setvariable variable=VariableName]value. Then you can set the csmFile parameter like this csmFile: '**\$(fileName)' in AzureResourceGroupDeployment task

您可以查看以下 yaml 管道,例如:

You can check below yaml pipeline for example:

- powershell: |
   #get the changed template
   $a = git diff-tree --no-commit-id --name-only -r $(Build.SourceVersion)  

   #assign the filename to a variable        
   echo "##vso[task.setvariable variable=fileName]$a" 

- task: AzureResourceGroupDeployment@2
  inputs:
    ....
    templateLocation: 'Linked artifact'
    csmFile: '**\$(fileName)'

也很容易定义多个管道来实现仅部署更改的模板.您只需要添加 路径触发到每个管道中的特定模板文件.使更改后的模板文件只触发其对应的管道.

It is also easy to define multiple pipelines to achieve only deploying the changed template. You only need to add the paths trigger to the specific template file in the each pipeline. So that the changed template file only triggers its corresponding pipeline.

trigger: 
  paths:
    include: 
    - pathTo/template1.json
...

 - task: AzureResourceGroupDeployment@2
      inputs:
        ....
        templateLocation: 'Linked artifact'
        csmFile: '**\template1.json'

希望以上有帮助!

这篇关于用于仅部署更改的 arm 模板的 Azure DevOps 管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆