简体   繁体   中英

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. 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:

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

I cannot find my python code among the file cloned.

What is the correct way to get the python code from the gitlab repository?

First of all,

does your git has all source code including the pip installed apps?

  • If yes: install the django version on your ubuntu machine, clone your project and possibly it will run.
  • 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. 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.

here is and example of a post-receive hook to deploy an example django project.

#!/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
  • The file post-recive must be executable 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)
  • Note touch wsgi.py would reload the code if your running apache with mod_wsgi on daemon mode ( more info )

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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