Checkout Part Of A Branch In Azure DevOps Pipelines (GetSources)


Answer :

In Azure DevOps you don't have option to get only part of the repository, but there is a workaround: Disable the "Get sources" step and get only the source you want by manually executing the according git commands in a script.

To disable the default "Get Sources" just specify none in the checkout statement:

- checkout: none 

In the pipeline add a CMD/PowerShell task to get the sources manually with one of the following 2 options:

1. Get only part of the repo with git sparse-checkout. For example, get only the directories src_1 and src_2 within the test folder (lines starting with REM ### are just the usual batch comments):

- script: |     REM ### this will create a 'root' directory for your repo and cd into it     mkdir myRepo     cd myRepo     REM ### initialize Git in the current directory     git init     REM ### set Git sparsecheckout to TRUE     git config core.sparsecheckout true     REM ### write the directories that you want to pull to the .git/info/sparse-checkout file (without the root directory)     REM ### you can add multiple directories with multiple lines     echo test/src_1/ >> .git/info/sparse-checkout     echo test/src_2/ >> .git/info/sparse-checkout     REM ### fetch the remote repo using your access token     git remote add -f origin https://your.access.token@path.to.your/repo     REM ### pull the files from the source branch of this build, using the build-in Azure DevOps variable for the branch name     git pull origin $(Build.SourceBranch)     displayName: 'Get only test/src_1 & test/src_2 directories' 

Now in the builds task make myRepo the working directory. Fetching the remote repo using an access token is necessary, since using checkout: none will prevent your login credentials from being used. In the end of the pipeline you may want to add step to clean the myRepo directory.

2. Get parts of the repo with Azure DevOps Rest API (Git - Items - Get Items Batch).


The other answers work well but I found a different way using potentially newer features of git.

This will fetch to a depth of 1 and show all the files in the root folder plus folder1, folder2 and folder3

        - task: CmdLine@2           inputs:             script: |               git init               git sparse-checkout init --cone               git sparse-checkout set folder1 folder2 folder3               git remote add origin https://<github-username>:%GITHUB_TOKEN%@<your-git-repo>               git fetch --progress --verbose --depth=1 origin               git switch develop           env:             GITHUB_TOKEN: $(GITHUB_TOKEN) 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?