如何修复Github Travis CI版本中git子模块更新的权限被拒绝(公钥)错误?
我无法更新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构建能够通过,并且我仍然可以单击存储库中的子模块以将其定向?
(很幸运)可以通过在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
我建议对子模块使用https
方案,因为这将允许您拉Travis并在本地推送:https://github.com/quadroid/clonejs.git
。
Travis现在支持使用ssh访问子模块,这是迄今为止最简单的解决方案。 您只需要将ssh密钥(或专用CI用户的ssh密钥)与正在构建的Github项目相关联,如私有依赖关系文档中所述。
$ travis sshkey --upload ~/.ssh/id_rsa -r myorg/main
请注意,Travis建议创建一个专用用户,这样您就不必使用自己的ssh密钥。
您收到此错误是因为您通过ssh-urls指定了子模块。 为了从travis-ci环境进行ssh访问,您需要配置一个密钥。
另外,您可以只为git子模块使用相对URL,因为您进行了投影,并且所有子模块都在Github上可用。
Git根据.gitmodules
解析相对网址。
例:
使用.gitmodules
中的前2个条目:
[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
替换为相对网址:
[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
您也可以通过git
直接操作.gitmodules文件(受此答案启发)。
git config --file=.gitmodules submodule.SUBMODULE_PATH.url https://github.com/ORG/REPO.git