DevOps构建管道-模板-从同一分支运行



我正在测试一个POC构建管道,并尝试将模板用作一种参考库,这样我就可以在多个管道中重用同一代码,同时如果需要更改,还可以进行更简单的编辑/更新。

我希望能够从DevOps分支运行管道,并在验证/运行该构建管道中的所有模板管道时引用相同的分支(或标记(。

最终目标是将多个变量传递给模板(通过循环/针对每个变量(,以创建多个引用公共存储库的构建工件(在本测试中是一个公共terraform github repo(。然后,我们可能只需要维护变量数据,而不需要维护源代码/引用代码。

我以为DevOps做了这种"OOTB",但意识到事实可能并非如此。类似这样的东西:

Repo:InfraAsData(所有管道(
分支:功能/稀疏克隆Repo
更新了azure-pipeline.yaml并添加了模板/s稀疏克隆.yaml(仅供参考-此模板也未测试(

azure-pipeline.yaml:

trigger: none
resources:
repositories:
- repository: InfraAsData #resource name to be used in the build pipeline
type: git #Azure git 
name: 'IAC/InfraAsData'
refs: 'refs/heads/$(branch_name)'
parameters:
- name: branch
displayName: branch
type: string
default: $(Build.SourceBranch)
variables:
- name: branch_name
value: ${{ parameters.branch }}
- template: ./templates/variables/resource-groups.yaml@InfraAsData #variables template
pool:
vmImage: 'ubuntu-latest'
stages:
#sparse clone the target public repo blobs to build pipeline artifacts directory
- stage: Template-Sparse-Clone-To-Artifacts
displayName: 'Test Sparse Clone to Artifacts'
jobs:
- deployment: sparseCloneTfModule
displayName: 'Sparse Clone Terraform module to Artifacts'
environment: Test
strategy:
runOnce:
deploy:
steps:
- template: $(Build.SourceBranch)/templates/sparse-clone.yaml@InfraAsData #Create artifacts based on template variables
parameters: #module specific var from var template
repoUrl: '${{ variables[template.repoUrl] }}' 
repoPath: '${{ variables[template.repoPath] }}'
artifactPath: '${{ variables[template.artifactPath] }}'

稀疏克隆.yaml

parameters:
repoUrl: ''
repoPath: ''
artifactPath: ''
#testing
steps:
- script: |
md $(Build.BinariesDirectory)/$(parameters.artifactPath)
git clone --filter=blob:none --sparse $(parameters.repoUrl) $(parameters.artifactPath)
cd $(Build.BinariesDirectory)/$(parameters.artifactPath)
git sparse-checkout init --cone
git sparse-checkout set $(parameters.repoPath)
git checkout main
dir $(Build.BinariesDirectory)/$(parameters.artifactPath)
displayName: 'Clone Github Repo Subdirectory - filter blob none'

当我从DevOps中的feature/s稀疏克隆repo分支运行此操作时,我得到一个错误,即主分支中不存在新模板(当然不存在(。

/build/test/azure-pipeline.yaml:在存储库中找不到文件/templates/variables/resource-groups.yamlhttps://dev.azure.com//IAC/_git/InfraAsData分支引用人/主管/主版本

我也尝试过不使用resources.repository,使用$(Build.SourceBranch($(Build.SourceBranchName(作为refs值和模板路径引用(类似于第二个模板示例(。

编辑:如果我删除resource.repositories引用,则会识别正确的分支,但azure-pipeline.yaml相对路径会附加到模板路径:

/build/test/azure-pipeline.yaml:在存储库中找不到文件/build/test/$(build.SourceBranch(/templates/variables/resource-groups.yamlhttps://dev.azure.com//IAC/_git/InfraAsData分支引用/头/特征/管道

所以正确的分支,但不是正确的相对路径。

编辑2:我可以删除资源部分并使用相对路径

../../templates/variables/resource-groups.yaml

但这不是动态的,对于"根"引用,因此如果文件夹结构发生变化(EG我将azure-pipeline.yaml向上移动了一个级别(,则不会起作用

我确信我遗漏了一些明显的东西,或者误解了文档(或管道模板(。感谢您的指点!

当我从DevOps I中的功能/稀疏克隆repo分支运行时得到一个新模板在主分支中不存在的错误(当然不是(。

根据您的描述,您似乎认为资源的"refs"决定了模板部分别名的分支。

但事实并非如此,"resources.repositories.repository"没有名为"refs"的节,只允许使用"ref"。

在您的情况下,您使用了一个名为"refs"的部分,但该部分并不存在Pipeline将使用存储库的默认分支来查找模板YAML。因此,更改存储库的默认分支或将"refs"更改为"ref"将解决第一个问题。


我注意到您在参考资料部分使用了变量,您有两个错误。

其中之一是"$(("不能在编译时部分使用。另一个是甚至编译时变量也不允许出现在"ref"部分中

ref

字符串

要签出的ref名称;默认为"refs/heads/main"。分支机构每当资源触发器触发时,默认情况下都会检出。不接受变量。

您可以这样使用您的管道:

trigger: none
resources:
repositories:
- repository: InfraAsData #resource name to be used in the build pipeline
type: git #Azure git 
name: 'BowmanCP/template_branch'
ref: 'refs/heads/${{parameters.branch}}'
parameters:
- name: branch
displayName: branch
type: string
default: main2
pool:
vmImage: 'ubuntu-latest'
stages:
#sparse clone the target public repo blobs to build pipeline artifacts directory
- stage: Template_Sparse_Clone_To_Artifacts
displayName: 'Test Sparse Clone to Artifacts'
jobs:
- deployment: sparseCloneTfModule
displayName: 'Sparse Clone Terraform module to Artifacts'
environment: Test
strategy:
runOnce:
deploy:
steps:
- template: ./templates/sparse-clone.yaml@InfraAsData #Create artifacts based on template variables
parameters: #module specific var from var template
repoUrl: 'xxx' 
repoPath: 'xxx'
artifactPath: 'xxx'

基本上,变量不能在您的情况下使用,并且没有参数重用功能。这就是一切。

最新更新