Manual Trigger In Multi-Stage YAML Pipeline - Azure DevOps

YAML pipelines in Azure Pipelines is a great functionality, however, at the time of this writing, it lacks some features. One of those is a manual trigger for a stage. Consider this sample use case:

  1. PR is merged into the main branch.
  2. Main pipeline is automatically invoked, YAML definition contains DEV, TEST, PROD stages to deploy to corresponding environments.
  3. We want to deploy to DEV after each commit to master, however, not to TEST, there could be different reasons why we do not want to deploy to TEST right away after each commit.

Solution

How to prevent the pipeline from executing TEST stage but be able to trigger it manually?

As a workaround for not running stage automatically, a condition can be added to execute the stage only if the whole pipeline was triggered manually: condition: eq(variables['Build.Reason'], 'Manual')

- stage: TEST
  condition: eq(variables['Build.Reason'], 'Manual')
  jobs: Deploy
    steps:
    - script: echo Hello, World!
      displayName: 'Run a one-line script'

This condition should be added to the TEST stage. Of course, this check can be combined with other checks to form a larger condition. More about Build.Reason and other predefined variables here.

Result

When new commit is added to the main branch, pipeline is kicked off automatically but stops after DEV stage:

Pipeline stopped before TEST stage Pipeline stopped before TEST stage

By triggering run manually TEST stage will be executed if selected in “Run pipeline” → “Stages to run”:

Fully run pipeline Fully run pipeline

Note: To deploy to the TEST stage we need to create a new run manually. We cannot trigger TEST stage from an existing run.

Alternative Solutions