如何修复 Github Travis CI 构建中 git 子模块更新的权限被拒绝(公钥)错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15674064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix a permission denied (publickey) error for a git submodule update in the Github Travis CI build?
提问by Quadroid
I cannot update the git submodule, with the error:
我无法更新 git 子模块,出现错误:
$ git submodule init
Submodule 'build/html' ([email protected]: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.
But when I do the same tasks locally, everything is OK.
但是当我在本地执行相同的任务时,一切正常。
How do I fix this so that the Travis CI build passes and I can still click on the submodule in the repo to direct to it?
如何解决此问题,以便 Travis CI 构建通过,并且我仍然可以单击存储库中的子模块以指向它?
回答by aknuds1
This can (thankfully) be easily solved by modifying the .gitmodules file on-the-fly on Travis, so that the SSH URL is replaced with the public URL, before initializing submodules. To accomplish this, add the following to .travis.yml:
这可以(谢天谢地)通过在 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/[email protected]:/https:\/\/github.com\//' .gitmodules
- git submodule update --init --recursive
Thanks to Michael Iedema for his gistfrom which I derived this solution.
感谢 Michael Iedema 的要点,我从中得出了这个解决方案。
If your submodules are private repositories, it should work to include credentials in the https URLs, I recommend making a GitHub access tokenwith restricted permissions for this purpose:
如果您的子模块是私有存储库,则应该可以在 https URL 中包含凭据,我建议为此目的制作具有受限权限的GitHub 访问令牌:
# Replace <user> and <token> with your GitHub username and access token respectively
- sed -i 's/[email protected]:/https:\/\/<user>:<token>@github.com\//' .gitmodules
回答by sarahhodne
I'd recommend using the https
scheme for submodules, as that'll allow you to pull on Travis and push locally: https://github.com/quadroid/clonejs.git
.
我建议https
对子模块使用该方案,因为这将允许您使用 Travis 并在本地推送:https://github.com/quadroid/clonejs.git
.
回答by emidander
Travis now supports accessing submodule using ssh, which is by far the easiest solution. You only need to associate your ssh key (or the ssh key of a dedicated CI user) with the Github project you are building, as described in the documentation for private dependencies.
Travis 现在支持使用 ssh 访问子模块,这是迄今为止最简单的解决方案。您只需要将您的 ssh 密钥(或专用 CI 用户的 ssh 密钥)与您正在构建的 Github 项目相关联,如私有依赖项文档中所述。
$ travis sshkey --upload ~/.ssh/id_rsa -r myorg/main
$ travis sshkey --upload ~/.ssh/id_rsa -r myorg/main
Note that Travis recommends creating a dedicated user so that you do not have to use your own ssh key.
请注意,Travis 建议创建一个专用用户,这样您就不必使用自己的 ssh 密钥。
回答by maxschlepzig
You get this error because you specified your submodules via ssh-urls. For ssh access from the travis-ci environment you need to configure a key.
您收到此错误是因为您通过 ssh-urls 指定了子模块。要从 travis-ci 环境进行 ssh 访问,您需要配置一个 key。
Alternatively, you can just use relative URLs for your git submodules since you project and your submodules are all available on Github.
或者,您可以只对 git 子模块使用相对 URL,因为您的项目和子模块都可以在 Github 上使用。
Git resolves relative urls against the ORIGIN
.
Git 解析相对于ORIGIN
.
Example:
例子:
Using the first 2 entries from your .gitmodules
:
使用您的前 2 个条目.gitmodules
:
[submodule "lib/es5-shim"]
path = lib/es5-shim
url = [email protected]:kriskowal/es5-shim.git
[submodule "build/html"]
path = build/html
url = [email protected]:quadroid/clonejs.git
Replaced with relative URLs:
替换为相对 URL:
[submodule "lib/es5-shim"]
path = lib/es5-shim
url = ../../kriskowal/es5-shim.git
[submodule "build/html"]
path = build/html
url = ../clonejs.git
Then when cloning - say - via https the origin is set like this:
然后当克隆 - 比如说 - 通过 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)
When cloning via ssh:
通过 ssh 克隆时:
$ git clone [email protected]:quadroid/clonejs.git
$ cd clonejs
$ git remote -v
origin [email protected]:quadroid/clonejs.git (fetch)
origin [email protected]:quadroid/clonejs.git (push)
With relative urls, the usual submodule sequence works independently of the origin:
使用相对 url,通常的子模块序列独立于来源工作:
$ git submodule init
$ git submodule update
回答by loudmouth
You can also just directly manipulate your .gitmodules file via git
. (Inspired by this answer).
您也可以直接通过 .gitmodules 文件操作您的 .gitmodules 文件git
。(受到这个答案的启发)。
git config --file=.gitmodules submodule.SUBMODULE_PATH.url https://github.com/ORG/REPO.git