In Jenkins, how to checkout a project into a specific directory (using GIT)

227,146

Solution 1

The default git plugin for Jenkins does the job quite nicely.

After adding a new git repository (project configuration > Source Code Management > check the GIT option) to the project navigate to the bottom of the plugin settings, just above Repository browser region. There should be an Advanced button. After clicking it a new form should appear, with a value described as Local subdirectory for repo (optional). Setting this to folder will make the plugin to check out the repository into the folder relative to your workspace. This way you can have as many repositories in your project as you need, all in separate locations.

Alternatively, if the project you're using will allow that, you can use GIT sub modules, which are similar to external paths in SVN. In the GIT Book there is a section on that very topic. If that will not be against some policy, submodules are fairly simple to use, giving you powerful way to control the locations, versions/tags/branches that will be imported AND it will be available on your local repository as well giving you better portability.

Obviously the GIT plugin supports checking out submodules, so Jenkins can work with them quite effectively.

Solution 2

In the new Jenkins 2.0 pipeline (previously named the Workflow Plugin), this is done differently for:

  • The main repository
  • Other additional repositories

Here I am specifically referring to the Multibranch Pipeline version 2.9.

Main repository

This is the repository that contains your Jenkinsfile.

In the Configure screen for your pipeline project, enter your repository name, etc.

Do not use Additional Behaviors > Check out to a sub-directory. This will put your Jenkinsfile in the sub-directory where Jenkins cannot find it.

In Jenkinsfile, check out the main repository in the subdirectory using dir():

dir('subDir') {
    checkout scm
}

Additional repositories

If you want to check out more repositories, use the Pipeline Syntax generator to automatically generate a Groovy code snippet.

In the Configure screen for your pipeline project:

  1. Select Pipeline Syntax. In the Sample Step drop down menu, choose checkout: General SCM.
  2. Select your SCM system, such as Git. Fill in the usual information about your repository or depot.
  3. Note that in the Multibranch Pipeline, environment variable env.BRANCH_NAME contains the branch name of the main repository.
  4. In the Additional Behaviors drop down menu, select Check out to a sub-directory
  5. Click Generate Groovy. Jenkins will display the Groovy code snippet corresponding to the SCM checkout that you specified.
  6. Copy this code into your pipeline script or Jenkinsfile.

Solution 3

I agree with @Łukasz Rżanek that we can use git plugin

But, I use option: checkout to a sub-direction what is enable as follow:
In Source Code Management, tick Git
click add button, choose checkout to a sub-directory

enter image description here

Solution 4

It's worth investigating the Pipeline plugin. With the plugin you can checkout multiple VCS projects into relative directory paths. Beforehand creating a directory per VCS checkout. Then issue commands to the newly checked out VCS workspace. In my case I am using git. But you should get the idea.

node{
    def exists = fileExists 'foo'
    if (!exists){
        new File('foo').mkdir()
    }
    dir ('foo') {
        git branch: "<ref spec>", changelog: false, poll: false, url: '<clone url>'
        ......
    }
    def exists = fileExists 'bar'
    if (!exists){
        new File('bar').mkdir()
    }
    dir ('bar') {
        git branch: "<ref spec>", changelog: false, poll: false, url: '<clone url>'
        ......
    }
    def exists = fileExists 'baz'
    if (!exists){
        new File('baz').mkdir()
    }
    dir ('baz') {
        git branch: "<ref spec>", changelog: false, poll: false, url: '<clone url>'
        ......
    }
}

Solution 5

Furthermore, if in the same Jenkins project we need to checkout several private GitHub repositories into several separate dirs under a project root. How can we do it please?

The Jenkin's Multiple SCMs Plugin has solved the several repositories problem for me very nicely. I have just got working a project build that checks out four different git repos under a common folder. (I'm a bit reluctant to use git super-projects as suggested previously by Łukasz Rżanek, as git is complex enough without submodules.)

Share:
227,146

Related videos on Youtube

viebel
Author by

viebel

Love Javascript and Maths (especially Axiomatic Set Theory). Appreciate Functional Programming especially Clojure and ClojureScript.

Updated on July 08, 2022

Comments

  • viebel
    viebel almost 2 years

    Sorry for the 'svn' style - we are in a process of migration from SVN to GIT (including our CI Jenkins environment).

    What we need is to be able to make Jenkins to checkout (or should I say clone?) the GIT project (repository?) into a specific directory. We've tried some refspecs magic but it wasn't too obvious to understand and use successfully.

    Furthermore, if in the same Jenkins project we need to checkout several private GitHub repositories into several separate dirs under a project root, how can we do it please?

    We have GitHub plugin installed. Hope we've phrased the things right.

  • ubuntudroid
    ubuntudroid almost 11 years
    There is only one Local subdirectory for repo setting is for all repositories so this is not really suitable for managing multiple repositories. See issues.jenkins-ci.org/browse/JENKINS-13086.
  • Zitrax
    Zitrax about 8 years
    I don't even see this option using Git plugin 2.4.2. This answer is 4 years old so maybe the option has been removed ? ( I do however see the "Checkout to a sub-directory" as mentioned in another answer by @biolinh, but that is for all repositories.
  • Pavel Leonov
    Pavel Leonov almost 8 years
    Multiple SCM doesn't show changes for each build, there is a bug and they already 1.5 year didn't fix it :(
  • Pavel Leonov
    Pavel Leonov almost 8 years
    But you can't set different folders for different repos.
  • biolinh
    biolinh almost 8 years
    In my case, my Jenkins job only clone code from one repository. I haven't tried to do this case you said.
  • John McGehee
    John McGehee over 7 years
    This gives the error, Scripts not permitted to use new java.io.File java.lang.String, which is true. See my answer for a way to use the pipeline code snippet generator to create the code you need for your pipeline script or Jenkinsfile.
  • davegallant
    davegallant over 7 years
    Code snippet here: checkout([$class: 'GitSCM', branches: [[name: '*/branchname']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'MyDirectory']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'myId', url: 'https://github.com/jenkinsci/jenkins.git']]])
  • Mark Waite
    Mark Waite about 7 years
    "Checkout to subdirectory" is in the "Additional Behaviours" section of the git plugin. However, that still limits you to only a single git repository per job. If you need more than one git repository per job, use the Jenkins pipeline.
  • Mig82
    Mig82 almost 7 years
    Actually I think the answer by @davey_dev is the right one. If you first step into a subdir with the dir('subDir') step then the repo will be cloned into another sub-directory of subDir and you'll get subDir/[repo name]/[contents]. I was looking for a way to get dubDir\[contents] so using the relativeTargetDir was the right way to do it.
  • Andrey Rodionov
    Andrey Rodionov over 6 years
    You could use sh "mkdir -p directory as a workaround for Scripts not permitted...
  • tvt173
    tvt173 almost 6 years
    In addition to this, if you are doing this with the main repository, you may want to also use the skipDefaultCheckout option as shown in this post to avoid checking out twice devops.stackexchange.com/a/1074
  • Jeremy
    Jeremy over 5 years
    This will throw an error when trying to use git executable saying there is no .git file
  • amleszk
    amleszk about 4 years
    What if we dont want it to be a sub directory? can you use "../other_dir" - Answering my own quastion, you can use a relative checkout, and in the build shell change to that directory using "cd ../other_dir"
  • Sighil
    Sighil about 3 years
    dir ('foo') {} will create folder if it not exists