是否可以在资源的 ref 属性中使用变量:Azure DevOps YAML 的存储库? [英] Is it possible to use a variable in the ref property of resources:repository for Azure DevOps YAML?
问题描述
我有两个 AzureDevOps Git 分支:
I have two AzureDevOps Git branches:
master
feature/mybranch
我在 yaml 中定义了一个多阶段构建管道,其中一些步骤被模板化为单独的 .yml 文件.
I have a multi-stage build pipeline defined in yaml, where some of the steps are templated into separate .yml files.
在我的外部 azure-pipelines.yml 中,我引用了我的模板 .yml 所在的存储库:
In my outer azure-pipelines.yml I reference a repository where my template .yml's live:
resources:
repositories:
- repository: templates
type: git
name: MyProject/MyRepo
当我在master"分支中构建时,一切都很好,因为默认情况下,存储库将在 refs/heads/master 中查找.
when I'm building in the 'master' branch everything is good as by default the repository will look in refs/heads/master.
当我在功能分支中工作并且我想测试我的模板 .yml 文件的实验性更改时,我不希望它从主分支获取它们,我希望它使用来自分支的文件我在工作.
when I'm working in the feature branch and I want to test experimental changes to my template .yml files, I don't want it to fetch them from the master branch, I want it to use the files from the branch I am working in.
以下工作并允许我这样做:
The following works and allows me to do this:
resources:
repositories:
- repository: templates
type: git
name: MyProject/MyRepo
ref: refs/heads/feature/mybranch
但是,当我将其合并回 master 时,我显然不希望 'ref:' 仍然指向功能分支,因此我想使用变量动态生成 'ref:' 的值.
However, when I merge this back to master, I obviously don't want 'ref:' still pointing at the feature branch, so I'd like to generate the value of 'ref:' dynamically with a variable.
我试过使用 ref: $(Build.SourceBranch)
其中 $(Build.SourceBranch)
应该扩展到 'refs/heads/feature/mybranch'
I've tried using ref: $(Build.SourceBranch)
where $(Build.SourceBranch)
should expand to 'refs/heads/feature/mybranch'
但它不起作用.错误:
62638: "/azure-pipelines.yml: Could not get the latest source version for repository MySolution hosted on Azure Repos using ref refs/heads/$(Build.SourceBranch)."
推荐答案
不要在资源中引用 repo,而是使用这里描述的内联结帐
Instead of referencing the repo in resources, use inline checkout as described here
- checkout: git://MyProject/MyRepo@features/tools
这个 yaml 元素允许使用使用变量、参数的模板表达式,例如
And this yaml element allows use of template expressions using variables, parameters e.g.
- checkout: git://$(System.TeamProject)/$(repoName)@${{ variables.branchRef }}
或
- checkout: git://$(System.TeamProject)/$(repoName)@${{ parameters.branchRef }}
你可以动态改变它
或者另一种选择是使用脚本任务如下
Or the other alternative is use script task as below
- script: |
# note checkout: git://$(System.TeamProject)/${{ parameters.repoName }}@${{ parameters.repoRef }} this does not work if this task is run multiple times in same pipeline
# see here for more details :https://developercommunity.visualstudio.com/t/checkout-fails-with-an-error-occurred-while-loadin/1005991#T-N1270459
repoDir=$(Agent.BuildDirectory)/${{ parameters.repoName }}
/bin/rm -rf $repoDir
url_with_token=$(echo $(System.CollectionUri) | sed -e "s/https:///https://$(System.AccessToken)@/g")
git clone $url_with_token/$(System.TeamProject)/_git/${{ parameters.repoName }} $repoDir
cd $repoDir
git fetch origin '${{ parameters.repoRef }}':'localBranch'
git checkout localBranch
name: clone_script
displayName: Checkout using script ${{ parameters.repoName }}@${{ parameters.repoRef }}
这篇关于是否可以在资源的 ref 属性中使用变量:Azure DevOps YAML 的存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!