Search code examples
azureazure-devopsyamlazure-pipelinesazure-pipelines-yaml

Triggering 2nd Azure Pipeline After Successful Run on 1st pipeline


I have two Azure pipelines, A and B. I'm aiming to have pipeline B execute whenever pipeline A completes successfully, but I also want pipeline B to be triggered independently based on its own criteria. My second pipeline, B, is defined as follows:

trigger:
  - master
pr: none

resources:
  pipelines:
    - pipeline: mySourcePipeline
      source: Source Pipeline
      trigger: true

pool:
  vmImage: ubuntu-20.04

steps:
  - checkout: self
    submodules: true
    lfs: true

  - task: NodeTool@0
    inputs:
      versionSpec: '18.x'
    displayName: 'Install Node.js'

  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: echo 'Second pipeline is triggered'

I'm following the guidance provided here: Azure Pipelines Triggers.

However, in my scenario, pipeline A completes successfully, yet the second pipeline (B) doesn't get triggered. What might be causing this issue?

Any insights or suggestions would be greatly appreciated!

What I'm aiming for is: The initial pipeline for setting up Azure infrastructure, including items like storage accounts and resource groups. The second pipeline to handle deploying and publishing content to the newly created storage account. The second pipeline, which will not only be triggered after the first one but also have its independent trigger. This means it can be initiated on its own apart from its connection to the first pipeline.


Solution

  • Assuming that first pipeline looks like

    trigger:
      branches:
        include:
        - staging
        - production
    variables:
    - name: isProduction
      value: $[eq(variables['Build.SourceBranch'], 'refs/heads/production')]
    - name: isStaging
      value: $[eq(variables['Build.SourceBranch'], 'refs/heads/staging')]
    
    stages:
    - stage: A
      jobs:
      - job: A1
        steps:
        - script: |
            echo "isProduction: $(isProduction)"
            echo "isStaging: $(isStaging)"
            echo "Build.SourceBranch $(Build.SourceBranch)"
    

    And it is named firstpipeline (not the yaml file, but the name of the pipeline in Azure Devops portal).

    Then your secondary pipeline should look like

    trigger:
    - staging
    - production
    
    resources:
      pipelines:
      - pipeline: firstpipeline   # Name of the pipeline resource
        source: firstpipeline       # Name of the triggering pipeline
        trigger: 
          branches:
            include:
            - staging
            - production
    variables:
      - name: isProduction
        value: $[eq(variables['Build.SourceBranch'], 'refs/heads/production')]
      - name: isStaging
        value: $[eq(variables['Build.SourceBranch'], 'refs/heads/staging')]
    
    stages:
    - stage: A
      jobs:
      - job: A1
        steps:
        - script: |
            echo "isProduction: $(isProduction)"
            echo "isStaging: $(isStaging)"
            echo "Build.SourceBranch $(Build.SourceBranch)"
    

    So as a source you should use name of the pipeline not the yaml file.

    You can find this here on github