Answers: <pre><code>steps:
  - powershell: |
      $releaseBranch = &quot;$(ReleaseBranchVariable)&quot;  # Replace with the name of the release branch variable
      $mainBranch = &quot;main&quot;  # Specify the name of the main branch
  
      # Check if the release branch exists in the repository
      if (git branch --list --remote origin/$releaseBranch) {
          $branchToUse = $releaseBranch
      } else {
          Write-Host &quot;Release branch not found, falling back to main branch.&quot;
          $branchToUse = $mainBranch
      }
  
      Write-Host &quot;Using branch: $branchToUse&quot;
  
      # Check out the selected branch
      git checkout $branchToUse
    displayName: 'Select Branch'
  </code></pre>
  <p>This script checks if the release branch exists in the repository. If it does, it uses the release branch; otherwise, it falls back to the main branch.</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-10-27T16:28:12.237Z
Answers: 2024-03-21T16:30:31.130Z
Answers: 2024-03-21T16:30:31.130Z
Answers: geeky_girl
Body: <p>I have a project I need to build in ADO that contains code in two different repos. Usually there is a matching release branch in both, but occasionally not. I would like to get the matching value and automatically fallback to main if there's no matching.</p>
  <p>I was told instead to make the other repo a nuget package, or just compile to a dll, but those both seem like poor solutions when VS2022 now supports multi-repo solutions pretty cleanly.</p>
  
Tags: <azure-devops><azure><azure-pipelines>
Title: In a YAML file how do I checkout a specific branch in different repo and fallback if it doesn't exist using Azure DevOps?
UserDisplayName: Dilbert789
ThreadText: # In a YAML file how do I checkout a specific branch in different repo and fallback if it doesn't exist using Azure DevOps?
  
  (Asked by: username_0 on 2023-10-27)
  
  I have a project I need to build in ADO that contains code in two different repos. Usually there is a matching release branch in both, but occasionally not. I would like to get the matching value and automatically fallback to main if there's no matching.
  
  I was told instead to make the other repo a nuget package, or just compile to a dll, but those both seem like poor solutions when VS2022 now supports multi-repo solutions pretty cleanly.
  
  **Tags:** azure-devops, azure, azure-pipelines
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-10-27. Score: 0
  
  ```
  steps:
  - powershell: |
      $releaseBranch = "$(ReleaseBranchVariable)"  # Replace with the name of the release branch variable
      $mainBranch = "main"  # Specify the name of the main branch
  
      # Check if the release branch exists in the repository
      if (git branch --list --remote origin/$releaseBranch) {
          $branchToUse = $releaseBranch
      } else {
          Write-Host "Release branch not found, falling back to main branch."
          $branchToUse = $mainBranch
      }
  
      Write-Host "Using branch: $branchToUse"
  
      # Check out the selected branch
      git checkout $branchToUse
    displayName: 'Select Branch'
  
  ```
  
  This script checks if the release branch exists in the repository. If it does, it uses the release branch; otherwise, it falls back to the main branch.
  
  
  ---
  
  