如何为多个环境定制一个require.txt? [英] How to customize a requirements.txt for multiple environments?
问题描述
组织最好的方法是什么?
我想到的是:
- 维护单独的需求文件,每个分支中都有一个生存频繁合并!)
- 告诉Heroku要使用哪个需求文件(环境变量?)
- 编写部署脚本(创建临时分支,修改需求文件,提交,部署,删除temp分支)
您可以级联您的需求文件并使用-r标志来告诉pip将一个文件的内容包含在另一个文件中。您可以将这些要求分解成如下所示的模块化文件夹层次结构:
` - django_project_root
| - 要求
| | - common.txt
| | - dev.txt
| ` - prod.txt
` - requirements.txt
文件内容将看起来像这样:
common.txt:
#包含要求所有环境的共同点
req1 == 1.0
req2 == 1.0
req3 == 1.0
...
dev.txt:
#仅指定特定于开发人员的需求
#但是也可以导入常用的
-r common.txt
dev_req == 1.0
...
prod.txt:
#同样的...
-r common.txt
prod_req == 1.0
...
在Heroku之外,您现在可以设置这样的环境:
pip install -r requirements / dev.txt
或
点安装-r要求/ prod.txt
由于Heroku专门为requirements.txt项目根,应该只是mi rror prod,如下所示:
requirements.txt:
镜子prod
-r要求/ prod.txt
I have two branches, Development and Production. Each has dependencies, some of which are different. Development points to dependencies that are themselves in development. Likewise for Production. I need to deploy to Heroku which expects each branch's dependencies in a single file called 'requirements.txt'.
What is the best way to organize?
What I've thought of:
- Maintain separate requirements files, one in each branch (must survive frequent merges!)
- Tell Heroku which requirements file I want to use (environment variable?)
- Write deploy scripts (create temp branch, modify requirements file, commit, deploy, delete temp branch)
You can cascade your requirements files and use the "-r" flag to tell pip to include the contents of one file inside another. You can break out your requirements into a modular folder hierarchy like this:
`-- django_project_root
|-- requirements
| |-- common.txt
| |-- dev.txt
| `-- prod.txt
`-- requirements.txt
The files' contents would look like this:
common.txt:
# Contains requirements common to all environments
req1==1.0
req2==1.0
req3==1.0
...
dev.txt:
# Specifies only dev-specific requirements
# But imports the common ones too
-r common.txt
dev_req==1.0
...
prod.txt:
# Same for prod...
-r common.txt
prod_req==1.0
...
Outside of Heroku, you can now setup environments like this:
pip install -r requirements/dev.txt
or
pip install -r requirements/prod.txt
Since Heroku looks specifically for "requirements.txt" at the project root, it should just mirror prod, like this:
requirements.txt:
# Mirrors prod
-r requirements/prod.txt
这篇关于如何为多个环境定制一个require.txt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!