azure-devopsazure-pipelinesbuild-triggers

Azure devops pipeline - trigger only on another pipeline, NOT commit


Requirement

So, there is some newish functionality in Azure DevOps which allows pipelines to trigger other pipelines and is documented here: https://learn.microsoft.com/en-us/azure/devops/pipelines/build/triggers?view=azure-devops&tabs=yaml#pipeline-triggers-1 It sounds great, apart from the fact I can't get the behaviour I need. I want 2 pipelines in the same repository:

Pipeline A Syntax

resources:
    pipelines:
    - pipeline: database
      source: database
      trigger:
        branches:
        - develop
        - release/*
        # The stages filter should work, according to: https://learn.microsoft.com/en-us/azure/devops/pipelines/build/triggers?view=azure-devops&tabs=yaml
        # However, this error occurs when specifying: /azure-pipelines.yml (Line: 8, Col: 15): Stage filters in pipeline resource database is not supported.
        #stages:
        #- Build
    - pipeline: auth
      source: auth
      trigger:
        branches:
        - develop
        - release/*
    - pipeline: api
      source: api
      trigger:
        branches:
        - develop
        - release/*
    - pipeline: web
      source: web
      trigger:
        branches:
        - develop
        - release/*
  ... multiple triggers - 9 in total
stages:
  ...

Current Behaviour

Pipeline A is not triggered by any of the other pipelines, but only on changes to its own repo. Since it makes changes to its own repo anyway, it triggers itself in an endless loop.

Questions / Comments

Discovery


Solution

  • Working Solution

    Because all of my builds are centralised in one pipeline template, I changed this template to trigger my pipeline A on successful publishing of an artifact. Here's the pipeline trigger code which is pretty much verbatim from (https://learn.microsoft.com/en-us/azure/devops/cli/azure-devops-cli-in-yaml?view=azure-devops), apart from the last couple of steps:

    # Updating the python version available on the linux agent
        - task: UsePythonVersion@0
          displayName: Upgrade build agent Python version
          inputs:
            versionSpec: '3.x'
            architecture: 'x64'
    
        # Updating pip to latest
        - script: python -m pip install --upgrade pip
          displayName: 'Upgrade pip'
    
        # Updating to latest Azure CLI version.
        - script: pip install --pre azure-cli --extra-index-url https://azurecliprod.blob.core.windows.net/edge
          displayName: 'Upgrade azure cli'
    
        - script: az --version
          displayName: 'Show Azure CLI version'
    
        - script: az extension add -n azure-devops
          displayName: 'Install Azure DevOps Extension'
    
        - script: echo ${AZURE_DEVOPS_CLI_PAT} | az devops login
          env:
            AZURE_DEVOPS_CLI_PAT: $(System.AccessToken)
          displayName: 'Login Azure DevOps Extension'
    
        - script: az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project="$(System.TeamProject)" --use-git-aliases true
          displayName: 'Set default Azure DevOps organization and project'
    
        - script: |
            set -euo pipefail
            if [[ "$(Build.SourceBranch)" == *"/release/"* ]]; then
              branchName="master"
            else
              branchName="develop"
            fi
            commandLine="--branch $branchName --name <YourPipelineName>"
            echo "Triggering release creation with: az pipelines run $commandLine"
            az pipelines run $commandLine
          displayName: Trigger release build for internal (develop) and external (release) builds
    

    Caveats