【问题标题】:Jenkins pipeline git command submodule updateJenkins 管道 git 命令子模块更新
【发布时间】:2017-07-06 12:28:06
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: git jenkins jenkins-pipeline


    【解决方案1】:

    这与之前的答案略有不同,但我相信这是最好的解决方案。

    我交换了分支参数以使用env.GIT_COMMIT,以便它检查已通过作业触发的特定提交。否则它将构建特定分支的最新提交,在其他示例中,当使用scm.branches 时,它将构建分支的尖端而不是预期的提交。

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

    【讨论】:

      【解决方案2】:

      如果您知道 checkout scm 使用的凭证名称,您还可以将其显式传递给 git 命令以更新子模块:

          stage ('Clone') {
              steps {
                  checkout scm
                  withCredentials([sshUserPrivateKey(credentialsId: 'bitbucket_ssh', keyFileVariable: 'SSH_KEY')]) {
                      sh 'GIT_SSH_COMMAND="ssh -i $SSH_KEY" git submodule update --init'
                  }
              }
          }
      

      【讨论】:

        【解决方案3】:
        checkout([
            $class: 'GitSCM', 
            branches: scm.branches, 
            doGenerateSubmoduleConfigurations: false, 
            extensions: [[
              $class: 'SubmoduleOption', 
              disableSubmodules: false, 
              parentCredentials: true, 
              recursiveSubmodules: true, 
              reference: '', 
              trackingSubmodules: false
            ]], 
            submoduleCfg: [], 
            userRemoteConfigs: scm.userRemoteConfigs
          ])
        

        【讨论】:

        • 我回来并试图投票...只是注意到我之前来过这里?‍♂️任何人,再次感谢。
        【解决方案4】:

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

        你想用的大概是这样的:

        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']]])
        

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

        【讨论】:

        • 我刚刚跑到上面得到了hudson.plugins.git.GitException: Command "git submodule update --init --recursive common" returned status code 1:
        • 控制台日志中没有其他输出吗?如果你不这样做,你可能应该直接在你的 Jenkins 实例上尝试失败的命令(如果你有访问权限),以确保你的子模块“common”可以手动更新
        • 好的,这是一个不同的问题:您没有克隆子模块存储库的权限。子模块存储库应该在 Bitbucket 中配置相同的公共 SSH 密钥,以便您可以签出它。
        • 是的,但是您正在为声明性管道语法提供答案和代码示例,而 @PassionateDeveloper 似乎正在使用 jenkins 管道标准语法
        • 对于那些获得Permission denied (publickey) 的人。您必须将凭据与 URL 放在同一行,例如:userRemoteConfigs: [[credentialsId: '45345trete-92eb-4eb0-8844-1fd7ghj76', url: 'ssh://git@bitbucket.....git']]])。要获取 Credentials ID,请单击 Jenkins 主页右侧工具栏中的 Credentials。您将看到您的凭据表,ID 是该表的一个字段。
        【解决方案5】:

        使用当前的Git plugin,您甚至不需要它。

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

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

        但是 OP 正在使用管道。

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

        git submodule update --init --recursive
        

        但是,OP 补充说:

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

        有可能:在Pipeline syntax,你可以define environment variables
        这意味着您可以设置GIT_SSH_COMMAND (with Git 2.10+)。
        这样你就可以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'
                    }
                }
            }
        } 
        

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

        【讨论】:

        • 这是 git 管道。如何配置 Pipeline 项目?
        • @PassionateDeveloper 不需要管道:常规的 Git 插件就足够了。
        • 我正在使用 Pipeline 脚本,它在其中调用 git 命令。我没有更改作业配置的选项 -> 部分源代码管理。
        • 是的,但如果我使用sh 'git submodule update --init --recursive',这将使用 $HOME/id_rsa 对吗?如果可能的话,我想为这个命令传递我的私钥......
        • @daitienshi 不确定:最好作为一个单独的问题,添加操作系统版本、Git 和 Jenkins。
        猜你喜欢
        • 2020-08-16
        • 2017-05-03
        • 2023-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多