How to clone a directory from a remote repo in a Jenkins pipeline script?

16,723 views
Skip to first unread message

Mark Allison

unread,
May 24, 2017, 3:23:31 AM5/24/17
to Jenkins Users
Hi,

I have a large repo in GitHub Enterprise and need to clone a sub-directory from it on my Jenkins build server and just build that sub-directory. I am using a pipeline script and have this right now:

node {
    stage ('checkout') {
        git url: 'g...@github.devops.mycompany.local:Org/MyLargeRepo.git'
    }
}

What I want is to clone from github.devops.mycompany.local:Org/MyLargeRepo/path/to/subproject

I know I probably need to use sparse and/or shallow checkouts, but can't seem to work out how to configure that in a Jenkins pipeline script with the git plugin. Any ideas?

Daniel Beck

unread,
May 24, 2017, 8:29:58 AM5/24/17
to Jenkins Users

> On 24. May 2017, at 09:23, Mark Allison <marka...@gmail.com> wrote:
>
> I know I probably need to use sparse and/or shallow checkouts, but can't seem to work out how to configure that in a Jenkins pipeline script with the git plugin. Any ideas?

Use the 'checkout' step and specify Git SCM. That should allow you to use all the options available in Git. Use the snippet generator for this.

Mark Allison

unread,
May 24, 2017, 11:00:56 AM5/24/17
to Jenkins Users, m...@beckweb.net
Daniel, how does that show checking out a directory within a repo? Do you have something more concrete? The snippet generator does not show this. 

Mark Waite

unread,
May 24, 2017, 12:48:48 PM5/24/17
to jenkins...@googlegroups.com, m...@beckweb.net
Checking out to a subdirectory within a workspace requires that you nest the checkout() step inside a dir("subdir-name") step.  The snippet generator only shows one at a time (as far as I recall), so it won't show you that combination.


Mark Waite
 
 

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/56b64b99-70dc-4e38-9a6a-6ba29e28aa1a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Allison

unread,
May 31, 2017, 11:32:17 AM5/31/17
to Jenkins Users, m...@beckweb.net


On Wed, May 24, 2017 at 9:01 AM Mark Allison <marka...@gmail.com> wrote:
On Wednesday, 24 May 2017 13:29:58 UTC+1, Daniel Beck wrote:

> On 24. May 2017, at 09:23, Mark Allison <marka...@gmail.com> wrote:
>
> I know I probably need to use sparse and/or shallow checkouts, but can't seem to work out how to configure that in a Jenkins pipeline script with the git plugin. Any ideas?

Use the 'checkout' step and specify Git SCM. That should allow you to use all the options available in Git. Use the snippet generator for this.

Daniel, how does that show checking out a directory within a repo? Do you have something more concrete? The snippet generator does not show this.

Checking out to a subdirectory within a workspace requires that you nest the checkout() step inside a dir("subdir-name") step.  The snippet generator only shows one at a time (as far as I recall), so it won't show you that combination.


Hi Mark,

I am trying to checkout FROM a remote subdirectory within a repo not to a subdirectory. Does anyone know how to do that with the git plugin?

Stephen Connolly

unread,
May 31, 2017, 2:36:19 PM5/31/17
to jenkins...@googlegroups.com
Git doesn't really do that.

I'd probably see if there is a command from the CLI that does what you want and then just use a `sh` step to do that

(I'm betting there is no such CLI command BTW)

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
Sent from my phone

Haibrayn González

unread,
Mar 27, 2019, 5:46:56 PM3/27/19
to Jenkins Users
From CLI you can

git checkout <sha or branch> -- path/to/subdir/

just that you would need to be in the repository so maybe clone an empty branch or the first commit of the repo instead of the large repo branch and then checkout the files from CLI


El miércoles, 31 de mayo de 2017, 13:36:19 (UTC-5), Stephen Connolly escribió:
On Wed 31 May 2017 at 16:32, Mark Allison <marka...@gmail.com> wrote:


On Wed, May 24, 2017 at 9:01 AM Mark Allison <marka...@gmail.com> wrote:
On Wednesday, 24 May 2017 13:29:58 UTC+1, Daniel Beck wrote:

> On 24. May 2017, at 09:23, Mark Allison <marka...@gmail.com> wrote:
>
> I know I probably need to use sparse and/or shallow checkouts, but can't seem to work out how to configure that in a Jenkins pipeline script with the git plugin. Any ideas?

Use the 'checkout' step and specify Git SCM. That should allow you to use all the options available in Git. Use the snippet generator for this.

Daniel, how does that show checking out a directory within a repo? Do you have something more concrete? The snippet generator does not show this.

Checking out to a subdirectory within a workspace requires that you nest the checkout() step inside a dir("subdir-name") step.  The snippet generator only shows one at a time (as far as I recall), so it won't show you that combination.


Hi Mark,

I am trying to checkout FROM a remote subdirectory within a repo not to a subdirectory. Does anyone know how to do that with the git plugin?

Git doesn't really do that.

I'd probably see if there is a command from the CLI that does what you want and then just use a `sh` step to do that

(I'm betting there is no such CLI command BTW)

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkins...@googlegroups.com.

Haibrayn González

unread,
Mar 27, 2019, 5:46:56 PM3/27/19
to Jenkins Users
Check out the snippet for git: checkout and try the sparse checkout advanced option

checkout changelog: false, poll: false, scm: [
    $class: 'GitSCM',
    branches: [[name: '*/master']],
    doGenerateSubmoduleConfigurations: false,
    extensions: [
       [
         $class: 'SparseCheckoutPaths',
         sparseCheckoutPaths: [[path: '/my/path']]
       ]
    ],
    submoduleCfg: [],
    userRemoteConfigs: [[url: 'ssh://us...@domain.com:x/11346/myrepo.git']]]
Reply all
Reply to author
Forward
0 new messages