2013-03-28 150 views
34

無法更新git子模塊。遇到錯誤:github子模塊訪問權限(Travis CI)

$ 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. 

完整的日誌: https://travis-ci.org/quadroid/clonejs/jobs/5855553

但是當我做本地相同的任務,一切都很好。

+0

通過更改子模塊的網址,以固定的git://github.com/quadroid/clonejs.git 但現在我不能在本地推動這個模塊... – Quadroid 2013-03-28 05:06:34

+0

使用[部署鍵(HTTPS:/ /help.github.com/categories/30/articles),或讓腳本只在travis中更改URL,而不是在本地 – 2013-03-28 11:05:14

回答

20

我建議使用子模塊的https方案,因爲這可以讓你拉上Travis並在本地推送:https://github.com/quadroid/clonejs.git

62

通過在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 

感謝Michael Iedema他gist從中我得出這一解決方案。

如果你的子模塊的私人倉庫,它應該工作,包括在https網址憑證,我建議在做針對此用途受限權限的GitHub access token

# Replace <user> and <token> with your GitHub username and access token respectively 
- sed -i 's/[email protected]:/https:\/\/<user>:<token>@github.com\//' .gitmodules 
+3

到目前爲止,這是迄今爲止這個問題的最佳解決方案! – 2014-07-09 07:51:12

+0

@StefanDragnev謝謝,我的目標是取悅;) – aknuds1 2014-07-09 17:23:44

+1

這似乎不適用於私人存儲庫。所有這些私人存儲庫都在同一個github帳戶下,並開啓了travis。任何人都有線索?謝謝。 – inder 2014-07-17 17:59:41

11

特拉維斯現在支持使用ssh訪問子模塊,其是迄今爲止最簡單的解決方案。您只需將您的ssh密鑰(或專用CI用戶的ssh密鑰)與您正在構建的Github項目相關聯,如documentation for private dependencies中所述。

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

注意,特拉維斯建議創建一個專用的用戶,這樣你就不必使用自己的SSH密鑰。

+1

爲了使這個解釋更加完整;找到一個可以訪問Travis正在構建的倉庫和子模塊(Travis建議爲此創建一個特定的CI用戶)的用戶,然後運行這個命令*執行構建*的repo。 – thoutbeckers 2014-09-04 16:46:17

+1

它不是在travis.org上可用(只有travis.com,不是免費的) – Toilal 2014-09-24 04:46:57

+0

這並不能真正提高安全性嗎?讓你的github密鑰傳播.. – matanster 2015-10-29 13:17:30

5

你得到這個錯誤是因爲你通過ssh-urls指定了子模塊。對於travis-ci環境下的ssh訪問,您需要configure a key

另外,你可以使用相對的URL爲你的git子模塊,因爲你的項目和你的子模塊都在Github上可用。

Git針對ORIGIN解析相關網址。

例子:

使用從.gitmodules第2項:

[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 

替換爲相對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 [email protected]:quadroid/clonejs.git 
$ cd clonejs 
$ git remote -v         
origin [email protected]:quadroid/clonejs.git (fetch) 
origin [email protected]:quadroid/clonejs.git (push) 

相對URL,通常的子模塊順序獨立工作的由來:

$ git submodule init 
$ git submodule update 
+0

在我的情況下,相對URL的工作非常好,這可能是最優雅的公共回購解決方案。感謝您提出這項技術! – matanster 2015-10-24 01:50:22

+0

令人討厭的是,它似乎打破了github的自動超鏈接到submodules github頁面。 – matanster 2015-10-24 02:02:03

+0

這不適用於私人子模塊。 Travis似乎隱藏了它使用私有令牌訪問回購的事實,但是在克隆子模塊時卻沒有這樣做。 – 2017-10-17 23:51:37

0

用戶也可以直接通過直接操縱git您.gitmodules文件。 (受this answer的啓發)。

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