Home > Back-end >  Azure Devops pipeline - is it possible to checkout to pull requests?
Azure Devops pipeline - is it possible to checkout to pull requests?

Time:12-26

I will like on every PR to test the code before it enter main branch.

I know I can do a build validation on the main branch which will cause a pipeline to run on every PR.

But this requires a separate pipeline for every repo, and I have about 50..

I will like to have one pipeline that will check the code on the PR. I know I can trigger the pipeline that is on a different repo / project, but how do I do a 'checkout' to the PR? I don't want to checkout to the source branch or the target branch, I want to checkout to the code that is on the PR after the merge but before it enter the main branch..

Thank you.

CodePudding user response:

A pull request trigger is indeed associated to one repository.

You can try and test that trigger when specifying multiple repositories (again, for testing)

resources:
  repositories:
  - repository: MyGitHubRepo # The name used to reference this repository in the checkout step
    type: github
    endpoint: MyGitHubServiceConnection
    name: MyGitHubOrgOrUser/MyGitHubRepo
  - repository: MyBitbucketRepo
    type: bitbucket
    endpoint: MyBitbucketServiceConnection
    name: MyBitbucketOrgOrUser/MyBitbucketRepo

trigger:
- ...

That would be a workaround, since it would mean managing the list of repositories manually.

CodePudding user response:

From your requirement, you need to set one pipeline for all repos to check out the Pull Request Branch.

I am afraid that there is no out-of-box method can directly meet your requirement.

For a workaround, you can use the Pull Request Related Pipeline variables and git command to checkout the related pull request code.

When you set the build validation for main branch, the pipeline will be PR Trigger. It will generate the Pipeline variable: $(System.PullRequest.SourceRepositoryURI) and $(BUILD.SOURCEVERSION). They contain the Pull Request Repo URL and Pull Request branch.

For more detailed info, you can refer to the doc: Predefined variable.

Here is an example:

jobs:
  - job: Variables
    displayName: 'Variables'
    steps:
    - checkout: none
 

    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          $PullRequestURL = "$(System.PullRequest.SourceRepositoryURI)"
          $NewPullRequestURL= "$PullRequestURL.replace('AzureTest23@','$(system.accesstoken)@')"
          git remote set-url origin $NewPullRequestURL
          git fetch --force --tags --prune --prune-tags --progress --no-recurse-submodules origin --depth=1  $(BUILD.SOURCEVERSION)

   

In this case, the pipeline will clone the repo based on the Pull Request information.

  • Related