Azure DevOps pipeline for deploying only changed arm templates

拈花ヽ惹草 提交于 2020-07-09 06:21:08

问题


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.

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

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?


回答1:


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.

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

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'

Hope above helps!




回答2:


What you asked is not supported out of the box. From what I understood you want to have triggers (based on file change) per a step or a job (depending hoq you organize your pipeline). However, I'm not sure if you need this. Deploying ARM template which was not changed will not affect you Azure resources if use use Create Or Update Resource Group doc here

You can also try to manually detect which file was changed (using powershell for instance and git commands), then set a flag and later use this falg to fire or not some steps. But it looks like overkill for what you want achieve.



来源:https://stackoverflow.com/questions/61116629/azure-devops-pipeline-for-deploying-only-changed-arm-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!