【问题标题】:How to fix a permission denied (publickey) error for a git submodule update in the Github Travis CI build?如何修复 Github Travis CI 构建中 git 子模块更新的权限被拒绝(公钥)错误?
【发布时间】:2013-03-18 10:22:14
【问题描述】:

我无法更新 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 中的子模块以定向到它?

【问题讨论】:

标签: git github git-submodules travis-ci


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这可以(谢天谢地)通过在 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 提供的 gist,我从中得出了这个解决方案。

    如果您的子模块是私有存储库,它应该可以在 https URL 中包含凭据,我建议为此目的创建一个具有受限权限的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
    

    【讨论】:

    • 这是迄今为止解决这个问题的最佳方案!
    • 这似乎不适用于私有存储库。所有这些私有存储库都在同一个 github 帐户下,并为 travis 启用。有人有线索吗?谢谢。
    • @inder 我还没有尝试过使用私有存储库的 Travis,但是您需要一些方法来访问您的存储库。这本身就是一个问题。
    • @inder 您可以通过将 HTTPS 与用户/密码(即https://user:password@github.com/organization/repo.git)结合使用来访问您的私有存储库吗?如果是这样,您可以相应地修改 sed 命令。让我知道这是否适合您。参考Travis Pro documentation
    • 如果您在 Mac OS X 系统上并且想知道为什么 sed 会失败,这是由于 mac 特定的 sed 版本需要在 -i 选项之后加上 -e 选项。
    【解决方案3】:

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

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

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

    【讨论】:

    • 为了让这个解释更加完整;找到一个可以同时访问 Travis 正在构建的存储库和子模块的用户(Travis 建议为此创建一个特定的 CI 用户),然后运行此命令为执行构建的 repo 。跨度>
    • 它在 travis.org 上不可用(只有 travis.com,不是免费的)
    • 这并没有真正提高安全性是不是......让你的 github 密钥散布在各处......
    • @matt 我更新了我的答案,表明 Travis 建议为此使用专门的 CI 用户 - 我认为这是一个好主意,原因有几个。
    【解决方案4】:

    您收到此错误是因为您通过 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
    

    替换为相对 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
    

    【讨论】:

    • 相对 URL 在我的情况下工作得很好,这可能是公共回购最优雅的解决方案。感谢您提出这项技术!
    • 然而,令人讨厌的是,它似乎破坏了 github 对子模块 github 页面的自动超链接。
    • 这不适用于私有子模块。 travis 似乎隐藏了它使用私有令牌来访问 repo 的事实,但在克隆子模块时并没有这样做。
    【解决方案5】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-11-11
      • 2011-11-25
      • 1970-01-01
      • 2012-01-18
      • 2012-04-25
      • 2012-10-08
      相关资源
      最近更新 更多