7

I'm trying to make a single build pipeline for 3 env (dev, qa, prod) but with ability to choose which one to build from.

The idea is to keep the pipeline on prod branch or another repo, and not having it in every env. The issue now is that on a PR it will start the pipeline only on master(prod) branch as it shall contain the yml file.

Is there a way to get the PR target branch in order to add additional conditions for PR triggers?

dAn
  • 517
  • 1
  • 6
  • 10
  • 5
    There's a [predefined system variable](https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#system-variables) that holds the name of the target branch: `System.PullRequest.TargetBranch` – Yan Sklyarenko Jul 13 '20 at 11:52

2 Answers2

14

how to get target branch from a pull request using azure devops api or other methods?

Agree with Yan Sklyarenko. Azure devops provides us with some predefined variables, like:

System.PullRequest.IsFork
System.PullRequest.PullRequestId
System.PullRequest.PullRequestNumber
System.PullRequest.SourceBranch
System.PullRequest.SourceRepositoryURI
System.PullRequest.TargetBranch

To get the target branch from a pull request, we could use the predefined variable System.PullRequest.TargetBranch.

So, we could use this predefined variable as condition:

condition: and(succeeded(), eq(variables['System.PullRequest.TargetBranch'], 'refs/heads/master'))
Leo Liu
  • 71,098
  • 10
  • 114
  • 135
3

You can still have one build pipeline. PR triggers are for github/bitbucket repositories. You can create branch policy which triggers your code etc.

The conditions you can have on each step/task: conditions in Azure DevOps for example:

- stage: B
  condition: and(succeeded(), eq(variables['build.sourceBranch'], 'refs/heads/master'))

with that condition this stage will run, only when source branch is named "master"