3

I'm looking for a solution to dynamically select a branch to build in the Azure pipeline. I have Azure Repos Git where I select project, repository and default branch. I would like to be able to select branch based on a variable.

What I'm trying now is to use the Command Line task and fire a git checkout command (e.g. branch is a variable):

git checkout $(branch)

I can't confirm it working yet but still I confirm it works but I feel that there is a better option than checking out default branch and then switching branch with the command line.

Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79

1 Answers1

2

Update:

If you want to have single pipeline that can build different branches (version branches) for different branches, you could just specify them in the trigger of branch filters. This will not build all branches.

The branch you select in build definition is just the default branch used when you Queue New Build manually. OTOH the build trigger establish which branch to download, e.g. if the build has been triggered by a git push on branch develop then that is the one checkout'ed and built. Take a look at this link: Get the sources from the branch that triggered the build in Team Services

Besides, you could disable the default get source step.Then use you own powershell script and git command to get source code manually(just what you want) and check out branch, finally build based on your variable.


Assuming you're choosing the default branch. That doesn't mean that it's the only branch that can be built by that build definition.

You can choose which branches to monitor for CI (on the Triggers tab, you can add multiple branch filters to represent the branches you wish to build on commit), and the appropriate branch will be checked out when the build is triggered. You can also choose an alternate branch when manually queuing the build.

Source Link: Get Sources from multiple branches

If you want to dynamically select default branch as below, this is not available at present.

enter image description here

This is the branch that you want to be the default when you manually queue this build. If you set a scheduled trigger for the build, this is the branch from which your build will get the latest sources.

The default branch has no bearing when the build is triggered through continuous integration (CI). Usually you'll set this to be the same as the default branch of the repository (for example, "master").

There is a related user voice here: When triggering a build, use the same branch as the trigger build. You could kindly vote up and track the process.

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62
  • I want to have single pipeline that can build different branches (version branches), not all of them. We can start build with a variable set, so I thought like with TFVC we could pass that variable to select the source. – Lukasz 'Severiaan' Grela Oct 03 '19 at 08:27
  • @Lukasz'Severiaan'Grela You could, for different branches, you just specify them in the trigger. The branch you select in build definition is just the default branch used when you Queue New Build manually. OTOH the build trigger establish which branch to download, e.g. if the build has been triggered by a git push on branch develop then that is the one checkout'ed and built. Take a look at this link https://stackoverflow.com/questions/44133816/get-the-sources-from-the-branch-that-triggered-the-build-in-team-services – PatrickLu-MSFT Oct 03 '19 at 08:32
  • @Lukasz'Severiaan'Grela If you insist on using variable solution, you could disable the default get source step in Azure DevOPs pipeline https://stackoverflow.com/questions/46421482/is-it-able-to-ignore-disable-the-first-step-get-source-in-vnext-build/46422981#46422981 Then use you own powershell script and git command to get source code and check out branch to build based on your variable. – PatrickLu-MSFT Oct 03 '19 at 08:35
  • Include these instructions in the answer please, I'm happy to consider this as an answer, thanks. – Lukasz 'Severiaan' Grela Oct 03 '19 at 08:37
  • @Lukasz'Severiaan'Grela Have done, thanks for your kindly response. – PatrickLu-MSFT Oct 03 '19 at 08:51