Django和VirtualEnv开发/部署最佳实践 [英] Django and VirtualEnv Development/Deployment Best Practices
问题描述
只是好奇人们如何与virtualenv一起部署他们的Django项目
Just curious how people are deploying their Django projects in combination with virtualenv
- 更具体地说,你如何使生产virtualenv正确同步与您的开发机器?
我使用git为scm,但我没有我的virtualenv里面的git repo - 最好使用点冻结,然后使用冻结输出在服务器上重新创建环境? (如果你这样做,你可以描述这些步骤 - 我在解冻过程中找到很少的好的文档 - 像 pip install -r freeze_output.txt
可能吗? )
I use git for scm but I don't have my virtualenv inside the git repo - should I, or is it best to use the pip freeze and then re-create the environment on the server using the freeze output? (If you do this, could you please describe the steps - I am finding very little good documentation on the unfreezing process - is something like pip install -r freeze_output.txt
possible?)
推荐答案
我刚刚在工作中使用pip,Fabric和git设置了这样的东西。流程基本上是这样的,并且从这个脚本:
I just set something like this up at work using pip, Fabric and git. The flow is basically like this, and borrows heavily from this script:
- 在我们的源代码树中,我们维护一个require.txt文件。我们将手动维护。
- 当我们执行新版本时,Fabric脚本将根据我们传递的任何树状结构创建归档。
- Fabric会找到我们正在部署的SHA,使用
git log -1 --format = format:%h TREEISH
。这将给我们SHA_OF_THE_RELEASE
- Fabric将获取我们的需求文件的最后一个SHA,具有
git log -1 - -format = format:%h SHA_OF_THE_RELEASE requirements.txt
。这散发出了散文的简短版本,如1d02afc
,这是该特定版本的该文件的SHA。 - 然后,脚本将查看我们的virtualenvs存储在远程主机上的目录。
- In our source tree, we maintain a requirements.txt file. We'll maintain this manually.
- When we do a new release, the Fabric script creates an archive based on whatever treeish we pass it.
- Fabric will find the SHA for what we're deploying with
git log -1 --format=format:%h TREEISH
. That gives usSHA_OF_THE_RELEASE
- Fabric will get the last SHA for our requirements file with
git log -1 --format=format:%h SHA_OF_THE_RELEASE requirements.txt
. This spits out the short version of the hash, like1d02afc
which is the SHA for that file for this particular release. - The Fabric script will then look into a directory where our virtualenvs are stored on the remote host(s).
- 如果没有名为
1d02afc
的目录,则会创建一个新的virtualenv并使用pip install -E / path / to / venv / 1d02afc -r /path/to/requirements.txt
- 如果是现有的
路径/到/ venv / 1d02afc
,没有任何事情完成
- If there is not a directory named
1d02afc
, a new virtualenv is created and setup withpip install -E /path/to/venv/1d02afc -r /path/to/requirements.txt
- If there is an existing
path/to/venv/1d02afc
, nothing is done
这个小魔术的一部分是通过任何你想要的git的树,并让它做包装(从Fabric)。通过使用 git存档my-branch
, git archive 1d02afc
或其他任何东西,我保证获得正确的包安装在我的远程机器上。
The little magic part of this is passing whatever tree-ish you want to git, and having it do the packaging (from Fabric). By using git archive my-branch
, git archive 1d02afc
or whatever else, I'm guaranteed to get the right packages installed on my remote machines.
我去了这条路线,因为如果软件包在发布之间没有改变,我真的不想有额外的虚拟机浮动。我也不喜欢在我自己的源代码树中使用我依赖的实际包的想法。
I went this route since I really didn't want to have extra virtuenvs floating around if the packages hadn't changed between release. I also don't like the idea of having the actual packages I depend on in my own source tree.
这篇关于Django和VirtualEnv开发/部署最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!