• Icon: New Feature New Feature
    • Resolution: Duplicate
    • Icon: Major Major
    • pipeline

      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.

          [JENKINS-33022] Allow Git shallow clone in Multibranch pipeline

          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.

          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.

          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.
          

          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.

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

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

          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

          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

          Nenad Miksa added a comment -

          novakov, 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

          Nenad Miksa added a comment - novakov , 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

          Nenad Miksa added a comment -

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

          Nenad Miksa added a comment - OK, solved by adding permission in In Process Script Approval options.

          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.

          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.

          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.

          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.

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

              Created:
              Updated:
              Resolved: