是否可以在resources:repository的ref属性中为Azure DevOps YAML使用变量? [英] Is it possible to use a variable in the ref property of resources:repository for Azure DevOps YAML?

查看:131
本文介绍了是否可以在resources:repository的ref属性中为Azure DevOps YAML使用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个AzureDevOps Git分支:

master
feature/mybranch

我有一个用yaml定义的多阶段构建管道,其中某些步骤以模板的形式存储在单独的.yml文件中.

在我的外部azure-pipelines.yml中,我引用了.yml模板所在的存储库:

resources:
  repositories:
    - repository: templates
      type: git
      name: MyProject/MyRepo

当我在'master'分支中构建时,一切都很好,因为默认情况下,存储库将位于refs/heads/master中.

当我在功能分支中工作并且想要测试对模板.yml文件的实验性更改时,我不希望它从master分支中获取它们,而是希望它使用分支中的文件我在工作.

以下作品可以使我做到这一点:

resources:
  repositories:
    - repository: templates
      type: git
      name: MyProject/MyRepo
      ref: refs/heads/feature/mybranch

但是,当我将其合并回master时,显然我不希望'ref:'仍然指向功能分支,因此我想使用变量动态生成'ref:'的值./p>

我尝试使用ref: $(Build.SourceBranch),其中$(Build.SourceBranch)应该扩展为'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)."

解决方案

是否可以在的ref属性中使用变量 resources:Azure DevOps YAML的存储库?

对于这个问题,答案是肯定的.

关于为什么收到该错误消息的原因只是您使用的变量($(Build.SourceBranch))不正确.您应该使用$(Build.SourceBranchName).

通常,对于ref,我们应该输入master或任何其他功能分支.如

ref: refs/heads/master

这可能使您认为与$(Build.SourceBranch)的值相同.我知道它看起来一样,但又不同.实际上,对于服务器,它将读取确切的分支名称,而不是分支路径,我们可以使用经典的编辑器类型清楚地知道它:

根据经典的编辑器类型,我们可以在这里知道应该输入确切的分支名称.

因此,作为预定义变量$(Build.SourceBranch)的值是分支路径,但是对于$(Build.SourceBranchName),它表示的是完全相同的分支名称.

因此,如果要成功执行,则需要使用:$(Build.SourceBranchName).而且它在我这边工作.

希望这也可以帮助您远离错误消息.

为我工作的完整脚本是:

resources:
  repositories:
    - repository: templates
      type: git
      name: MyApp/MyconApp
      ref: $(Build.SourceBranchName)

I have two AzureDevOps Git branches:

master
feature/mybranch

I have a multi-stage build pipeline defined in yaml, where some of the steps are templated into separate .yml files.

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

when I'm building in the 'master' branch everything is good as by default the repository will look in refs/heads/master.

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

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.

I've tried using ref: $(Build.SourceBranch) where $(Build.SourceBranch) should expand to 'refs/heads/feature/mybranch'

But it doesn't work. Error:

62638: "/azure-pipelines.yml: Could not get the latest source version for repository MySolution hosted on Azure Repos using ref refs/heads/$(Build.SourceBranch)."

解决方案

Is it possible to use a variable in the ref property of resources:repository for Azure DevOps YAML?

For this question, the answer is Yes, it's possible.

About why you receive that error message, just is the variable($(Build.SourceBranch)) you used is incorrect. You should use $(Build.SourceBranchName).

As normal, for ref, we should input master or any other feature branches. Such as

ref: refs/heads/master

This may make you thought that this is same with the value of $(Build.SourceBranch). It looks same, I know, but different. In fact, for server, it will read the exactly branch name not the branch path, which we can clearly figure out with the classic editor type:

According with classic editor type, we can know here we should input the exactly branch name.

So, as the Predefined variables defined, the value of $(Build.SourceBranch) is the branch path, but for $(Build.SourceBranchName), it's represent a exactly branch name.

So, if you want to execute successfully, you need to use : $(Build.SourceBranchName). And it's worked on my side.

Hope this also can help you stay away from the error message.

Edit:

The complete script which is worked for me is:

resources:
  repositories:
    - repository: templates
      type: git
      name: MyApp/MyconApp
      ref: $(Build.SourceBranchName)

这篇关于是否可以在resources:repository的ref属性中为Azure DevOps YAML使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆