Hello, following the next steps in the previous post ( Jenkins – SSH Keys and GitLab Checkout ), now I want to show how to do a Sparse Checkout using Groovy, in the previous example, it was just a simple checkout, of the entire repository. But in cases where the repository is very large, and you only need a small group of files, Sparse Checkout is the most viable option. https://www.jenkins.io/doc/pipeline/steps/params/scmgit/

The authentication part, ssh key, credentials, etc., is the same process as in the previous post, now I want to show only the snippet of code that performs the sparse checkout. The method was created in a Shared Library: Jenkins – Shared Libraries

The method is simple, it receives as parameters:

  • The full URL of the Git repository, whether ssh or https.
  • The name of the source branch of the folder that will be sparse checked out.
  • The path to the source folder where the files for sparse checkout are located.
  • The ID of the credential created to use Git, see the link in the previous post at the beginning of this post.
  • The checkout destination folder.
  def SparseCheckout(String str_gitUrl, String str_gitBranch, String str_path, String str_gitCredentials, String str_DestinationPath){
    /**
     * Method for doing Sparse Checkout from a Git repository.
     * @param str_gitUrl Full Git repository URL (git@ or https).
     * @param str_gitBranch String - Name of the Branch from which the checkout will be made.
     * @param str_path String - Path of the source folder (git repository) from which the sparse checkout will be performed.
     * @param str_gitCredentials String - ID of the credential (Jenkins) used to connect to Git.
     * @return None.
     * @exception IOException On input error.
     * @see IOException
     */
    obj_Pipeline_Context.checkout([$class: 'GitSCM',
            branches: [[name: "*/${str_gitBranch}"]],
            userRemoteConfigs: [[credentialsId: "${str_gitCredentials}",url: "${str_gitUrl}"]],
            doGenerateSubmoduleConfigurations: false,
            extensions: [
                    [$class: 'CloneOption', noTags: false,
                     shallow: false],
                    [$class: 'CheckoutOption', timeout: 180],
                    [$class: 'CleanCheckout'],
                    [$class: 'SubmoduleOption', disableSubmodules: false,
                     parentCredentials: true,
                     recursiveSubmodules: true,
                     reference: '',
                     trackingSubmodules: false],
                    [$class: 'RelativeTargetDirectory', relativeTargetDir: "${str_DestinationPath}"],
                    [$class: 'SparseCheckoutPaths', sparseCheckoutPaths: [[path: "${str_path}/*"]]]
            ]
    ])
  }

Info

For this method, I needed to make a small change to the Shared Library class, to use the checkout method, it was necessary to have the “context” of the pipeline loaded into a variable, so to facilitate the other methods that were created, passing the parameter context is in the class constructor. See the details in Git: https://github.com/faustobranco/devops-db/tree/master/infrastructure/pipelines/lib-utilities

The call is also simple, as in the example of the pipeline below, which in the same way as the previous one, I leave a few minutes of sleep so that we can validate the files and structures created in the container.

@Library('devopsdb-global-lib') _

import devopsdb.utilities.Utilities
def obj_Utilities = new Utilities(this)

pipeline {
    agent {
        docker {
            image 'registry.devops-db.internal:5000/ubuntu_python:3.10.6'
            label 'docker'
            args '-u root'
        }
    }
    options { timestamps ()
        skipDefaultCheckout(true)
    }
    stages {
        stage('Source') {
            steps {
                script {
                    def str_folder = "${env.WORKSPACE}/NewFolder/Level1/Level2"
                    println(str_folder)
                    obj_Utilities.CreateFolders(str_folder)
                    obj_Utilities.SparseCheckout('git@gitlab.devops-db.internal:infrastructure/pipelines/lib-utilities.git',
                            'master',
                            '/src/devopsdb/utilities',
                            'usr-service-jenkins',
                            str_folder)
                }
            }
        }
        stage('Validation') {
            steps {
                sh 'ls -lahR ${WORKSPACE}'
            }
        }
        stage('Build') {
            steps {
                sh 'sleep 10m'
            }
        }
        stage('Cleanup') {
            steps {
                cleanWs deleteDirs: true, disableDeferredWipeout: true
            }
        }
    }
}

Finally, this is the part of the pipeline result that matters.

21:02:07  
21:02:07  /var/jenkins_home/workspace/test_pipeline@2/NewFolder/Level1/Level2/src/devopsdb/utilities:
21:02:07  total 12K
21:02:07  drwxr-xr-x 1 502 dialout   96 May 13 20:02 .
21:02:07  drwxr-xr-x 1 502 dialout   96 May 13 20:02 ..
21:02:07  -rw-r--r-- 1 502 dialout 2.5K May 13 20:02 Utilities.groovy
21:02:07  

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.