简体   繁体   English

如何在使用Gitlab时在Ubuntu上部署Django应用程序

[英]How to deploy Django app on Ubuntu when using Gitlab

I have create a simple Django application, then I have commit and push to Gitlab. 我创建了一个简单的Django应用程序,然后我提交并推送到Gitlab。 Finally I would like to install that application on my "production server" which is running ubuntu so from that server terminal I run the command: 最后我想在运行ubuntu的“生产服务器”上安装该应用程序,以便从该服务器终端运行命令:

git clone https://domain.com/path/to/git

I cannot find my python code among the file cloned. 我无法在克隆的文件中找到我的python代码。

What is the correct way to get the python code from the gitlab repository? 从gitlab存储库获取python代码的正确方法是什么?

First of all, 首先,

does your git has all source code including the pip installed apps? 您的git是否包含所有源代码,包括pip安装的应用程序?

  • If yes: install the django version on your ubuntu machine, clone your project and possibly it will run. 如果是:在你的ubuntu机器上安装django版本,克隆你的项目,它可能会运行。
  • If no: you must create a virtual enviroment (which is a recommended option in any case), install all the pip installed apps AND django inside the virtual enviroment. 如果不是:您必须创建虚拟环境 (在任何情况下都是推荐的选项),在虚拟环境中安装所有pip安装的应用程序 django。 After that clone your project and it will run. 之后克隆你的项目,它将运行。

Well i don't know about gitlab, and there's basically everything missing about your configuration on the server, but you can set up a git hook on a git repo. 好吧,我不知道gitlab,在服务器上基本上没有关于你的配置的一切,但是你可以在git repo上设置一个git hook。

here is and example of a post-receive hook to deploy an example django project. 这是一个用于部署示例django项目的post-receive钩子的示例。

#!/bin/bash
while read oldrev newrev ref
do
    if [[ $ref =~ .*/master$ ]];
    then
        echo "master branch received, deploying..."
        GIT_WORK_TREE="/PATH/TO/DJANGO/PROJECT" git checkout -f master > /dev/null

        source /PATH/TO/DJANGO/VIRTUAL/ENVIRONMENT/bin/activate > /dev/null
        echo "installing new pip dependencies..."
        pip install -r /PATH/TO/DJANGO/PROJECT/dependencies.txt > /dev/null
        echo "applaying new db migrations..."
        python /PATH/TO/DJANGO/PROJECT/manage.py migrate_schemas > /dev/null
        deactivate > /dev/null

        touch /PATH/TO/DJANGO/PROJECT/PROJECT_NAME/wsgi.py > /dev/null
        echo "Sever code reloaded."
    else
        echo "Received branch $ref, not deploying."
    fi
done

so a few thing to consider: 所以要考虑一些事情:

  • This script is located on gitrepo.git/hooks/post-recive 此脚本位于gitrepo.git / hooks / post-recive
  • The file post-recive must be executable chmod +x FILE_NAME post-recive文件必须是可执行文件chmod +x FILE_NAME
  • It assumes you have a virtual environment. 它假设您有一个虚拟环境。
  • It assumes you have a file on the repo called dependencies.txt, you can generate it with pip freeze > dependencies.txt (note, everything on the file will be insatled on the server) 它假设您在repo上有一个名为dependencies.txt的文件,您可以使用pip freeze > dependencies.txt生成它(请注意,文件上的所有内容都将嵌入到服务器上)
  • Note touch wsgi.py would reload the code if your running apache with mod_wsgi on daemon mode ( more info ) 注意touch wsgi.py将重新加载代码,如果您在守护进程模式下运行带有mod_wsgi的apache( 更多信息

问题是分支,我解决了选择一个特定的分支,应用选项“-b”后跟正确的分支名称“develop”:

git clone -b develop https://example.com/username/projectname.git

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM