Jenkins sparse checkout

Jenkins sparse checkout | How to Pull Git subdirectory in 2022

Jenkins is mainly used for CI/CD operations where our code is present in any central repository, and we use Jenkins to pull the SCM. But most of the time, we don’t want Jenkins to pull the entire code; instead, we want to pull the code from a particular git folder. This blog will learn how to perform Jenkins sparse checkout.

Jenkins sparse checkout

Checkout from Jenkins means to pull the code from the version control system like git into the Jenkins local disk. Jenkins supports checkout from various SCM systems like Github, GitLab, etc. You can read the below block on how to perform SCM checkout from the Jenkins

Jenkins pipeline github

In cases where the github repo is too big, the Jenkins will take a lot of time to pull the entire code into the local repository. The other disadvantage of pulling the entire code from Jenkins is the unnecessary memory consumption.

Jenkins provides a functionality called sparsed checkout using which a user can pull a specific directory from a git repo instead of pulling the entire repo.

Jenkins Git checkout subdirectory

In this session, we will create a Jenkins job to perform a sparse checkout from the GitHub repo. To create a new job in Jenkins click on new item > freestyle project

enkins Git checkout subdirectory

Provide the job name as sparsed checkout and click on the ok button. select the Source code Management as Git

enkins Git checkout subdirectory

Provide the repository URL and branch name and click on additional behavior

enkins Git checkout subdirectory

Select sparse checkout paths

Jenkins sparse checkout

Now select the git path which you want Jenkins to be pulled into the local file system.

Jenkins sparse checkout

save the Jenkins job and click on the build now button. You can verify if the Jenkins job is working fine by going to the workspace and by checking the folder in which the Jenkins job has been pulled for this job.

Jenkins pipeline sparse checkout

So far we have seen how to perform the sparsed checkout using the Jenkins UI. In this session, we will understand how to create a Jenkins job to perform the sparsed checkout using the Jenkins pipeline. You can check my below blogs to get a basic understanding of how a Jenkins pipeline works.

Jenkins pipeline syntax

Jenkins scripted pipeline.

To create a new pipeline Goto the Jenkins home page and click on New Item > Pipeline. Provide a pipeline name and click on the OK button.

To use the sparse checkout functionality, you need to install the github plugin. Now Goto the script session and paste the below code

Jenkins sparse checkout pipeline
pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
              checkout([$class: 'GitSCM', 
                branches: [[name: '*/main']],
                extensions: [
                    [$class: 'SparseCheckoutPaths', 
                    sparseCheckoutPaths:[[$class:'SparseCheckoutPath', path:'/jenkins/script']]]
                userRemoteConfigs: [[url: '']]])
              sh "ls -ltr"

Pipeline Explanation

  1. The Jenkins Declarative pipeline always starts with the pipeline block.
  2. In the second step, we select an agent as any.
  3. In the stages block, we define a checkout stage.
  4. In the checkout step, we have defined the branch name and git URL.
    • Using the extensions property we have defined the sparsed checkout property.
    • In the sparseCheckoutPaths property, we have mentioned the git path which needs to be copied to Jenkins local directory.
  5. Finally, we are listing all files and directories using the “ls -ltr” shell command.

save the pipeline and click on the build now button. On a successful run, you will get the below console output.

Jenkins sparse checkout logs


I hope you have liked this blog. In this blog, we have learned what is sparsed checkout in Jenkins and why we need to implement sparsed checkout while pulling the code from the github. We have also seen the example of the Jenkins job where we have implemented the sparsed checkout.

Finally, we have learned how to write a Jenkins sparsed checkout pipeline. Please do let me know if you are facing any issues while following along.

1 thought on “Jenkins sparse checkout | How to Pull Git subdirectory in 2022”

Leave a Comment

Your email address will not be published.

Scroll to Top