itye1970
itye1970

Reputation: 1995

How can i run a stage only if a Pull Request has been raised in YAML

Im learning YAML and want a stage to run only when a PULL Request has been made on a particular branch. How can i do this?

Upvotes: 2

Views: 4058

Answers (1)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35494

To run a stage only when a pull request has been made on a particular branch, you can set the condition in the stage.

condition: and(eq(variables['System.PullRequest.TargetBranch'], 'refs/heads/branchname'), eq(variables['Build.Reason'], 'PullRequest'))

Here is an example:

stages:
- stage: A
  condition: and(eq(variables['System.PullRequest.TargetBranch'], 'refs/heads/branchname'), eq(variables['Build.Reason'], 'PullRequest'))
  jobs:
  - job: A1
    steps:
      - script: xx

You can use predefined variables: System.PullRequest.TargetBranch and Build.Reason to filter the trigger method and pull request target branch.

For more detailed info, you can refer to the docs: Condition and Predefined variables.

Upvotes: 6

Related Questions