简体   繁体   中英

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?

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'])

You can definitely customize the checkout scm command to add more flexibility. Check out this link for all of the options - https://jenkins.io/doc/pipeline/steps/workflow-scm-step/

Timeouts:

$class: CheckoutOption timeout:::: Specify a timeout (in minutes) for checkout. This option overrides the default timeout of 10 minutes. You can change the global git timeout via the property org.jenkinsci.plugins.gitclient.Git.timeOut (see JENKINS-11286). Note that property should be set on both master and slave to have effect (see JENKINS-22547). Type: int

SparseCheckoutPaths:

$class: SparseCheckoutPaths Specify the paths that you'd like to sparse checkout. This may be used for saving space (Think about a reference repository). Be sure to use a recent version of Git, at least above 1.7.10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM