72

I have a large repository in Git. How do I create a job in Jenkins that checks out just one sub-folder from the project?

Pavel Chuchuva
  • 22,633
  • 10
  • 99
  • 115
  • 2
    Is it really necessary to add that complexity? Once the repository is cloned the first time, git would just be pulling new objects. Unless your jenkins server was lacking sufficient storage, I'd just clone the whole thing and avoid any additional complexity. – wadesworld May 28 '12 at 23:41
  • 15
    Yes, because in a CI/CD environment, your slaves or Jenkins servers come and go with demand and load...so persistent disk is not there. Reliance on persistence is valued less than repeatability in many DevOps circles – cgseller Jun 07 '18 at 15:48

3 Answers3

77

Jenkins Git Plugin support sparse checkouts since git-plugin 2.1.0 (April, 2014). You will need git >= 1.7.0 for this feature. It is under "Additional Behaviors" -> "Sparse Checkout paths."

screenshot

See: Jira issue JENKINS-21809

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
uı6ʎɹnɯ ꞁəıuɐp
  • 3,431
  • 3
  • 40
  • 49
  • Unfortunately Git would still need to copy whole repository locally. So you may not experience any speed improvements for Git to get the files from server. – luka5z Aug 30 '16 at 14:09
  • 11
    Unfortunately it still checks it out with the original "path structure" intact, so it's not like it actually checks out "one directory" it just happens to populate only one directory, FWIW – rogerdpack Sep 15 '16 at 21:53
  • 1
    Quite and old one, but how do we define a subdirectory names with space . I want to checkout "folder/to/include/directory withSpace" .. – OK999 Mar 07 '17 at 19:07
  • If A is root directory and sparsing to B sub-directory then Sparse checkout path is: `/B/` for B directory. @OK999: Try BackSlash `\ ` before space or any special character in the file or folder name. Example: `/Path/To/My Folder` will be like `/Path/To/My\ Folder` or try passing path between quotes "/Path/To/My Folder". – bh4r4th Jul 18 '18 at 14:32
  • @luka5z This answer needs to be combined with a shallow clone of depth 1, which Jenkins also supports. – Johan Boulé Jul 26 '18 at 16:10
  • When you use shallow clone, the list of 'recent changes' shown on the Jenkins job can become inaccurate and misleading. – Ed Randall Jul 18 '19 at 07:18
29

You can use sparse checkout feature of Git. Note that Git still clones whole repository to local disk. That's not too bad however, because it is compressed.

  1. Create a new job in Jenkins, set Git repository in Source Code Management section.
  2. Build the project. This will clone whole repository to local disk.
  3. Open projects's workspace folder, delete everything there except .git folder.
  4. Open Git shell for project's workspace folder. Enable sparse-checkout:

    git config core.sparsecheckout true
    
  5. Update working tree:

    git read-tree -mu HEAD
    
  6. Create sparse-checkout file in .git/info folder. Add path to sub-folder you want to checkout to that file, like so (note trailing slash):

    folder/to/include/
    
  7. Build the project again. This time only one sub-folder should appear in workspace folder.

Pavel Chuchuva
  • 22,633
  • 10
  • 99
  • 115
  • 1
    @RuudLenders Yes, you need to do it for every job that you want to checkout one subfolder only. – Pavel Chuchuva Jun 25 '13 at 22:30
  • Shouldn't step 5 ´git read-tree -mu HEAD´ follow step 6? As far as I can follow you should first tell Git which files it should look at (step 6) and then read the tree information into the index. – borisdiakur Jan 22 '14 at 12:45
1

You could have a custom step that would just use

git checkout your-branch -- the/desired/path anthother/desired/path

To clear it you could just rm -rf the working folder and recreate it with mkdir workingdir. This would require you to specify this option on the git level of the above command:

git --working-dir="/path/to/workingdir" checkout your-branch -- the/desired/path anthother/desired/path

All this depends on how well you know Jenkins.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141