How can I create Build Validations on Azure DevOps branch policies for forks?

113 Views Asked by At

I have 2 repos on Azure Devops, the second one was created as a fork of the first one. My team wants to create PRs from a branch from the original repo to the main branch on the fork, or using a source and target branch on the fork.

How can I create a build validation on my branch policies to build the source branch from the PR, depending on if it's from the original repo or from the fork? Having a single build validation won't work, because the build pipeline targets just one of the repos.

Any help will be appreciated.

Thanks!

2

There are 2 best solutions below

0
On

From your description,

How can I create a build validation on my branch policies to build the source branch from the PR, depending on if it's from the original repo or from the fork?

If you want to build source branch from PR on two repos, you can create two build validations, one is for original repo and another is for fork repo. Then check out multiple repositories in your yaml pipeline.

You can follow steps below:

1.Add two Build Validations.(one is for original repo and another is for fork repo) enter image description here

2 Check out multiple repositories.

resources:
  repositories:
  - repository: seocond
    type: git
    name: "your second repo name"

steps:
- checkout: self
- checkout: seocond

Test Result: enter image description here

When a new PR from the original repo or from the fork repo is created or changes are pushed to an existing PR that targets the branch, the build will be triggered.

If there is any misunderstanding, please provide more information related to your issue.

0
On

How can I create a build validation on my branch policies to build the source branch from the PR, depending on if it's from the original repo or from the fork?

You can check the predefined variable value for $(System.PullRequest.IsFork) , $(System.PullRequest.SourceRepositoryURI) and $(System.PullRequest.SourceBranch) in your build validation pipeline, to determine build which source branch. You can specify condition to run the tasks.

If PR is created from original repo to fork, or from fork to original, the System.PullRequest.IsFork value will be true. and System.PullRequest.SourceRepositoryURI will show the source repo url.System.PullRequest.SourceBranch will show the source branch without repo info.

enter image description here enter image description here

For example, i have original repo test1 with branches main and dev, and created a fork repo. I created the PR from orignal repo dev1 to folk repo main branch:

enter image description here

I created a new PR from forked repo dev1 to forked main branch: enter image description here

My build validation sample below, add condition for the task, so that it will be executed when a pr is created from original repo dev1 to forked repo main branch.

trigger: none

pool:
  vmImage: ubuntu-latest

steps:
- checkout: none  # add checkout none as i don't need to build on the merge branch.

- powershell: |
    echo "System.PullRequest.IsFork is: $(System.PullRequest.IsFork)"
    echo "System.PullRequest.PullRequestId is: $(System.PullRequest.PullRequestId)"
    echo "System.PullRequest.targetBranchName is: $(System.PullRequest.targetBranchName)"
    echo "System.PullRequest.SourceBranch is: $(System.PullRequest.SourceBranch)"
    echo "System.PullRequest.SourceRepositoryURI is: $(System.PullRequest.SourceRepositoryURI)"
    echo "System.PullRequest.TargetBranch is: $(System.PullRequest.TargetBranch)"
  continueOnError: true

- powershell: |
    echo "This is a sample task when i create from orginal repo dev1 branch to forked repo main branch"
  condition: and(eq(variables['System.PullRequest.IsFork'], 'true'), eq(variables['System.PullRequest.SourceBranch'], 'refs/heads/dev1'), endsWith(variables['System.PullRequest.SourceRepositoryURI'], 'test1'))

enter image description here