Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-33022

Allow Git shallow clone in Multibranch pipeline

    Details

    • Similar Issues:

      Description

      Cloning full Git repository can take a long time (in my case over 10 minutes which results in timeout).

      In the freestyle project I can specify additional clone options like shallow clone. Unfortunatelny I cannot do this in Multibranch Pipeline project with Jenkinsfile.

      I've tried modifying scm.extensions using code like this:

      @NonCPS
      def checkout_src() {
        def copt = new CloneOption(true, "", 999)
      
        def r = scm.extensions.add(copt)
      
        echo "${scm.extensions}"
      
        checkout scm
      
        scm.extensions.remove(copt)
      }
      

      However when executed CloneOption is not added to extensions:

      Extensions: [hudson.plugins.git.extensions.impl.BuildChooserSetting@87d105]
      

      It would be nice if it was possible to add extensions to SCM via Jenkinsfile.

        Attachments

          Issue Links

            Activity

            Hide
            mottaviani Mark Ottaviani added a comment -

            You can do a shallow git clone using the "checkout: General SCM" step instead of the Git step.

            The snippet generator will provide the code.

            Show
            mottaviani Mark Ottaviani added a comment - You can do a shallow git clone using the "checkout: General SCM" step instead of the Git step. The snippet generator will provide the code.
            Hide
            novakov Maciej Nowak added a comment -

            When I used:

            checkout scm: [
                     $class: 'GitSCM',
                     extensions: [
                       [$class: 'CloneOption', noTags: false, reference: '', shallow: true]
                     ]
                   ]
            

            I got error:

            ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
            
            Show
            novakov Maciej Nowak added a comment - When I used: checkout scm: [ $class: 'GitSCM' , extensions: [ [$class: 'CloneOption' , noTags: false , reference: '', shallow: true ] ] ] I got error: ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
            Hide
            mottaviani Mark Ottaviani added a comment -

            A checkout scm step still need to know where it's getting code from, which is probably the source of the error.

            userRemoteConfigs: [[credentialsId: 'xxxxx', url: 'ssh://git@yoursite.com/project/repo.git']]

            Show
            mottaviani Mark Ottaviani added a comment - A checkout scm step still need to know where it's getting code from, which is probably the source of the error. userRemoteConfigs: [ [credentialsId: 'xxxxx', url: 'ssh://git@yoursite.com/project/repo.git'] ]
            Hide
            novakov Maciej Nowak added a comment -

            I've done something like this:

              checkout([
                    $class: 'GitSCM',
                    branches: scm.branches,
                    doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
                    extensions: scm.extensions + [[$class: 'CloneOption', noTags: false, reference: '', shallow: true]],
                    submoduleCfg: [],
                    userRemoteConfigs: scm.userRemoteConfigs
                  ])
            

            It didn't failed with exception but there still is no --depth parameter in git fetch call

            Show
            novakov Maciej Nowak added a comment - I've done something like this: checkout([ $class: 'GitSCM' , branches: scm.branches, doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations, extensions: scm.extensions + [[$class: 'CloneOption' , noTags: false , reference: '', shallow: true ]], submoduleCfg: [], userRemoteConfigs: scm.userRemoteConfigs ]) It didn't failed with exception but there still is no --depth parameter in git fetch call
            Hide
            dodoent Nenad Miksa added a comment -

            Maciej Nowak, your trick causes following exception on my side:

            org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method hudson.plugins.git.GitSCM getBranches

            Show
            dodoent Nenad Miksa added a comment - Maciej Nowak , your trick causes following exception on my side: org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method hudson.plugins.git.GitSCM getBranches
            Hide
            dodoent Nenad Miksa added a comment -

            OK, solved by adding permission in In Process Script Approval options.

            Show
            dodoent Nenad Miksa added a comment - OK, solved by adding permission in In Process Script Approval options.
            Hide
            mheinzerling Martin Heinzerling added a comment - - edited

            It didn't failed with exception but there still is no --depth parameter in git fetch call

            Just in case anybody is searching for any hint in the logs. There are non in success case. See https://github.com/jenkinsci/git-client-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java#L491 . You just need to check if .git/objects/info/alternates exists.

            Show
            mheinzerling Martin Heinzerling added a comment - - edited It didn't failed with exception but there still is no --depth parameter in git fetch call Just in case anybody is searching for any hint in the logs. There are non in success case. See https://github.com/jenkinsci/git-client-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java#L491 . You just need to check if .git/objects/info/alternates exists.
            Hide
            ssbarnea Sorin Sbarnea added a comment -

            Somehow it seems that adding the exception is not possible in all cases. I get the same exception and but I the Process Script Approval does not prompt me to add it, and as you know it has not list that I could manually update. Due to this I raised https://issues.jenkins-ci.org/browse/JENKINS-42860

            If you have a hint on how can I address this it would be extremely useful. I only hope it would NOT involve patching some files on Jenkins master because these pipeline jobs do run on multiple masters (at least 4) and on some of them I do not even have direct access.

            Show
            ssbarnea Sorin Sbarnea added a comment - Somehow it seems that adding the exception is not possible in all cases. I get the same exception and but I the Process Script Approval does not prompt me to add it, and as you know it has not list that I could manually update. Due to this I raised https://issues.jenkins-ci.org/browse/JENKINS-42860 If you have a hint on how can I address this it would be extremely useful. I only hope it would NOT involve patching some files on Jenkins master because these pipeline jobs do run on multiple masters (at least 4) and on some of them I do not even have direct access.

              People

              • Assignee:
                jglick Jesse Glick
                Reporter:
                novakov Maciej Nowak
              • Votes:
                0 Vote for this issue
                Watchers:
                8 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: