在 Heroku 应用程序上 pip 安装私有 git 存储库的正确/安全方法是什么?

Jos*_*der 1 git pip github heroku

应用程序结构(Python FastAPI):

-my_app
  -server.py
  -Procfile
  -requirements.txt
Run Code Online (Sandbox Code Playgroud)

为了安装 Heroku 应用程序所需的私有 git 存储库,我将以下行添加到我的requirements.txt

git+https://<github-token>@github.com/me/my-private-repo.git
Run Code Online (Sandbox Code Playgroud)

然而,在推送时,Github 给我发了一封电子邮件,说由于我在提交中暴露了我的令牌,所以它已撤销了该令牌。(我的应用程序存储库是私有的。)完全公平!但是,我的 Heroku 构建现在失败了,因为它在尝试安装私有存储库时提示输入密码。

我在互联网上搜索过很多次 re: private repos,但总是遇到相互矛盾的建议。

我们将很高兴听到在这种情况下,在自动化构建中安全安装私有存储库的最佳实践是什么。

到目前为止我尝试过的:

  • git+git://username:password@github.com/me/myrepo.git而不是令牌显然有同样的问题
  • git+ssh://git@github.com/me/myrepo.git- 产生错误Host key verification failed.
  • 将用户名:密码(或令牌)存储为 Heroku 环境变量 - 从这里看来,这是不可能的pip

要扩展该ssh选项,请在我的本地计算机上执行以下操作:

  • pip3 install git+ssh://git@github.com/me/my_private-repo.git
  • git clone https://github.com/me/my_private-repo.git

然而,当我的requirements.txtcontains时git+ssh://git@github.com/me/my_private-repo.git,我的 Heroku 构建会返回Host key verification failed. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.

Jos*_*der 6

终于开始工作了。我感谢 Michel Blancard 的回答和相关要点,以及 Bo Jeanes 的定制 buidpack

requirements.txt

git+ssh://git@github.com/me/my-private-repo.git
Run Code Online (Sandbox Code Playgroud)

将我的 SSH 私钥转换为 Heroku 的(旧)PEM 格式(!):

ssh-keygen  -f ~/.ssh/id_rsa -m PEM -p
Run Code Online (Sandbox Code Playgroud)

(由于这个答案而致谢)

添加 SSH 私钥作为 Heroku 变量:

heroku config:set BUILDPACK_SSH_KEY="$(cat ~/.ssh/id_rsa)"
Run Code Online (Sandbox Code Playgroud)

添加自定义构建包以在启用 SSH 私钥的 Python 构建包之前运行:

heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-ssh-key.git
Run Code Online (Sandbox Code Playgroud)

部署!

  • 最初的构建包现在已不再维护,请使用heroku org上的构建包:https://github.com/heroku/heroku-buildpack-ssh-key,文档:https://elements.heroku.com/buildpacks/heroku/heroku -buildpack-ssh-密钥 (2认同)