Home > other >  Pull only one folder from get repository Git from Azure Pipelines
Pull only one folder from get repository Git from Azure Pipelines

Time:02-11

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.

CodePudding user response:

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://[email protected]/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

  • Related