I was recently working on a project where I required only a particular folder inside a bitbucket repository to be cloned and tracked on the server. This was when I stumbled upon the sparse checkout feature that Jenkins offered. I decided to write this short post so that it might help you in case you have a similar requirement as me.
I have the following folder structure:
Here,I would like to clone and track only the survey folder highlighted above.
Here is how you can go about doing this,
1) Start off by creating a new freestyle project
2) Under the configure page, navigate to source code management section.
3) Here, click on the git option. Enter the repository URL followed by the credentials and the branch that you would like to build.
4) Now, besides the additional behaviour tab click on the Add dropdown menu, and select Sparse Checkout path option.
5) A textbox will be displayed. Here, enter the path to the folder that you would like to clone.
Go ahead and save your configuration
6) Click on Build Now.
Only the folder which was specified would be cloned and tracked on the server.
And that’s it, you successfully did a sparse checkout using Jenkins!
Feel free to ask any further queries you have.
I am a QA Engineer. I completed my engineering in computer science and joined Qxf2 as an intern QA. During my internship, I got a thorough insight into software testing methodologies and practices. I later joined Qxf2 as a full-time employee after my internship period. I love automation testing and seek to learn more with new experiences and challenges. My hobbies are reading books, listening to music, playing chess and soccer.
Fantastic and thanks for sharing. Do you know if there is a way to exclude certain directory from checkout?
Hi, thanks for your comment. If you’d like to exclude a certain directory from within the sparse checkout directory, you can ssh into the Jenkins server(where your repository is getting cloned). Navigate to the cloned repo, then `vi .git/info/sparse-checkout` . Here in this file, you can add the directory that you would like to exclude.
Eg:
survey/*
!survey/survey_db/*
Now in the above example, all the files and subdirectories under the “survey” folder will be cloned except the “survey_db” folder
Or if that doesn’t work, you could try adding the directory to exclude in .git/info/exclude
If that doesn’t help either you could use the following command to not keep track of the folder in future checkouts,
git update-index –assume-unchanged folder_name/*
Thanks Akkul for sharing this valuable details that saved me a lot of time.
I configured the Sparse Checkout using Jenkins. I want to copy a different folder everytime as the folder name is created with everyday date and I want to copy the current date folder only everyday
Thanks in advance
Thanks Gobinath, I’m not quite sure, but in-order to dynamically select the folder you could use the Jenkins pipeline project instead of the freestyle project. You can add the sparse checkout code to the pipeline script. Something similar to this
checkout([$class: 'GitSCM',
branches: [[name: '*/branchName']],
doGenerateSubmoduleConfigurations: false,
extensions: [
[$class: 'SparseCheckoutPaths', sparseCheckoutPaths:[[$class:'SparseCheckoutPath', path:'folderName/']]]
],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'someID',
url: '[email protected]']]])
You could use another step to fetch the current date within the script and use that date in a regular expression with the
path
argumenti am getting an error
stderr: error: Sparse checkout leaves no entry on working directory
Finished: FAILURE
i have given
Branch Specifier – */main
Local subdirectory for repo
/tools/testingenvcheckout/devperftest/
Sparse Checkout paths
?
Path – /foldet/folder/folder to checkout which has my .jmx file
Can you please check if the sparse checkout and command result is in an empty directory? You can also check the content of the sparse checkout file to make sure that it contains the path of the file. If the file is empty, you can add the necessary path to it.