Search code examples
gitjenkinspluginsversion-controlcheckout

Jenkins GitSCM checkout in specific folder


I searched a lot, the GitSCM plugin and stackoverflow etc but could not find answer. I want to checkout Git repository via Jenkins GitSCM plugin into one specific folder say 'MyFolder'.

So if my repository is xyz.git then after checkout all files of repository should be in MyFolder/* and NOT in MyFolder/xyz/*.

To understand more, I want to achieve below (assuming repository is xyz.git) with Jenkins GitSCM plugin. Below command will checkout files in MyFolder excluding root folder xyz

git clone <REPOSITORY> -- MyFolder

and below command will checkout files in xyz folder

git clone <REPOSITORY> 

For reference I am working on below configuration

  checkout([$class: 'GitSCM', branches: [[name: gitDefaultBranch]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout'],[$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: true, recursiveSubmodules: true, reference: '', trackingSubmodules: false]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: gitCreds, url: gitProjectUrl]]])

Please suggest. Versions information:

  • Jenkins 2.121.1 core
  • Pipeline: Declarative 1.3.2
  • Pipeline: Groovy 2.55
  • Git plugin 3.9.1
  • org.jenkins-ci.plugins:scm-api:2.3.0
  • org.jenkins-ci.plugins.workflow:workflow-scm-step:2.7

Solution

  • You can use the [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'MyFolder/']] in you checkout code.

    Something like below:-

     checkout([   $class: 'GitSCM',
             branches: [[name: gitDefaultBranch]],
             doGenerateSubmoduleConfigurations: false,
             extensions: [[$class: 'CleanBeforeCheckout'],
                          [$class: 'SubmoduleOption',
                           disableSubmodules: false,
                           parentCredentials: true,
                           recursiveSubmodules: true,
                           reference: '',
                           trackingSubmodules: false],
                          [$class: 'RelativeTargetDirectory',
                           relativeTargetDir: 'MyFolder/']],
             submoduleCfg: [],
             userRemoteConfigs: [[credentialsId: gitCreds, url: gitProjectUrl]]
           ])