Github同主機多倉庫部署deploy key問題處理

我的多個站點運行在同一個主機上,對應多個github私有倉庫,打包部署使用deploy key,尷尬的是github限制每個倉庫必須用不同的key,如果使用主機上面默認的那個key,大部分時候會遇到錯誤提示:

~ git clone [email protected]:kmephistoh/xxxx.git
Cloning into 'xxxx'...
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

若要同一個主機使用deploy key部署多個倉庫

首先,創建多個key

比如使用ssh-keygen創建多個密鑰對,把對應的公鑰(xxx.pub的文件)放到github的deploy key中去,千萬不要說你找不到在哪裏啊……

➜  ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/mephisto/.ssh/id_rsa): keyone
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in keyone
Your public key has been saved in keyone.pub
The key fingerprint is:
SHA256:EMDob/6SzsSOUGIdEqEUYLeSAIdJczUbl6HeLDUnthY [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|B@+=o=.oo        |
|B.B o *o         |
|.= o o.E .       |
|  = o =.*        |
|...o o =S        |
|.o .o o          |
|.  oo.           |
| . =+            |
|  ..+o.          |
+----[SHA256]-----+

➜  ls -al keyone*
-rw------- 1 mephisto mephisto 2602 Nov  4 18:19 keyone
-rw-r--r-- 1 mephisto mephisto  569 Nov  4 18:19 keyone.pub

使用同樣方法創建另外一個key,不操作演示了

其次,配置~/.ssh/config文件

示例如下:

Host github.com-repo-1
        Hostname github.com
        IdentityFile=/home/user/.ssh/repo-1_deploy_key

Host github.com-repo-2
        Hostname github.com
        IdentityFile=/home/user/.ssh/repo-2_deploy_key
  • Host github.com-repo-1 # 倉庫別名(這個別名很關鍵)
  • Hostname github.com # github域名
  • IdentityFile=/home/user/.ssh/repo-1_deploy_key # 倉庫使用的私鑰

最後,使用別名拉取代碼

注意,github.com-repo-1就是上面的別名,仔細看,替換了@和:之間的字符串,不得不說github也是會玩。示例如下:

$ git clone [email protected]:OWNER/repo-1.git

這樣設置後,你就可以正常部署代碼了, 官方文檔https://docs.github.com/en/developers/overview/managing-deploy-keys

我想一個密鑰對走天下,怎麼辦?理論上可以把個人主力電腦的密鑰對放到服務器上面去,就像你在本地電腦操作一樣,但是這樣有安全風險,所以老實按照官方文檔處理,方便和安全總是有點矛盾,除非不在乎。

相關文章:

翻譯: