Search code examples
azure-devopsyamldevops

Struggling to specify exact build conditions in Azure Devops


I'm struggling to make Azure devops run builds in exactly the way I need it to. Right now, I have a solution where the second stage is always skipped unless the build was triggered by a schedule. This is mostly fine but is a nightmare in the rare times that we would need to run the second stage manually. Here is how I have achieved that:

- stage: second_stage condition: eq(variables['Build.Reason'], 'Schedule') (I can't work out how to linebreak code here!)

What I want to achieve

  1. Both stages should run when part of a scheduled build
  2. Only the first stage should be run when creating a PR or merging
  3. Only the first stage should be "checked" in the UI when a manual build is run but the user should have the option to check the second stage as well

Please help! Thank you! <3


Solution

  • The following yaml can meet your request.

    stages:
    - stage: first_stage
      jobs:
      - job: A
        steps:
          - script: echo Hello first_stage!
          
    - stage: second_stage
      condition: or(eq(variables['Build.Reason'], 'Schedule'), eq(variables['Build.Reason'], 'Manual'))
      jobs:
      - job: B1
        steps:
          - script: echo Hello second_stage!
    

    Only the first stage should be "checked" in the UI when a manual build is run but the user should have the option to check the second stage as well

    Please manual choose which stage you want to run here when a manual build is required.

    enter image description here

    enter image description here