Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I augment scm in Jenkinsfile?

It's taken me ages to understand what checkout scm really means in Jenkinsfile (checkout is a function and scm is a default global variable by the way).

Now that I've understood it, I want to augment scm for example to increase the timeout for a particular checkout or to set sparseCheckoutPaths. Is this possible? If so, how?

like image 356
mjaggard Avatar asked Sep 01 '17 16:09

mjaggard


People also ask

What is SCM Jenkinsfile?

In Jenkins, SCM stands for "Source Code Management". This option instructs Jenkins to obtain your Pipeline from Source Control Management (SCM), which will be your locally cloned Git repository.

What does checkout SCM do in Jenkinsfile?

The checkout step will checkout code from source control; scm is a special variable which instructs the checkout step to clone the specific revision which triggered this Pipeline run.

What is SCM change in Jenkins?

It means that someone checked in code changes to your version control system / software configuration management (CVS, SVN, Git, etc), and Hudson started a built based on that change.


1 Answers

For Git, checkout scm is basically equivalent to :

checkout([
     $class: 'GitSCM',
     branches: scm.branches,
     doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
     extensions: scm.extensions,
     userRemoteConfigs: scm.userRemoteConfigs
])

If you want to add sparse checkout to the existing scm, what you would do is something like:

checkout([
     $class: 'GitSCM',
     branches: scm.branches,
     doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
     extensions: scm.extensions + [$class: 'SparseCheckoutPaths',  sparseCheckoutPaths:[[$class:'SparseCheckoutPath', path:'path/to/file.xml']]],
     userRemoteConfigs: scm.userRemoteConfigs
])

Even better, you can define a custom step, sparseCheckout in a shared library.

def call(scm, files) {
    if (scm.class.simpleName == 'GitSCM') {
        def filesAsPaths = files.collect {
            [path: it]
        }

        return checkout([$class                           : 'GitSCM',
                         branches                         : scm.branches,
                         doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
                         extensions                       : scm.extensions +
                                 [[$class: 'SparseCheckoutPaths', sparseCheckoutPaths: filesAsPaths]],
                         submoduleCfg                     : scm.submoduleCfg,
                         userRemoteConfigs                : scm.userRemoteConfigs
        ])
    } else {
        // fallback to checkout everything by default
        return checkout(scm)
    }
}

Then you call it with:

sparseCheckout(scm, ['path/to/file.xml'])
like image 90
Chadi Avatar answered Nov 07 '22 16:11

Chadi