Nappa
Nappa

Reputation: 13

YAML pipeline: Execute task if RESOURCES_TRIGGERINGALIAS = "ui"

I have two pre-merge pipelines:

  1. uipr
  2. apipr

And one CI/CD pipeline:

  1. Application

in the UI repo, whenever a PR is generated to the branch main the uipr will run. the same goes for the API repo and apipr.

If this pre PR run is successful, the Application pipeline will run.

The Application pipeline will populate the resources.triggeringalias variable which includes the name of the pre-merge pipeline that executed the CI: uipr / apipr


The Application pipeline also has two boolean parameters called compileui and compileapi, set to false by default. If I run the Application pipeline manually and set any of these to true it will execute the template to compile whichever was selected.


What I want to achieve is:

  1. If resources.triggeringalias equals uipr OR parameters.compileui equals true then execute the template compileui.yaml
  2. If resources.triggeringalias equals apipr OR parameters.compileapi equals true then execute the template compileapi.yaml

Here's what I have so far:

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

I know that the (variables['resources.triggeringalias'], 'ui') is only happening during runtime and not on template expansion. That is what I'm missing and don't know how to do it

Upvotes: 1

Views: 1472

Answers (2)

Leo Liu
Leo Liu

Reputation: 76910

YAML pipeline: Execute task if RESOURCES_TRIGGERINGALIAS = “ui”

You could use pipeline resource metadata as predefined variables resources.pipeline.<Alias>.pipelineName and parameters as condition on the job level:

- 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

You could check following references for some more details:

The pipeline resource metadata as predefined variables

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

Upvotes: 1

Krzysztof Madej
Krzysztof Madej

Reputation: 40829

If I got it you want to have part of pipeline run only when pipeline was triggered by another pipeline or when it was run manually with marked parameter. You can achieve this with this:

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"

Upvotes: 0

Related Questions