我有一个
Azure DevOps
存储库,我想用许多(管道)模板来填充它。
每个模板文件代表一个
Azure Container App Job
。
在合并到生产时,我希望管道能够识别更改的模板并运行这些模板。然后,模板将在我的
Container App Jobs
中更新/添加 Container Environment
。
如果这样不行的话什么方法会更好?
澄清一下:我已经准备好了一个容器,我想将其作为具有不同参数的多个 Azure 容器应用程序作业启动。我不是问如何在 Container Registry 中创建容器。
恐怕没有现成的变量可以包含更改的文件信息。
希望管道能够识别已更改的模板并运行这些模板
根据您的描述,您正在使用 Pipeline 中的模板来运行作业。
我们需要运行脚本来收集更改的文件并发送到管道以确认要运行哪些模板。
由于YAML模板会在编译时展开,脚本运行会在运行时执行,所以我们需要创建两个Pipeline来实现需求。
这是一个例子:
管道一:使用Rest API和Git命令收集更改的文件并将结果发送到管道二。
steps:
- checkout: self
fetchDepth: 0
- powershell: |
$prefix_url = "$(System.CollectionUri)"
$project = "$(System.TeamProject)"
$buildId = $(Build.BuildId)
$url = "${prefix_url}${project}/_apis/build/builds/${buildId}/changes?includeSourceChange=true&api-version=7.0"
$headers = @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
$response=Invoke-RestMethod -Uri $url -Headers $headers
$changesCount = $response.count
Write-Host "The count of commits associated with this build is $changesCount."
$modifiedFiles= git diff HEAD HEAD~$changesCount --name-only
echo $modifiedFiles
$modifiedFiles1 = $modifiedFiles -join ','
echo $modifiedFiles1
$url="${prefix_url}${project}/_apis/pipelines/{PipelineTwoID}/runs?api-version=5.1-preview"
$JSON = "
{
`"resources`": {
`"repositories`": {
`"self`": {
`"ref`": `"refs/heads/main`"
}
}
},
`"templateParameters`": {
`"files`":`"[$modifiedFiles1]`"
},
}"
$response = Invoke-RestMethod -Uri $url -Headers@{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"} -Method Post -Body $JSON -ContentType application/json
管道二:使用Object类型参数和每个表达式循环更改的文件来运行模板。
parameters:
- name: files
type: object
jobs:
- ${{ each file in parameters.files }}:
- template: ${{file}}
结果:
管道一:
管道二: