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

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


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 SampleStep drop down menu, choose checkout: General SCM.
  2. Select your SCM system, such as Git. Fill in the usual informationabout your repository or depot.
  3. Note that in the Multibranch Pipeline, environment variableenv.BRANCH_NAME contains the branch name of the main repository.
  4. In the Additional Behaviors drop down menu, selectCheck out to a sub-directory
  5. Click Generate Groovy. Jenkins will display the Groovy code snippetcorresponding to the SCM checkout that you specified.
  6. Copy this code into your pipeline script or Jenkinsfile.


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


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.