8

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?

mjaggard
  • 2,389
  • 1
  • 23
  • 45
  • https://stackoverflow.com/questions/43293334/sparsecheckout-in-jenkinsfile-pipeline/43293547 just found a similar question with an answer – minas Sep 01 '17 at 17:02
  • Yes I'd seen that question @minas but it doesn't answer my question. – mjaggard Sep 04 '17 at 07:45

2 Answers2

8

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'])
Chadi
  • 737
  • 1
  • 6
  • 21
0

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

  • But how do I augment the existing `scm` object with those values? – mjaggard Oct 16 '17 at 10:38
  • @mjaggard - do you mind posting your current jenkinsfile? it'll be easier to understand/explain. – Ilya Chernomorin Feb 16 '18 at 16:17
  • I have a line that reads `checkout scm` where `scm` is a particular repo, branch, etc. but I don't know what repo at time of writing the Jenkinsfile. I want to use sparse checkout paths. – mjaggard Feb 20 '18 at 16:48
  • @mjaggard - I believe this should comment should have the information you're looking for: https://stackoverflow.com/a/43340315/5107771 – Ilya Chernomorin Feb 21 '18 at 19:54
  • It doesn't list how to change an existing variable (`scm`) with the values. – mjaggard Mar 07 '18 at 20:24
  • @mjaggard try replacing `checkout scm` with the snippet in the link I gave you in the previous comment (of course insert the correct values for your case). – Ilya Chernomorin Mar 08 '18 at 11:56