Jenkins 管道 git 命令子模块更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42290133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:48:36  来源:igfitidea点击:

Jenkins pipeline git command submodule update

gitjenkinsjenkins-pipeline

提问by Passionate Engineer

I want to update submodule on git clone.

我想在 git clone 上更新子模块。

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

有没有办法用 Jenkins 管道 Git 命令来做到这一点?

Currently I'm doing this...

目前我正在这样做......

git branch: 'master',
    credentialsId: 'bitbucket',
    url: 'ssh://bitbucket.org/hello.git'

It doesn't however update submodule once cloned

但是一旦克隆它就不会更新子模块

采纳答案by VonC

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

使用当前的Git 插件,您甚至不需要那个。

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

GIT 插件支持带有子模块的存储库,而子模块本身又具有子模块。
但这必须打开:

在作业配置 -> 部分源代码管理,Git -> 高级按钮(在要构建的分支下)-> 递归更新子模块

But the OP is using pipeline.

但是 OP 正在使用管道。

So a simple first build step is enough:

所以一个简单的第一个构建步骤就足够了:

git submodule update --init --recursive

However, the OP adds:

但是,OP补充说:

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

是的,但如果我正在使用sh 'git submodule update --init --recursive',这将使用$HOME/id_rsa对吗?如果可能的话,我想为这个命令传递我的私钥。

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.

有可能:在流水线语法中,您可以定义环境变量
这意味着您可以设置GIT_SSH_COMMAND使用 Git 2.10+)。
这允许您引用您自己的私钥

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.

如果任何克隆涉及 ssh url,则该 ssh 克隆将使用正确的私钥。

回答by Pom12

The git commandas 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.

作为管道步骤的git 命令相当有限,因为它提供了更复杂的checkout 命令的默认实现。对于更高级的配置,您应该使用checkout 命令,您可以为其传递大量参数,包括所需的子模块配置。

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 !

从文档来看,编写这些行通常很麻烦,我建议您改用 Jenkins 非常好Snippet Generator(YourJenkins > yourProject > PipelineSyntax)来自动生成结帐行!

回答by deepelement

checkout([
    $class: 'GitSCM', 
    branches: scm.branches, 
    doGenerateSubmoduleConfigurations: false, 
    extensions: [[
      $class: 'SubmoduleOption', 
      disableSubmodules: false, 
      parentCredentials: true, 
      recursiveSubmodules: true, 
      reference: '', 
      trackingSubmodules: false
    ]], 
    submoduleCfg: [], 
    userRemoteConfigs: scm.userRemoteConfigs
  ])