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

时间:2020-10-30 11:49:33

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

我有两个合并前管道:

  1. uipr
  2. apipr

和一条CI / CD管道:

  1. 应用程序

在用户界面存储库中,只要生成到分支main的PR,就会运行 uipr 。 API存储库和 apipr 也是如此。

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

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


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


我想要实现的是:

  1. 如果resources.triggeringalias等于 uipr parameters.compileui等于 true ,则执行模板 compileui.yaml
  2. 如果resources.triggeringalias等于 apipr parameters.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') 仅在运行时发生,而不在模板扩展上发生。那就是我所缺少的,不知道该怎么做

2 个答案:

答案 0 :(得分:0)

如果得到了它,则仅当管道被另一个管道触发时或使用带标记的参数手动运行时,才希望部分管道运行。您可以这样实现:

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"

答案 1 :(得分:0)

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?