2

I'm working on Git. My Environment is Azure DevOps. I have a vsts build agent. I'm trying to pull only one folder from my repo on to the build agent. I'm using powershell. What I have tried is :

git init
git remote add origin https://MYUSERNAME:MYPERSONALACCESSTOKEN@mytfs-git-project-url
git config core.sparseCheckout true
Set-Content .git\info\space-checkout "myfolder/*" -Encoding Ascii
git pull origin master

I'm getting Authentication failed for username error. I can clone it locally using "git clone https://MYPERSONALACCESSTOKEN@tfs-git-url", But if I try the same on Azure DevOps Powershell it gives the error as cannot read username error. I did try out the stackoverflow suggestions but it did not work. Any new suggestions are appreciated.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Abhinandan RK
  • 47
  • 1
  • 10

1 Answers1

2

I made a few changes to your script, it's working on my environment when downloading the sources from the same Azure DevOps organization. The changes are as follows:

  • Instead of relying on manually setting the sparse config, I use the sparse-checkout set command, it will automatically set other required flags and configs.
  • Instead of relying on (insecurely) setting the security token in the clone URL, instead I pass the variable in as an extraheader.
  • Instead of relying on a PAT (which is fine locally), I instead pick up the access token from the build/release job.

This results in the following task:

steps:
- task: PowerShell@2
  displayName: 'Sparse Checkout'
  inputs:
    script: |
     git init
     git remote add origin https://org@dev.azure.com/org/project/_git/repo
     git config core.sparseCheckout true
     git sparse-checkout set "Folder/*"
     git -c http.extraheader="AUTHORIZATION: Bearer $env:System.AccessToken" pull origin master
    workingDirectory: '$(System.ArtifactsDirectory)'
  env:
    System.AccessToken: $(System.AccessToken)

Setting these inputs in the UI works as well:

enter image description here

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • I tried the above script and it's giving the error as could not read user name for https://tfs.organization.com/git/reponame – Abhinandan RK Feb 10 '22 at 14:16
  • Have you configured your Azure DevOps instance to limit the job token to only referenced repositories? In which case you'll need to make sure the release is allowed to access the repo. https://learn.microsoft.com/en-us/azure/devops/pipelines/process/access-tokens?WT.mc_id=DOP-MVP-5001511&view=azure-devops&tabs=yaml – jessehouwing Feb 10 '22 at 14:26
  • I'll explain. My dotnet code is in Tfs-git. I run the CI pipeline and build my project. Now i want to run the release job, in my release job i want to pull one of my folder in Tfs-Git repo. So now im running the above script and I'm getting the error at git remote add origin "https://tfs.orgname.com/Project/_git/reponame". – Abhinandan RK Feb 10 '22 at 14:29
  • Yes it's a git repository in Azure DevOps Server. yes im using https: // in clone url. – Abhinandan RK Feb 10 '22 at 15:06
  • And yeah my organization url starts as https : //tfs.organization.com/CollectionName/Project. – Abhinandan RK Feb 10 '22 at 15:07
  • So the Git repo which is on the Azure DevOps Server has the clone link https : // tfs.organizationName.com/CollectionName/ProjectName/_git/reponame – Abhinandan RK Feb 10 '22 at 15:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241908/discussion-between-abhinandan-rk-and-jessehouwing). – Abhinandan RK Feb 10 '22 at 15:13
  • 1
    Thanks @jessehouwing your script worked. I just had to make a small change. Means a lot. Thanks for your guidance. – Abhinandan RK Feb 10 '22 at 17:11
  • What was the change you made? Can you update the answer above by suggesting a change or leave a comment? – jessehouwing Feb 10 '22 at 17:37
  • I just changed the System.Access token approach. You provided $env:System.AccessToken and I used $(System.AccessToken). Basically they are same, but the second one worked. – Abhinandan RK Feb 10 '22 at 19:27