Jenkins sparse checkout

Jenkins sparse checkout | How to Pull Git subdirectory in 2023

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 pulling 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, 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: 'https://github.com/naiveskill/devops.git']]])
              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’s 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

Jenkins sparse checkout Configure the github path and branch

By default, Jenkins will automatically parse the Jenkinsfile in the root of the repository, but it is possible to specify a different location for the Jenkinsfile using the scriptPath option in the checkout step.

Below is the sample code snipped where the user can customize the github branch and script path as per his need.

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/my-repo.git']]], scriptPath: 'path/to/Jenkinsfile')
           }
        }
    }
}

It is important to note that this is just an example and you need to modify the repository URL, branch, and scriptPath as per your needs.

Conclusion

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 2023”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top