Skip to content

Instantly share code, notes, and snippets.

@scorpionlion
Last active March 25, 2024 04:27
Show Gist options
  • Save scorpionlion/1773d08b62ca5875cc2fd6dcdd0394d2 to your computer and use it in GitHub Desktop.
Save scorpionlion/1773d08b62ca5875cc2fd6dcdd0394d2 to your computer and use it in GitHub Desktop.
Checkout Azure devops repos dynamically
pool:
vmImage: ubuntu-latest
steps:
# Checkout on a branch
- template: step-checkout.yaml
parameters:
repoName: test_repo
repoRef: test-branch
# Checkout on a tag
- template: step-checkout.yaml
parameters:
repoName: test_repo
repoRef: test-tag
# Checkout on a commit
- template: step-checkout.yaml
parameters:
repoName: test_repo
repoRef: 4647296b81b53c5c2430d76325d1586752ffb007
parameters:
- name: collectionURL
displayName: (optional) Project collection url, default is current collection/organisation
default: $(System.CollectionUri)
- name: projectName
displayName: (optional) Project name containing repository, default is cuurent project
default: $(System.TeamProject)
- name: repoName
displayName: (required) Name of Repository to be checked out
- name: repoRef
displayName: (required) Reference of the repo to be checked out - can be a tag or branch or commit id and dynamically calculated e.g. $(branchRef)
steps:
- script: |
repoDir=$(Agent.BuildDirectory)/${{ parameters.repoName }}/${{parameters.repoRef}}
echo "##[info] checking out in directory: $repoDir"
/bin/rm -rf $repoDir
url_with_token=$(echo ${{ parameters.collectionURL }} | sed -e "s/https\:\/\//https\:\/\/$(System.AccessToken)\@/g")
git clone $url_with_token/${{ parameters.projectName}}/_git/${{ parameters.repoName }} $repoDir
cd $repoDir
git checkout ${{ parameters.repoRef }}
displayName: Checkout - ${{ parameters.repoName }}@${{ parameters.repoRef }}
@tagelokoy
Copy link

This looks really nice, but when using it without any edits I get this error in the output

`Starting: Checkout - MyRepo@refs/heads/main

Task : Command line
Description : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version : 2.178.0
Author : Microsoft Corporation
Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/command-line

'repoDir' is not recognized as an internal or external command,
operable program or batch file.
The system cannot find the path specified.
'url_with_token' is not recognized as an internal or external command,
operable program or batch file.
Generating script.
========================== Starting Command Output ===========================
"C:\Windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "D:....cmd""
"##[info] checking out in directory: $repoDir"
##[error]Cmd.exe exited with code '255'.
Finishing: Checkout - MyRepo@refs/heads/main
`
Any idea why this happens?

@scorpionlion
Copy link
Author

Ah..this gist uses bash for checking out...so please run it on Ubuntu/Linux agent. I will modify it to use poweshell core later so that it can be executed on both windows and Linux

@padhibibaswan
Copy link

I followed the steps you mentioned , But it is failing with authorization . I can see the bearer token is getting appended and the git clone URL is also getting changed , but still it fails . DO you have any idea how I can solve this .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment