如何在声明性管道中检出具有不同部署密钥的 git 子模块

How to checkout git submodule with different deploy key in declarative pipeline

我在企业 GitHub 实例中托管了一个项目。该项目引用同一实例中的子模块。

注意:我不能在 GitHub Enterprise 上使用相同的部署密钥:Key Already in Use

Jenkins 作业被定义为多分支管道,我在 Jenkinsfile 中使用 checkout 命令:

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

我尝试了两个选项:设置 parentCredentials: trueparentCredentials: false,但是在执行作业时两者都会导致此错误:

hudson.plugins.git.GitException: Command "git submodule update --init --recursive my-submodule" returned status code 1: stdout: stderr: Cloning into '/private/tmp/workspace/project_develop/my-submodule'... git@github.mycompany.com: Permission denied (publickey). fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists. fatal: clone of 'git@github.mycompany.de:myorga/my-submodule.git' into submodule path '/private/tmp/workspace/project_develop/my-submodule' failed Failed to clone 'my-submodule'. Retry scheduled Cloning into '/private/tmp/workspace/project_develop/my-submodule'... git@github.mycompany.de: Permission denied (publickey). fatal: Could not read from remote repository.

Please make sure you have the correct access rights

两个部署密钥都可以在 Jenkins 上使用。那么,我怎样才能检出子模块呢?在检出子模块时,如何告诉 checkout 命令使用特定的 credentials-id

我认为这远远低于 git 配置,而且 SCM 支持的边缘用例太多。

关于 GitSCM 的选项submoduleCfg,但它只允许您设置子模块的名称和分支。

package hudson.plugins.git;

public class SubmoduleConfig implements java.io.Serializable {
    private static final long serialVersionUID = 1L;
    String   submoduleName;
    String[] branches;

如果您要从使用密钥转为使用凭据,那么您可以使用硬编码更新子模块配置 user/password。

git submodule init
git config submodule.my_submodule.url "https://username:password@url/my_submodule.git"
git submodule update