Search code examples
azureazure-devopstriggersazure-pipelinespipeline

Azure devops resource trigger runs pipeline multiple times


I have configured a pipeline, in Azure DevOps, that is responsible for triggering runs of other pipelines

name: Trigger

trigger:
  batch: true
  branches:
    include:
      - master

parameters:
  - name: runA
    displayName: Run A
    type: boolean
    default: true
  - name: runB
    displayName: Run B
    type: boolean
    default: true
  - name: runC
    displayName: Run C
    type: boolean
    default: true

stages:
  - stage: RunA
    dependsOn: []
    condition: ${{ and(eq(parameters.runA, true) }}
    jobs:
      - job:
        steps:
          - script: echo running A
  - stage: RunB
    dependsOn: []
    condition: ${{ and(eq(parameters.runB, true) }}
    jobs:
      - job:
        steps:
          - script: echo running B
  - stage: RunC
    dependsOn: []
    condition: ${{ and(eq(parameters.runC, true) }}
    jobs:
      - job:
        steps:
          - script: echo running C

And then I have other 3 pipelines that are triggered by (Trigger) resource pipeline when particular stage complete:

name: A

resources:
  pipelines:
    - pipeline: parent-pipeline
      source: Trigger-Pipeline
      trigger:
        stages:
          - RunA

trigger: none

stages:
  ....
name: B

resources:
  pipelines:
    - pipeline: parent-pipeline
      source: Trigger-Pipeline
      trigger:
        stages:
          - RunB

trigger: none

stages:
  ....
name: C

resources:
  pipelines:
    - pipeline: parent-pipeline
      source: Trigger-Pipeline
      trigger:
        stages:
          - RunC

trigger: none

stages:
  ....

When I run Trigger pipeline with all parameters set to true, pipelines A, B and C run multiple times. For example A is triggered 3 times, B 2 times, and C once. I want A, B and C get triggered just once. Am I doing something wrong? Did I miss something in the azure documentation?

I have tried recreating pipelines A, B and C, because I had them before configuring resource triggers.


Solution

  • I can reproduce the same with your yaml.

    This is because when every stage completes, it will check all stages but not only current stage in pipeline, to trigger the target pipeline by the pipeline resource trigger.

    You have dependsOn: [] on each stage on Trigger-Pipeline, so every stages are individual, every stage could run first. For example, sequence: run A -> Run B -> Run C:

    1. when stage Run A complete, it will trigger Pipeline A.
    2. when stage Run B complete, it will check again, and find stage RunA, Run B complete, it will trigger Pipeline B, and again Pipeline A.
    3. when stage Run C complete, it will find stage RunA, RunB, RunC complete, trigger Pipeline C, again Pipeline B, again Pipeline A

    As an alternative, to trigger the Pipeline A, B, C only once, you could consider below options:

    1. split source Trigger-Pipeline into 3 pipelines for each stage, in Pipeline A, B, C, monitor on different source pipelines in this case.

    2. or remove pipeline resource trigger, in each stage you could use rest api Runs - Run Pipeline(sample) or task trigger to trigger target pipeline. You can put the task at the end of the stage with always() condition if needed.

    I understand your concern that the stage parameter in pipeline resource trigger could not work as expected, if you would like the feature, it's recommended to raise a feature request in community link, thanks.