在管道之间传递分支名称

问题描述 投票:0回答:1

到目前为止,我正在使用下面的管道来触发主管道。它跟踪开发人员推送到主分支的标签,如果是,则开始运行。

first_pipeline.yml

trigger:
  tags:
    include:
      - '*'
  paths:
    include:
      - master

variables:
  agent_pool: 'DEVAGENT'

pool:
  name: $(dev_pool)

上面的管道触发了一个:

main_pipeline.yml

trigger: none
pr: none

resources:
  repositories:
    - repository: Grand
      type: git
      name: Automotive/Grand
      ref: master
      endpoint: Grand
    - repository: CICD
      type: git
      name: pipelines
      ref: master
  pipelines:
    - pipeline: Grand
      source: 'Grand'
      project: Automotive
      trigger: true

variables:
  - template: variables.yml

stages:
    - stage: DEV
      displayName: 'Development'
      variables:
        - name: environment
          value: ${{ variables.env_dev}}
      pool:
        name: ${{ variables.agent_dev }}
      jobs:
        - deployment: DEV
          displayName: 'Deploying to DEV'
      # Rest of the code   
  - stage: PRD
    displayName: 'Production Stage'
    variables:
      - name: environment
        value: ${{ variables.prd }}
    pool:
      name: ${{ variables.agent_prd }}
    jobs:
      - deployment: PRDDeploy
        displayName: 'Deploying to PRD'
    # Rest of the code

上述管道将更改部署到 DEV 阶段,而不是 PRD 阶段。

现在我正在尝试修改管道以使其工作方式如下:

  • 当更改来自 dev 或部署分支时,则仅运行 DEV 阶段
  • 当更改来自功能或修补程序分支时,它将直接进入 PRD
  • 当它像现在一样来自主人时,它就会去 给 DEV 和 PRD。

到目前为止,它仅适用于最后一个条件,我的第一次尝试会导致错误,因为我不确定如何将这些分支名称从

first
传递到
main
管道并修改第二个。

azure-devops azure-pipelines azure-pipelines-yaml
1个回答
0
投票

当您使用

pipeline.resources.repository.trigger
还有其他可用的管道变量:

resources.pipeline.<Alias>.projectName
resources.pipeline.<Alias>.projectID
resources.pipeline.<Alias>.pipelineName
resources.pipeline.<Alias>.pipelineID
resources.pipeline.<Alias>.runName
resources.pipeline.<Alias>.runID
resources.pipeline.<Alias>.runURI
resources.pipeline.<Alias>.sourceBranch
resources.pipeline.<Alias>.sourceCommit
resources.pipeline.<Alias>.sourceProvider
resources.pipeline.<Alias>.requestedFor
resources.pipeline.<Alias>.requestedForID

根据您的触发器,您似乎正在使用“标签”或提交“master”来触发管道。您应该能够使用

$(resources.pipeline.Grand.sourceBranch)
来获取原始分支。 根据这个答案,当标签触发构建时,sourceBranch将以
/refs/tags/<tag-name>
开头。

基于此,您应该能够执行以下操作:

#first pipeline
trigger:
  tags:
    include:
    - '*'
  branches:
    include:
    - master

...


```yaml
# main_pipeline.yml
trigger: none

resources:
  pipelines:
  - pipeline: Grand # this is the alias
    source: 'Grand'
    project: Automotive
    trigger: true

stages:
- stage: dev
  condition: |
    or(
      startsWith(variables['resources.pipeline.Grand.sourceBranch', 'refs/tags/dev'),
      eq(variables['resource.pipeline.Grand.sourceBranch', 'refs/heads/master')
      ),
  ...

- stage: prod
  condition: |
     or(
      startsWith(variables['resources.pipeline.Grand.sourceBranch', 'refs/tags/feature'),
      eq(variables['resource.pipeline.Grand.sourceBranch', 'refs/heads/master')
     )
  ...
© www.soinside.com 2019 - 2024. All rights reserved.