In Azure DevOps pipeline, you might want to check out a repository using a specific branch or tag, in this blog post – I am going to show how you can use variables to dynamically achieve this!
Why dynamic?
My thought on creating pipelines is that I usually want it to be as dynamic as possible – so many possibilities – rather than static values, I much prefer dynamic- that I can set or change on a specific runtime when select specific values when I want to run the pipeline
The setup
I have two repositories:
https://github.com/thomast1906/dynamic-checkout-repo that has two pipelines:
https://github.com/thomast1906/dynamic-checkout-repo2 is a secondary repo – that I will be referencing in each of these pipelines
Pipeline for branch variable
For each pipeline, I will be using parameter/variable as below for branch
parameters:
- name: branch
displayName: branch
type: string
default: 'testbranch'
variables:
- name: branch_name
value: ${{ parameters.branch }}
Notice the below repositories ref? With $(branch_name)
being used
resources:
repositories:
- repository: dynamic-checkout-repo2
type: github
ref: 'refs/heads/$(branch_name)'
name: thomast1906/dynamic-checkout-repo2
endpoint: 'thomast1906'
To show this working, I am running a simple stage with two checkouts, one for the current repository and the additional repository with specific branch
stages:
- stage: stage1
jobs:
- job: stage1job
steps:
- checkout: self
- checkout: dynamic-checkout-repo2
Reviewing Azure DevOps – we can see it working correctly and repository dynamic-checkout-repo2
using the testbranch
that I defined in parameters of the pipeline

Reviewing the log output, we can see the branch testbranch
has been checked out

Starting: Checkout thomast1906/dynamic-checkout-repo2@testbranch to s/dynamic-checkout-repo2
==============================================================================
Task : Get sources
Description : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.
Version : 1.0.0
Author : Microsoft
Help : [More Information](https://go.microsoft.com/fwlink/?LinkId=798199)
==============================================================================
Syncing repository: thomast1906/dynamic-checkout-repo2 (github)
git version
git version 2.37.1
git lfs version
git-lfs/3.2.0 (GitHub; linux amd64; go 1.18.2)
Pipeline for tag variable
Using same approach, lets look at tag variable
parameters:
- name: tag
displayName: tag
type: string
default: '0.0.1'
variables:
- name: tag_name
value: ${{ parameters.tag }}
resources:
repositories:
- repository: dynamic-checkout-repo2
type: github
ref: 'refs/tags/$(tag_name)'
name: thomast1906/dynamic-checkout-repo2
endpoint: 'thomast1906'
stages:
- stage: stage1
jobs:
- job: stage1job
steps:
- checkout: self
- checkout: dynamic-checkout-repo2
We can see the repository has a release 0.0.1 available

Similar as before, though the checkout this time is the specific tag 0.0.1
and not branch

Awesome, two scenarios shown for both branch & tag specific options that can be used within your Azure DevOps pipelines!