![](/img/trans.png)
[英]pip install package from private github repo with deploy key in docker
[英]Is it possible to use pip to install a package from a private GitHub repository?
我正在嘗試從私有 GitHub 存儲庫安裝 Python 包。 對於公共存儲庫,我可以發出以下運行良好的命令:
pip install git+git://github.com/django/django.git
但是,如果我嘗試將其用於私有存儲庫:
pip install git+git://github.com/echweb/echweb-utils.git
我得到以下輸出:
Downloading/unpacking git+git://github.com/echweb/echweb-utils.git
Cloning Git repository git://github.com/echweb/echweb-utils.git to /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-VRsIoo-build
Complete output from command /usr/local/bin/git clone git://github.com/echweb/echweb-utils.git /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-VRsIoo-build:
fatal: The remote end hung up unexpectedly
Cloning into /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-VRsIoo-build...
----------------------------------------
Command /usr/local/bin/git clone git://github.com/echweb/echweb-utils.git /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-VRsIoo-build failed with error code 128
我想這是因為我試圖在不提供任何身份驗證的情況下訪問私有存儲庫。 因此,我嘗試使用 Git + ssh
希望 pip 使用我的 SSH 公鑰進行身份驗證:
pip install git+ssh://github.com/echweb/echweb-utils.git
這給出了以下輸出:
Downloading/unpacking git+ssh://github.com/echweb/echweb-utils.git
Cloning Git repository ssh://github.com/echweb/echweb-utils.git to /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-DQB8s4-build
Complete output from command /usr/local/bin/git clone ssh://github.com/echweb/echweb-utils.git /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-DQB8s4-build:
Cloning into /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-DQB8s4-build...
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
----------------------------------------
Command /usr/local/bin/git clone ssh://github.com/echweb/echweb-utils.git /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-DQB8s4-build failed with error code 128
我正在努力實現的目標是可能的嗎? 如果是這樣,我該怎么做?
您可以使用git+ssh
URI 方案,但您必須設置用戶名。 注意 URI 中的git@
部分:
pip install git+ssh://git@github.com/echweb/echweb-utils.git
另請閱讀部署密鑰。
PS:在我的安裝中,“git+ssh”URI 方案僅適用於“可編輯”要求:
pip install -e URI#egg=EggName
請記住:在pip
命令中使用遠程地址之前,將git remote -v
打印的:
字符更改為/
字符:
$ git remote -v
origin git@github.com:echweb/echweb-utils.git (fetch)
# ^ change this to a '/' character
如果你忘記了,你會得到這個錯誤:
ssh: Could not resolve hostname github.com:echweb:
nodename nor servname provided, or not known
作為一項附加技術,如果您在本地克隆了私有存儲庫,您可以執行以下操作:
pip install git+file://c:/repo/directory
更現代的是,您可以這樣做(並且-e
意味着您不必在更改反映之前提交更改):
pip install -e C:\repo\directory
您可以像這樣直接使用 HTTPS URL 進行操作:
pip install git+https://github.com/username/repo.git
例如,這也適用於在Django項目的 requirements.txt 中附加該行。
它也適用於Bitbucket :
pip install git+ssh://git@bitbucket.org/username/projectname.git
在這種情況下,Pip 將使用您的 SSH 密鑰。
我發現使用令牌比使用 SSH 密鑰容易得多。 我在這方面找不到太多好的文檔,所以我主要通過反復試驗找到了這個解決方案。 此外,從 pip 和 setuptools 安裝有一些細微的差別; 但這種方式應該適用於兩者。
GitHub(目前,截至 2016 年 8 月)不提供獲取私有存儲庫的 zip/tarball 的簡單方法。 所以你需要告訴 setuptools 你指向一個 Git 倉庫:
from setuptools import setup
import os
# Get the deploy key from https://help.github.com/articles/git-automation-with-oauth-tokens/
github_token = os.environ['GITHUB_TOKEN']
setup(
# ...
install_requires='package',
dependency_links = [
'git+https://{github_token}@github.com/user/{package}.git/@{version}#egg={package}-0'
.format(github_token=github_token, package=package, version=master)
]
這里有幾點注意事項:
我想出了一種自動“pip install”一個不需要密碼提示的 GitLab 私有存儲庫的方法。 這種方法使用 GitLab“部署密鑰”和 SSH 配置文件,因此您可以使用個人 SSH 密鑰以外的密鑰進行部署(在我的情況下,供“機器人”使用)。 也許有好心人可以使用 GitHub 進行驗證。
ssh-keygen -t rsa -C "GitLab_Robot_Deploy_Key"
該文件應顯示為~/.ssh/GitLab_Robot_Deploy_Key
和~/.ssh/GitLab_Robot_Deploy_Key.pub
。
將~/.ssh/GitLab_Robot_Deploy_Key.pub
文件的內容復制並粘貼到 GitLab“部署密鑰”對話框中。
以下命令告訴 SSH 使用您的新部署密鑰來設置連接。 成功后,您應該會收到消息:“歡迎來到 GitLab,用戶名!”
ssh -T -i ~/.ssh/GitLab_Robot_Deploy_Key git@gitlab.mycorp.com
接下來,使用編輯器創建一個~/.ssh/config
文件。 添加以下內容。 'Host' 值可以是您想要的任何值(記住它,因為稍后您將使用它)。 HostName 是 GitLab 實例的 URL。 identifyFile 是您在第一步中創建的 SSH 密鑰文件的路徑。
Host GitLab
HostName gitlab.mycorp.com
IdentityFile ~/.ssh/GitLab_Robot_Deploy_Key
oxyum 為我們提供了在 SSH 中使用 pip 的秘訣:
pip install git+ssh://git@gitlab.mycorp.com/my_name/my_repo.git
我們只需要稍微修改一下,讓 SSH 使用我們的新部署密鑰。 我們通過將 SSH 指向 SSH 配置文件中的主機條目來做到這一點。 只需將命令中的 'gitlab.mycorp.com' 替換為我們在 SSH 配置文件中使用的主機名即可:
pip install git+ssh://git@GitLab/my_name/my_repo.git
該軟件包現在應該在沒有任何密碼提示的情況下安裝。
如果你想從CI服務器或類似的需求文件中安裝依賴項,你可以這樣做:
git config --global credential.helper 'cache'
echo "protocol=https
host=example.com
username=${GIT_USER}
password=${GIT_PASS}
" | git credential approve
pip install -r requirements.txt
就我而言,我使用GIT_USER=gitlab-ci-token
和GIT_PASS=${CI_JOB_TOKEN}
。
這種方法有一個明顯的優勢。 您有一個包含所有依賴項的需求文件。
需求文件的語法在這里給出:
https://pip.pypa.io/en/latest/reference/pip_install.html#requirements-file-format
例如,使用:
-e git+http://github.com/rwillmer/django-behave#egg=django-behave
如果您希望源在安裝后保留。
要不就
git+http://github.com/rwillmer/django-behave#egg=django-behave
如果您只想安裝它。
如果您需要在命令行單行中執行此操作,它也是可能的。 我能夠在 Google Colab 上進行部署:
pip install git+https://<PERSONAL ACCESS TOKEN>@github.com/<USERNAME>/<REPOSITORY>.git
您還可以通過git+https://github.com/... URL 安裝私有存儲庫依賴項,方法是使用.netrc
文件為curl提供登錄憑據(登錄名和密碼,或部署令牌):
echo "machine github.com login ei-grad password mypasswordshouldbehere" > ~/.netrc
pip install "git+https://github.com/ei-grad/my_private_repo.git#egg=my_private_repo"
我的情況比答案中描述的大多數情況都復雜。 我是 Github 組織中兩個私有存儲庫repo_A
和repo_B
的所有者,並且需要在 repo_B 的python
單元測試期間pip install repo_A
repo_B
Github 操作。
我為解決此任務而遵循的步驟:
repo_B
下創建了一個存儲庫機密,將我的個人訪問令牌粘貼在那里並將其命名為PERSONAL_ACCESS_TOKEN
。 這很重要,因為與Jamie提出的解決方案不同,我不需要在 github 操作.yml
文件中顯式公開我寶貴的原始個人訪問令牌。pip install
包,如下所示:export PERSONAL_ACCESS_TOKEN=${{ secrets.PERSONAL_ACCESS_TOKEN }}
pip install git+https://${PERSONAL_ACCESS_TOKEN}@github.com/MY_ORG_NAME/repo_A.git
如果不想使用 SSH,可以在 HTTPS URL 中添加用戶名和密碼。
下面的代碼假定您在工作目錄中有一個名為“pass”的文件,其中包含您的密碼。
export PASS=$(cat pass)
pip install git+https://<username>:$PASS@github.com/echweb/echweb-utils.git
當我從 GitHub 安裝時,我可以使用:
pip install git+ssh://git@github.com/<username>/<projectname>.git#egg=<eggname>
但是,由於我必須將 pip 作為sudo
運行,SSH 密鑰不再適用於 GitHub,並且“git clone”在“Permission denied (publickey)”上失敗。 使用git+https
可以讓我以 sudo 身份運行命令,並讓 GitHub 詢問我的用戶名/密碼。
sudo pip install git+https://github.com/<username>/<projectname>.git#egg=<eggname>
如果你在 GitHub、GitLab 等上有自己的庫/包,你必須添加一個標簽來提交庫的具體版本,例如 v2.0,然后你可以安裝你的包:
pip install git+ssh://link/name/repo.git@v2.0
這對我有用。 其他解決方案對我不起作用。
oxyum 的解決方案對於這個答案是可以的。 我只想指出,如果您使用sudo
進行安裝,則需要小心,因為密鑰也必須為 root 存儲(例如, /root/.ssh
)。
然后你可以輸入
sudo pip install git+ssh://git@github.com/echweb/echweb-utils.git
只需從原始git clone
命令(或從git remote -v
)復制遠程。 你會得到這樣的東西:
比特桶: git+ssh://git@bitbucket.org:your_account/my_pro.git
GitHub: git+ssh://git@github.com:your_account/my_pro.git
接下來,您需要將:
替換為域名旁邊的/
。
所以安裝使用:
pip install git+ssh://git@bitbucket.org/your_account/my_pro.git
我正在嘗試從私有GitHub存儲庫安裝Python軟件包。 對於公共存儲庫,我可以發出以下正常運行的命令:
pip install git+git://github.com/django/django.git
但是,如果我嘗試將其用於私有存儲庫:
pip install git+git://github.com/echweb/echweb-utils.git
我得到以下輸出:
Downloading/unpacking git+git://github.com/echweb/echweb-utils.git
Cloning Git repository git://github.com/echweb/echweb-utils.git to /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-VRsIoo-build
Complete output from command /usr/local/bin/git clone git://github.com/echweb/echweb-utils.git /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-VRsIoo-build:
fatal: The remote end hung up unexpectedly
Cloning into /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-VRsIoo-build...
----------------------------------------
Command /usr/local/bin/git clone git://github.com/echweb/echweb-utils.git /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-VRsIoo-build failed with error code 128
我猜這是因為我試圖在不提供任何身份驗證的情況下訪問私有存儲庫。 因此,我嘗試使用Git + ssh
希望pip使用我的SSH公鑰進行身份驗證:
pip install git+ssh://github.com/echweb/echweb-utils.git
這給出了以下輸出:
Downloading/unpacking git+ssh://github.com/echweb/echweb-utils.git
Cloning Git repository ssh://github.com/echweb/echweb-utils.git to /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-DQB8s4-build
Complete output from command /usr/local/bin/git clone ssh://github.com/echweb/echweb-utils.git /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-DQB8s4-build:
Cloning into /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-DQB8s4-build...
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
----------------------------------------
Command /usr/local/bin/git clone ssh://github.com/echweb/echweb-utils.git /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-DQB8s4-build failed with error code 128
我正在努力實現的目標是否可能? 如果是這樣,我該怎么辦?
你可以試試
pip install git+git@gitlab.mycorp.com/my_name/my_repo.git
沒有ssh:...
這對我行得通。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.