在Heroku应用上通过点子安装私有git repo的正确/安全方法是什么?

时间:2020-06-07 09:32:45

标签: git github heroku pip

应用程序结构(Python FastAPI):

-my_app
  -server.py
  -Procfile
  -requirements.txt

为了安装Heroku应用程序所需的私有git repo,我在requirements.txt中添加了以下行:

git+https://<github-token>@github.com/me/my-private-repo.git

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

我曾多次在SO / Internet上搜索过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环境变量-从here看来,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.txt包含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.

1 个答案:

答案 0 :(得分:4)

最后使它起作用。我要感谢Michel Blancard的answer和相关的gist,以及Bo Jeanes的custom buidpack

requirements.txt中:

git+ssh://git@github.com/me/my-private-repo.git

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

ssh-keygen  -f ~/.ssh/id_rsa -m PEM -p

(归功于this answer的信用)

将私有SSH密钥添加为Heroku变量:

heroku config:set SSH_KEY="$(cat ~/.ssh/id_rsa)"

添加this自定义buildpack以在启用私有SSH密钥的Python buildpack之前运行:

heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-ssh-key.git

部署!