【问题标题】:YAML pipeline: Execute task if RESOURCES_TRIGGERINGALIAS = "ui"YAML 管道:如果 RESOURCES_TRIGGERINGALIAS = "ui" 则执行任务
【发布时间】:2020-10-30 11:49:33
【问题描述】:

我有两个预合并管道:

  1. uipr
  2. apipr

还有一个 CI/CD 管道:

  1. 应用

在 UI 存储库中,每当一个 PR 生成到分支 main 时,uipr 就会运行。 API repo 和 apipr 也是如此。

如果此 PR 前运行成功,Application 管道将运行。

Application 管道将填充 resources.triggeringalias 变量,其中包括执行 CI 的预合并管道的名称:uipr / apipr


Application 管道还有两个布尔参数,分别称为 compileuicompileapi,默认设置为 false。如果我手动运行Application 管道并将其中任何一个设置为true,它将执行模板以编译所选的任何一个。


我想要实现的是:

  1. 如果 resources.triggeringalias 等于 uiprparameters.compileui 等于 true 则执行模板 compileui.yaml
  2. 如果 resources.triggeringalias 等于 apiprparameters.compileapi 等于 true 然后执行模板 compileapi.yaml

这是我目前所拥有的:

parameters:
- name: compileui
  displayName: compile ui
  type: boolean
  default: false
- name: compileapi
  displayName: compile api
  type: boolean
  default: false

resources:
  repositories:
  - repository: api
  - repository: ui
  pipelines:
    - pipeline: ui
      source: uipr
      trigger: true
    - pipeline: api
      source: apipr
      trigger: true

stages:
- stage: ci
  jobs:
  - job: ui
    steps:
    - checkout: ui
    - ${{ if or(eq(variables['resources.triggerinalias'], 'ui'),eq(parameters.compileui, true)) }}:
      - template: steps/build-ui.yaml
  - job: api
    steps:
    - checkout: api
    - ${{ if or(eq(variables['resources.triggeringalias'], 'api'),eq(parameters.compileapi, true)) }}:
      - template: steps/build-api.yaml

我知道 (variables['resources.triggeringalias'], 'ui') 仅在运行时发生,而不是在模板扩展时发生。这就是我所缺少的,不知道该怎么做

【问题讨论】:

  • 你好@Leo Liu-MSFT 我即将测试你提出的解决方案我会告诉你谢谢你的帮助!

标签: azure-devops azure-yaml-pipelines azure-devops-yaml


【解决方案1】:

YAML 管道:如果 RESOURCES_TRIGGERINGALIAS = “ui”,则执行任务

您可以将管道资源元数据用作预定义变量resources.pipeline.<Alias>.pipelineName,并将参数用作作业级别的条件:

- stage: ci
  jobs:
  - job: ui
    pool:
      vmImage: 'vs2017-win2016'
    condition: or(eq(variables['resources.pipeline.ui.pipelineName'], 'ui'), eq(${{parameters.compileui}}, true))
    steps:
      - template: steps/build-ui.yaml

您可以查看以下参考资料了解更多详情:

The pipeline resource metadata as predefined variables

How to get previous build pipeline's build number in case of consecutive build pipelines?

【讨论】:

    【解决方案2】:

    如果我知道了,您希望仅当管道由另一个管道触发或使用标记参数手动运行时才运行部分管道。您可以通过以下方式实现:

    parameters:
    - name: compileui
      displayName: compile ui
      type: boolean
      default: false
    
    trigger: none
    pr: none
    
    resources:
      pipelines:
      - pipeline: hadar   # Name of the pipeline resource
        source: kmadof.hadar # Name of the pipeline referenced by the pipeline resource
        trigger: 
          branches:
          - releases/*
          - master
    
    stages:
    - stage: printer
      jobs:
      - job: printVariables
        steps:
        - script: |
            echo "$(Build.Reason)"
            echo "Build run name was: $(resources.pipeline.hadar.runName)"
    - ${{ if or(and(eq(parameters.compileui, true), eq(variables['build.reason'], 'Manual')), eq(variables['build.reason'], 'ResourceTrigger')) }}:
      - stage: ci
        condition: and(in(variables['build.reason'], 'Manual', 'ResourceTrigger'), succeeded())
        jobs:
        - job: ui
          steps:
          - script: echo "Hello from ci"
    
    

    【讨论】:

    • 你好@Krzysztof-Madej 我添加了更多信息(希望能更清楚),感谢您的回复
    猜你喜欢
    • 2020-08-27
    • 2021-03-28
    • 2020-08-07
    • 2020-11-23
    • 1970-01-01
    • 2021-12-13
    • 2019-02-26
    • 2020-05-28
    • 2022-11-10
    相关资源
    最近更新 更多