提问者:小点点

Azure运营模式多存储库,多分支触发器,选择要从中构建的分支


在Azure运营模式管道中,我有

resources:
  repositories:
    - repository: self
      type: git
      name: MyProject.Web
      trigger:
        - master
    - repository: UiRepo
      type: git
      name: MyProject.Web.UI
      trigger:
        - main
[other stuff]
steps:
  - checkout: self
  - checkout: UiRepo
[other stuff]

这工作得很好:管道在完成对“self”repo的主分支或UiRepo的主分支的拉取请求时运行。签出从“self”的主分支和UiRepo的主分支拉取。

现在我正在为每个repo添加一个名为release/1.0的分支,并将在未来引入其他release/x. x分支。所以现在我有了:

resources:
  repositories:
    - repository: self
      type: git
      name: MyProject.Web
      trigger:
        - master
        - release/*
    - repository: UiRepo
      type: git
      name: MyProject.Web.UI
      trigger:
        - main
        - release/*
[other stuff]
steps: # ??????
  - checkout: self
  - checkout: UiRepo
[other stuff]

现在需要以下内容:

  • 如果触发分支的名称($Build. SourceChanchName)是'master'或'main',则从“self”中签出master,从UiRepo中签出main。
  • 否则,从两个存储库中检查一个值为$Build. Source驶离的分支。

https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/multi-repo-checkout?view=azure-devops#checking-out-a-specific-ref,我读到我可以在结帐步骤中使用内联语法来包含特定的引用。就我而言,我认为结果如下:

  - ${{ if in(variables['Build.SourceBranchName''], 'master', 'main') }}:
    - checkout: git://MyProject/MyProject.Web@refs/heads/master
    - checkout: git://MyProject/MyProject.Web.UI@refs/heads/main
  - ${{ else }}
    - checkout: git://MyProject/MyProject.Web@$(Build.SourceBranch)
    - checkout: git://MyProject/MyProject.Web.UI@$(Build.SourceBranch)

(or do I need to be using [ variables['Build.SourceBranch'] ] here instead of $(Build.SourceBranch)?)

But, by dispensing with the references in the checkout steps to "self" and UiRepo, this seems to be divorcing the connection between the checkout steps and the repos as I've already defined them. Does that matter? Is this correct anyway? If it isn't, how can I accomplish my goal?

共1个答案

匿名用户

您使用的if/else在运营模式概念中被命名为条件插入。

需要澄清的是,在这个概念中,它确实可以接受预定义的变量,但只能接受“在模板中可用”。

我注意到您使用的是Build. Reposity.SourceChanchName,这个变量甚至不在预定义变量列表中,更不用说“在模板中可用”的概念了。因此,您的条件插入的第一部分将不是进程。只有第二部分能够处理。

并且请不要使用'self'作为仓库的别名,这会造成歧义。(你目前的情况使用this没问题,这只是一个建议。)

所以基本上,你的管道定义应该是这样的:

trigger:
- none

resources:
  repositories:
  - repository: self
    type: git
    name: Repo1
    trigger:
      - master
      - release/*
  - repository: UiRepo
    type: git
    name: Repo2
    trigger:
      - main
      - release/*

pool:
  vmImage: ubuntu-latest

steps:
- ${{ if in(variables['Build.SourceBranchName'], 'master', 'main') }}: #This place needs to change
    - checkout: git://BowmanCP/Repo1@refs/heads/master
    - checkout: git://BowmanCP/Repo2@refs/heads/main
- ${{ else }}:
    - checkout: git://BowmanCP/Repo1@$(Build.SourceBranch) 
    - checkout: git://BowmanCP/Repo2@$(Build.SourceBranch) 
  

在我这边工作: