Azure DevOps管道仅用于部署更改的手臂模板 [英] Azure DevOps pipeline for deploying only changed arm templates
问题描述
我们在Azure DevOps上有一个带有仓库的项目,我们在其中存储基础架构的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模板的部署的管道?让我们来看一个示例.我们在回购中提供了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
很容易获得更改的文件.获得更改的文件名后,您需要
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'
希望上面有帮助!
这篇关于Azure DevOps管道仅用于部署更改的手臂模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!