47

我无法更新 git 子模块,出现错误:

$ git submodule init
Submodule 'build/html' (git@github.com:quadroid/clonejs.git) registered for path 'build/html'
...
$ git submodule update
Cloning into 'build/html'...
Warning: Permanently added 'github.com,207.97.227.239' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

但是当我在本地执行相同的任务时,一切正常。

我该如何解决这个问题,以便 Travis CI 构建通过并且我仍然可以单击 repo 中的子模块以定向到它?

4

6 回答 6

76

这可以(谢天谢地)通过在 Travis 上即时修改 .gitmodules 文件来轻松解决,以便在初始化子模块之前将 SSH URL 替换为公共 URL。为此,请将以下内容添加到 .travis.yml:

# Handle git submodules yourself
git:
    submodules: false
# Use sed to replace the SSH URL with the public URL, then initialize submodules
before_install:
    - sed -i 's/git@github.com:/https:\/\/github.com\//' .gitmodules
    - git submodule update --init --recursive

感谢 Michael Iedema 的要点,我从中得出了这个解决方案。

如果您的子模块是私有存储库,它应该可以在 https URL 中包含凭据,我建议为此目的制作一个具有受限权限的GitHub 访问令牌:

# Replace <user> and <token> with your GitHub username and access token respectively
- sed -i 's/git@github.com:/https:\/\/<user>:<token>@github.com\//' .gitmodules
于 2014-07-06T20:49:32.043 回答
24

我建议https对子模块使用该方案,因为这将允许您使用 Travis 并在本地推送:https://github.com/quadroid/clonejs.git.

于 2013-06-16T13:30:56.533 回答
13

Travis 现在支持使用 ssh 访问子模块,这是迄今为止最简单的解决方案。您只需要将您的 ssh 密钥(或专用 CI 用户的 ssh 密钥)与您正在构建的 Github 项目相关联,如私有依赖项文档中所述。

$ travis sshkey --upload ~/.ssh/id_rsa -r myorg/main

请注意,Travis 建议创建一个专用用户,这样您就不必使用自己的 ssh 密钥。

于 2014-08-25T19:01:21.273 回答
7

您收到此错误是因为您通过 ssh-urls 指定了子模块。要从 travis-ci 环境进行 ssh 访问,您需要配置一个密钥

或者,你可以只为你的 git 子模块使用相对 URL,因为你的项目和你的子模块都在 Github 上可用。

Git 根据ORIGIN.

例子:

使用您的前 2 个条目.gitmodules

[submodule "lib/es5-shim"]
        path = lib/es5-shim
        url = git@github.com:kriskowal/es5-shim.git
[submodule "build/html"]
        path = build/html
        url = git@github.com:quadroid/clonejs.git

替换为相对 URL:

[submodule "lib/es5-shim"]
        path = lib/es5-shim
        url = ../../kriskowal/es5-shim.git
[submodule "build/html"]
        path = build/html
        url = ../clonejs.git

然后当通过 https 克隆时 - 比如说 - 原点设置如下:

$ git clone https://github.com/quadroid/clonejs.git
$ cd clonejs
$ git remote -v
origin  https://github.com/quadroid/clonejs.git (fetch)
origin  https://github.com/quadroid/clonejs.git (push)

通过 ssh 克隆时:

$ git clone git@github.com:quadroid/clonejs.git
$ cd clonejs
$ git remote -v                                
origin  git@github.com:quadroid/clonejs.git (fetch)
origin  git@github.com:quadroid/clonejs.git (push)

使用相对 url,通常的子模块序列独立于原点工作:

$ git submodule init
$ git submodule update
于 2015-04-23T22:22:44.347 回答
1

您也可以通过 .gitmodules 直接操作您的 .gitmodules 文件git。(受此答案的启发)。

git config --file=.gitmodules submodule.SUBMODULE_PATH.url https://github.com/ORG/REPO.git
于 2017-02-14T15:11:27.053 回答
1

2022 年实际

  1. 转到 travis-ci 中的 repo 设置并启用Clone or Import用作子模块的 repos 选项 克隆或导入
  1. 在你的使用https协议.gitmodules(但最好使用没有协议的相对路径)

  2. 享受。我只花了3个小时搞清楚

于 2022-01-17T14:54:30.043 回答