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

Qua*_*oid 43 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.
Run Code Online (Sandbox Code Playgroud)

但是当我在本地执行相同的任务时,一切都很好.

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

akn*_*ds1 72

这可以(幸运的是)通过在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
Run Code Online (Sandbox Code Playgroud)

感谢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
Run Code Online (Sandbox Code Playgroud)

  • 到目前为止,这是解决这个问题的最佳解决方案! (4认同)
  • 如果您在 Mac OS X 系统上并且想知道为什么 sed 失败,这是由于特定于 macs 的 sed 版本需要 -i 选项后的 -e 选项。 (2认同)

sar*_*dne 24

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


emi*_*der 13

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

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

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


max*_*zig 6

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

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

Git解决了相对的网址ORIGIN.

例:

使用您的前两个条目.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
Run Code Online (Sandbox Code Playgroud)

替换为相对URL:

[submodule "lib/es5-shim"]
        path = lib/es5-shim
        url = ../../kriskowal/es5-shim.git
[submodule "build/html"]
        path = build/html
        url = ../clonejs.git
Run Code Online (Sandbox Code Playgroud)

然后当克隆 - 比如说 - 通过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)
Run Code Online (Sandbox Code Playgroud)

通过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)
Run Code Online (Sandbox Code Playgroud)

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

$ git submodule init
$ git submodule update
Run Code Online (Sandbox Code Playgroud)