如何修复Github Travis CI构建中git子模块更新的权限被拒绝(公钥)错误?

时间:2013-03-28 04:01:58

标签: git github git-submodules travis-ci

我无法使用错误更新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构建通过,我仍然可以点击仓库中的子模块直接指向它?

5 个答案:

答案 0 :(得分:71)

这可以(幸运的是)通过在Travis上即时修改.gitmodules文件来轻松解决,以便在初始化子模块之前用公共URL替换SSH 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)的gist,我从中得出了这个解决方案。

如果您的子模块是私有存储库,它应该可以在https网址中包含凭据,我建议为此目的设置一个具有受限权限的GitHub access token

# Replace <user> and <token> with your GitHub username and access token respectively
- sed -i 's/git@github.com:/https:\/\/<user>:<token>@github.com\//' .gitmodules

答案 1 :(得分:24)

我建议对子模块使用https方案,因为这样可以让你在本地推送Travis并推送:https://github.com/quadroid/clonejs.git

答案 2 :(得分:13)

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

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

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

答案 3 :(得分:6)

您收到此错误是因为您通过ssh-urls指定了子模块。要从travis-ci环境访问ssh,您需要configure a key

或者,您可以使用git子模块的相对URL,因为您进行了项目,并且您的子模块在Github上都可用。

Git会针对ORIGIN解析相对网址。

示例:

使用.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

答案 4 :(得分:0)

您也可以通过git直接操作.gitmodules文件。 (灵感来自this answer)。

git config --file=.gitmodules submodule.SUBMODULE_PATH.url https://github.com/ORG/REPO.git