Search code examples
gitjenkinsgroovyjenkins-pipelinegit-submodules

Implicitly loading submodules in Jenkins pipeline stages


Is there a way to modify a Jenkins pipeline script using Git repositories to automatically init submodules?

pipeline {
 stages {
  stage('Something A') {
    steps {
      sh 'git submodule update --init'
      // stuff
    }
  }
  stage('Something B') {
    steps {
      sh 'git submodule update --init'
      // stuff
    }
  }
 }
}

Should preferably be updated to something like

pipeline {
 options { submodule auto }
 stages {
  stage('Something A') {
    steps {
      // stuff
    }
  }
  stage('Something B') {
    steps {
      // stuff
    }
  }
 }
}

Is there an implemented way of doing this? I couldn't find any.


Solution

  • I assume you have been using the git step so far to clone a repository.

    However for advanced features (like submodules) there's the checkout step available: https://jenkins.io/doc/pipeline/steps/workflow-scm-step/

    The checkout steps provides an option to update all submodules and can even update the submodules recursively, e.g.:

    checkout([$class: 'GitSCM',
        branches: [[name: '*/master']],
        doGenerateSubmoduleConfigurations: false,
        extensions: [[$class: 'SubmoduleOption',
            disableSubmodules: false,
            parentCredentials: false,
            recursiveSubmodules: false,
            reference: '',
            trackingSubmodules: false
        ]],
        submoduleCfg: [],
        userRemoteConfigs: [[url: 'ssh://myserver/myrepo']]
    ])
    

    What actually enables cloning of the submodules is the SubmoduleOption extension as seen in the example above.

    As the syntax is - let's say - little bit more complex I recommend using the snipped generator.