casparjespersen
casparjespersen

Reputation: 3870

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.

Upvotes: 0

Views: 5281

Answers (2)

chrisinmtown
chrisinmtown

Reputation: 4364

You can also configure the pipeline SCM checkout feature to get all submodules with Jenkins project config, and leave the Jenkinsfile short. Find the Jenkins pipeline SCM configuration item Additional Behaviors and click the Add button, pick Advanced sub-modules behaviors, and in the list of options that appears, check both the option Recursively update submodules and the option Use credentials from default remote of parent repository. I've attached a screenshot from my Jenkins (version 2.303.1) showing the list of options with the required boxes checked.

screenshot of Jenkins SCM additional behaviors

Upvotes: 0

Joerg S
Joerg S

Reputation: 5149

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.

Upvotes: 3

Related Questions