Jenkins pipeline git command submodule update

前端 未结 3 1549
甜味超标
甜味超标 2020-12-08 02:31

I want to update submodule on git clone.

Is there a way to do this with Jenkins pipeline Git command?

Currently I\'m doing this...

git branch         


        
相关标签:
3条回答
  • 2020-12-08 03:10
    checkout([
        $class: 'GitSCM', 
        branches: scm.branches, 
        doGenerateSubmoduleConfigurations: false, 
        extensions: [[
          $class: 'SubmoduleOption', 
          disableSubmodules: false, 
          parentCredentials: true, 
          recursiveSubmodules: true, 
          reference: '', 
          trackingSubmodules: false
        ]], 
        submoduleCfg: [], 
        userRemoteConfigs: scm.userRemoteConfigs
      ])
    
    0 讨论(0)
  • 2020-12-08 03:16

    The git command as a pipeline step is rather limited as it provides a default implementation of the more complex checkout command. For more advanced configuration, you should use checkout command, for which you can pass a whole lot of parameters, including the desired submodules configuration.

    What you want to use is probably something like this :

    checkout([$class: 'GitSCM',
              branches: [[name: '*/master']],
              doGenerateSubmoduleConfigurations: false,
              extensions: [[$class: 'SubmoduleOption',
                            disableSubmodules: false,
                            parentCredentials: false,
                            recursiveSubmodules: true,
                            reference: '',
                            trackingSubmodules: false]], 
              submoduleCfg: [], 
              userRemoteConfigs: [[url: 'your-git-server/your-git-repository']]])
    

    From the documentation it is often cumbersome to write these kind of lines, I recommand you use instead Jenkins very good Snippet Generator (YourJenkins > yourProject > PipelineSyntax) to automatically generate the checkout line !

    0 讨论(0)
  • 2020-12-08 03:25

    With the current Git plugin, you don't even need that.

    The GIT plugin supports repositories with submodules which in turn have submodules themselves.
    This must be turned on though:

    in Job Configuration -> Section Source Code Management, Git -> Advanced Button (under Branches to build) -> Recursively update submodules

    But the OP is using pipeline.

    So a simple first build step is enough:

    git submodule update --init --recursive
    

    However, the OP adds:

    Yes but if I'm using sh 'git submodule update --init --recursive', this will use $HOME/id_rsa right? I want to pass in my private key for this command if possible.

    It is possible: In the Pipeline syntax, you can define environment variables.
    Which means you can set GIT_SSH_COMMAND (with Git 2.10+).
    That allows you to reference your own private key.

    pipeline {
        agent any
    
        environment {
            GIT_SSH_COMMAND = 'ssh -i /path/to/my/private/key'
        }
    
        stages {
            stage('Build') {
                steps {
                    sh 'printenv'
                    sh 'git submodule update --init --recursive'
                }
            }
        }
    } 
    

    If any clone involve an ssh url, that ssh clone will use the right private key.

    0 讨论(0)
提交回复
热议问题