Jenkins管道git命令子模块更新

Pas*_*per 23 git jenkins jenkins-pipeline

我想更新git clone上的子模块.

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

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

git branch: 'master',
    credentialsId: 'bitbucket',
    url: 'ssh://bitbucket.org/hello.git'
Run Code Online (Sandbox Code Playgroud)

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

Pom*_*m12 41

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

你想要使用的可能是这样的:

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']]])
Run Code Online (Sandbox Code Playgroud)

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

  • 如果你在userRemoteConfigs中使用credentialsId,你还应该在subModuleOption中使用**parentCredentials: true**,如果没有权限被拒绝将会继续出现:) (3认同)
  • 是的,但是您正在为声明性管道语法提供答案和代码示例,而@PassionateDeveloper似乎正在使用jenkins管道标准语法 (2认同)
  • 对于那些获得“权限被拒绝(公钥)”的人。您必须将凭据与 URL 位于同一行,例如:`userRemoteConfigs: [[credentialsId: '45345trete-92eb-4eb0-8844-1fd7ghj76', url: 'ssh://git@bitbucket..... git']]])` 。要获取 _Credentials ID_,请单击 Jenkins 主页右侧工具栏中的 _Credentials_。您将看到一个包含您的凭据的表格,**ID** 是该表格的一个字段。 (2认同)

Von*_*onC 25

使用当前的Git插件,您甚至不需要它.

GIT插件支持具有子模块的存储库,子模块本身又具有子模块.
必须打开它:

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

但OP正在使用管道.

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

git submodule update --init --recursive
Run Code Online (Sandbox Code Playgroud)

但是,OP增加了:

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

有可能:在Pipeline语法中,您可以定义环境变量.
这意味着你可以设置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'
            }
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)

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

  • 不过,这与 git 管道有关。如何使用 Pipeline 项目进行配置? (2认同)

dee*_*ent 17

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