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

Viewed 4389

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?

2 Answers

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'))

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"

Related